Skip to content

Improve Project Euler problem 014 solution 1 #5747

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions project_euler/problem_014/sol1.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,37 @@ def solution(n: int = 1000000) -> int:
n → n/2 (n is even)
n → 3n + 1 (n is odd)

# The code below has been commented due to slow execution affecting Travis.
# >>> solution(1000000)
# 837799
>>> solution(1000000)
837799
>>> solution(200)
171
>>> solution(5000)
3711
>>> solution(15000)
13255
"""
largest_number = 0
pre_counter = 0
largest_number = 1
pre_counter = 1
counters = {1: 1}

for input1 in range(n):
counter = 1
for input1 in range(2, n):
counter = 0
number = input1

while number > 1:
while True:
if number in counters:
counter += counters[number]
break
if number % 2 == 0:
number //= 2
counter += 1
else:
number = (3 * number) + 1
counter += 1

if input1 not in counters:
counters[input1] = counter

if counter > pre_counter:
largest_number = input1
pre_counter = counter
Expand Down