-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem=14.py
More file actions
27 lines (24 loc) · 746 Bytes
/
problem=14.py
File metadata and controls
27 lines (24 loc) · 746 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Longest Collatz Sequence - https://projecteuler.net/problem=14
def collatz_sequence_length(start):
length = 1
n = start
while n != 1:
if n % 2 == 0:
n //= 2
else:
n = 3 * n + 1
length += 1
return length
def longest_collatz_sequence(limit):
max_length = 0
starting_number = 0
for i in range(1, limit):
length = collatz_sequence_length(i)
if length > max_length:
max_length = length
starting_number = i
return starting_number
if __name__ == "__main__":
limit = 1_000_000
result = longest_collatz_sequence(limit)
print(f"The starting number under {limit} that produces the longest Collatz sequence is: {result}")