forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhailstone.py
More file actions
36 lines (27 loc) · 864 Bytes
/
hailstone.py
File metadata and controls
36 lines (27 loc) · 864 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""
Hailstone Sequence (Collatz Conjecture)
Generate the hailstone sequence starting from n: if n is even, next is n/2;
if n is odd, next is 3n + 1. The sequence ends when it reaches 1.
Reference: https://en.wikipedia.org/wiki/Collatz_conjecture
Complexity:
Time: O(unknown) - conjectured to always terminate
Space: O(sequence length)
"""
from __future__ import annotations
def hailstone(n: int) -> list[int]:
"""Generate the hailstone sequence from n to 1.
Args:
n: The starting positive integer.
Returns:
The complete hailstone sequence from n down to 1.
Examples:
>>> hailstone(8)
[8, 4, 2, 1]
>>> hailstone(10)
[10, 5, 16, 8, 4, 2, 1]
"""
sequence = [n]
while n > 1:
n = 3 * n + 1 if n % 2 != 0 else int(n / 2)
sequence.append(n)
return sequence