-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem14.py
More file actions
42 lines (32 loc) · 803 Bytes
/
problem14.py
File metadata and controls
42 lines (32 loc) · 803 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# n -> n/2 (n is even)
# n -> 3n + 1 (n is odd)
def collatz(n):
return n//2 if n % 2 == 0 else (3*n)+1
n = 13
for i in range(13, 23):
#print(collatz(n))
n = collatz(n)
# searching for the max length of a collatz sequence
# from starting number to 1, what starting number
# (under a million) has the biggest length?
# first, function to get the length of a collatz seq
# from a starting number
def collatzseqlen(n):
x = n
count = 1
while x != 1:
count += 1
x = collatz(x)
return count
# max len
maxlen = 0
# max start
maxstart = 0
# starting from 13, going to 1 million
for i in range(13, 1000000):
seqlen = collatzseqlen(i)
if (seqlen > maxlen):
maxlen = seqlen
maxstart = i
print(maxstart, maxlen)
print(i)