mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
Improve Project Euler problem 014 solution 1 (#5747)
* Improve solution * Uncomment code that has been commented due to slow execution affecting Travis * Fix
This commit is contained in:
parent
b6eb448e63
commit
e835e96856
|
@ -25,9 +25,8 @@ 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)
|
||||
|
@ -35,14 +34,18 @@ def solution(n: int = 1000000) -> int:
|
|||
>>> 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
|
||||
|
@ -50,6 +53,9 @@ def solution(n: int = 1000000) -> int:
|
|||
number = (3 * number) + 1
|
||||
counter += 1
|
||||
|
||||
if input1 not in counters:
|
||||
counters[input1] = counter
|
||||
|
||||
if counter > pre_counter:
|
||||
largest_number = input1
|
||||
pre_counter = counter
|
||||
|
|
Loading…
Reference in New Issue
Block a user