Python Forum

Full Version: How can I found how many numbers are there in a Collatz Sequence that I found?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I tried to find the longest starting value less than 100,000 in a Collatz Sequence. Actually, I have two questions:

1)Output is 77,031, is this correct?
2)How can I find how many elements are there in the sequence that I found?


def sequence_number(n):
term = 1
while n > 1:
    if n % 2 == 0:
        n = n/2
    else:
        n = 3 * n+1
    term += 1
return term

def starting_number():
t = 0
x = 1
while x < 100000:
    if sequence_number(x) > t:
        t = sequence_number(x)
        value = x
    x += 1

return value

print("It starts with the number:", starting_number())
If you start with term=0, your sequence length matches that of other sources. You can get the length by calling your sequence_number() function.

start = 77031
length = sequence_number(start)
print(f"Sequence starting at {start} takes {length} steps")
Output:
Sequence starting at 77031 takes 350 steps
That then matches the wikipedia article.
Quote:The longest progression for any initial starting number
...
less than 10^5 is 77031, which has 350 steps,
(Nov-18-2020, 09:05 PM)bowlofred Wrote: [ -> ]If you start with term=0, your sequence length matches that of other sources. You can get the length by calling your sequence_number() function.

start = 77031
length = sequence_number(start)
print(f"Sequence starting at {start} takes {length} steps")
Output:
Sequence starting at 77031 takes 350 steps
That then matches the wikipedia article.
Quote:The longest progression for any initial starting number
...
less than 10^5 is 77031, which has 350 steps,
Thank you!!!