Python Forum
How can I found how many numbers are there in a Collatz Sequence that I found?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I found how many numbers are there in a Collatz Sequence that I found?
#1
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())
Reply
#2
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,
Reply
#3
(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!!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Absolute paths in subprocess - file not found kittyticker 4 403 Jan-28-2024, 10:37 PM
Last Post: kittyticker
  file open "file not found error" shanoger 8 944 Dec-14-2023, 08:03 AM
Last Post: shanoger
  pyside6 module not found ForeverNoob 4 1,272 Aug-18-2023, 04:36 PM
Last Post: snippsat
  I found a problem with Python Lahearle 12 1,395 Jul-20-2023, 10:58 PM
Last Post: Pedroski55
  [SOLVED] [BeautifulSoup] Why attribute not found? Winfried 0 728 Mar-11-2023, 10:00 PM
Last Post: Winfried
  Module Not Found Error bitoded 4 1,344 Jan-01-2023, 09:08 AM
Last Post: bitoded
  Name not found in response json NewbiePyt 4 1,019 Dec-29-2022, 11:12 AM
Last Post: buran
  X13ARIMA & statsmodels: X13NotFoundError: x12a and x13as not found on path JaneTan 1 1,397 Dec-24-2022, 07:44 AM
Last Post: ndc85430
  Os.system("shutdown"); command not found cosmin1805 4 1,617 Nov-13-2022, 02:07 PM
Last Post: cosmin1805
  Using locationtagger to extract locations found in a specific country/region lord_of_cinder 1 1,222 Oct-04-2022, 12:46 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020