Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit cf13060

Browse filesBrowse files
0xp4bloabranhe
authored andcommitted
Add collatz sequence
1 parent 30e3434 commit cf13060
Copy full SHA for cf13060

File tree

Expand file treeCollapse file tree

2 files changed

+29
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+29
-0
lines changed

‎math/Collatz Conjeture/README.md

Copy file name to clipboard
+9Lines 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

Copy file name to clipboard
+20Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.