Skip to content

Improve Project Euler problem 014 solution 2 #4752

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
12 changes: 6 additions & 6 deletions project_euler/problem_014/sol2.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@
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:
Expand All @@ -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]


Expand Down