8000 Add collatz sequence · jeffmikels/python@cf13060 · GitHub
[go: up one dir, main page]

Skip to content

Commit cf13060

Browse files
0xp4bloabranhe
authored andcommitted
Add collatz sequence
1 parent 30e3434 commit cf13060

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

math/Collatz Conjeture/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Collatz conjecture
2+
3+
The Collatz conjecture is a conjecture in mathematics that concerns a sequence defined as follows:
4+
* Start with any positive integer n.
5+
* Then each term is obtained from the previous term as follows:
6+
* if the previous term is even, the next term is one half the previous term.
7+
* If the previous term is odd, the next term is 3 times the previous term plus 1.
8+
9+
The conjecture is that no matter what value of n, the sequence will always reach 1.

math/Collatz Conjeture/collatz.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""Collatz sequence.
2+
3+
@author: Pablo Trinidad <github.com/pablotrinidad>
4+
"""
5+
6+
7+
def collatz(n):
8+
"""Sequence generation."""
9+
l = []
10+
while n > 1:
11+
l.append(n)
12+
if n % 2 == 0:
13+
n = n / 2
14+
else:
15+
n = (3 * n) + 1
16+
l.append(n)
17+
return l
18+
19+
n = int(input("Enter an integer n to compute the Collatz sequence: "))
20+
print(collatz(n))

0 commit comments

Comments
 (0)
0