MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / collatz_sequence_length

Function collatz_sequence_length

project_euler/problem_014/sol2.py:34–41  ·  view source on GitHub ↗

Returns the Collatz sequence length for n.

(n: int)

Source from the content-addressed store, hash-verified

32
33
34def collatz_sequence_length(n: int) -> int:
35 """Returns the Collatz sequence length for n."""
36 if n in COLLATZ_SEQUENCE_LENGTHS:
37 return COLLATZ_SEQUENCE_LENGTHS[n]
38 next_n = n // 2 if n % 2 == 0 else 3 * n + 1
39 sequence_length = collatz_sequence_length(next_n) + 1
40 COLLATZ_SEQUENCE_LENGTHS[n] = sequence_length
41 return sequence_length
42
43
44def solution(n: int = 1000000) -> int:

Callers 1

solutionFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected