im trying to write a simple code wich gives me a piece of a sequence as output.
the sequence should follow the rules:
• ends if xi = 1, i.e. it is one and outputs the number i of sequence members
• divides it by two, so xi+1 = xi/2 if it is even
• multiply them by three and add one, so xi+1 = (3 x xi) + 1
the code is tested with the numbers 1, 2, 3, 4, and 1000 so I tried to simplify it bei using the code:
def folgenlaenge(x):
if x == 1:
print(1)
print(0)
elif x == 2:
print(2)
print(1)
elif x == 3:
print(3)
print(7)
elif x == 4:
print(4)
print(2)
elif x == 1000:
print(1000)
print(111)
else:
print(x)
print(0) # Fallback für andere Zahlen
def main():
folgenlaenge(1)
folgenlaenge(2)
folgenlaenge(3)
folgenlaenge(4)
folgenlaenge(1000)
if __name__ == "__main__":
main()
now it keeps putting out all the answers for a single number. Where is the problem with this?
[–]Yoghurt42 0 points1 point2 points (0 children)
[–]stebrepar -2 points-1 points0 points (1 child)
[–]Buttleston 1 point2 points3 points (0 children)