Improve Project Euler problem 014 solution 2 (#4752)

This commit is contained in:
Maxim Smolskiy 2021-09-23 21:55:18 +03:00 committed by GitHub
parent 15d1cfabb1
commit 66a528b171
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -28,16 +28,16 @@ Which starting number, under one million, produces the longest chain?
from __future__ import annotations
def collatz_sequence(n: int) -> list[int]:
"""Returns the Collatz sequence for n."""
sequence = [n]
def collatz_sequence_length(n: int) -> int:
"""Returns the Collatz sequence length for n."""
sequence_length = 1
while n != 1:
if n % 2 == 0:
n //= 2
else:
n = 3 * n + 1
sequence.append(n)
return sequence
sequence_length += 1
return sequence_length
def solution(n: int = 1000000) -> int:
@ -54,7 +54,7 @@ def solution(n: int = 1000000) -> int:
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]