mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 23:11:09 +00:00
Improve Project Euler problem 014 solution 2 (#4752)
This commit is contained in:
parent
15d1cfabb1
commit
66a528b171
|
@ -28,16 +28,16 @@ Which starting number, under one million, produces the longest chain?
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
|
||||||
def collatz_sequence(n: int) -> list[int]:
|
def collatz_sequence_length(n: int) -> int:
|
||||||
"""Returns the Collatz sequence for n."""
|
"""Returns the Collatz sequence length for n."""
|
||||||
sequence = [n]
|
sequence_length = 1
|
||||||
while n != 1:
|
while n != 1:
|
||||||
if n % 2 == 0:
|
if n % 2 == 0:
|
||||||
n //= 2
|
n //= 2
|
||||||
else:
|
else:
|
||||||
n = 3 * n + 1
|
n = 3 * n + 1
|
||||||
sequence.append(n)
|
sequence_length += 1
|
||||||
return sequence
|
return sequence_length
|
||||||
|
|
||||||
|
|
||||||
def solution(n: int = 1000000) -> int:
|
def solution(n: int = 1000000) -> int:
|
||||||
|
@ -54,7 +54,7 @@ def solution(n: int = 1000000) -> int:
|
||||||
13255
|
13255
|
||||||
"""
|
"""
|
||||||
|
|
||||||
result = max((len(collatz_sequence(i)), i) for i in range(1, n))
|
result = max((collatz_sequence_length(i), i) for i in range(1, n))
|
||||||
return result[1]
|
return result[1]
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user