Skip to content

Commit 1b5eaf9

Browse files
committed
Another dummy solution which takes time to finish
1 parent 03110d1 commit 1b5eaf9

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

project_euler/problem_014/sol3.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""
2+
Problem 14: https://projecteuler.net/problem=14
3+
4+
Problem Statement:
5+
The following iterative sequence is defined for the set of positive integers:
6+
7+
n → n/2 (n is even)
8+
n → 3n + 1 (n is odd)
9+
10+
Using the rule above and starting with 13, we generate the following sequence:
11+
12+
13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1
13+
14+
It can be seen that this sequence (starting at 13 and finishing at 1) contains
15+
10 terms. Although it has not been proved yet (Collatz Problem), it is thought
16+
that all starting numbers finish at 1.
17+
18+
Which starting number, under one million, produces the longest chain?
19+
"""
20+
21+
22+
def solution(n: int = 1000000) -> int:
23+
"""Returns the number under n that generates the longest sequence using the
24+
formula:
25+
n → n/2 (n is even)
26+
n → 3n + 1 (n is odd)
27+
28+
# The code below has been commented due to slow execution affecting Travis.
29+
# >>> solution(1000000)
30+
# 837799
31+
>>> solution(200)
32+
171
33+
>>> solution(5000)
34+
3711
35+
>>> solution(15000)
36+
13255
37+
"""
38+
largest_number = 0
39+
pre_counter = 0
40+
41+
for input1 in range(n):
42+
counter = 1
43+
number = input1
44+
45+
while number > 1:
46+
if number % 2 == 0:
47+
number /= 2
48+
counter += 1
49+
else:
50+
number = (3 * number) + 1
51+
counter += 1
52+
53+
if counter > pre_counter:
54+
largest_number = input1
55+
pre_counter = counter
56+
return largest_number
57+
58+
59+
if __name__ == "__main__":
60+
print(solution(int(input().strip())))

0 commit comments

Comments
 (0)