File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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.
Original file line number Diff line number Diff line change 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 ))
You can’t perform that action at this time.
0 commit comments