Skip to content

Commit 1294be3

Browse files
authored
Merge pull request #128 from Pymode-Dev/PYTHON/ProblemsWithSolutions
Collatz sequence and Factorial Added
2 parents 1b7300f + 2190f28 commit 1294be3

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# collatz_sequence.py
2+
"""
3+
Description:
4+
This is a mathematical phenomenon whereby starting with any number,
5+
it will always stop at 1 using the following mathematical concepts.
6+
base number = ?
7+
if the base number is even; divide it by 2, and the answer,
8+
reassign it to the base number
9+
else 3 * base number + 1
10+
For more explanation:
11+
Collatz conjecture. (2022, October 9). In Wikipedia. https://en.wikipedia.org/wiki/Collatz_conjecture
12+
"""
13+
14+
15+
def collatz(number: int) -> int:
16+
# It stops when the number equals 1.
17+
while number != 1:
18+
if number % 2 == 0:
19+
number //= 2
20+
else:
21+
number = 3 * number + 1
22+
print(number, end=" ")
23+
24+
25+
response: int = int(input("Enter a number: ").strip())
26+
collatz(response)

PYTHON/ProblemsAndSolutions/colltaz_sequence.py

Whitespace-only changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# fibonacci.py
2+
"""
3+
Factorial is a mathematical function of f(n!):
4+
Mathematical formulae: n * (n - 1)!
5+
Further explanation: Factorial. (2022, October 4).
6+
In Wikipedia. https://en.wikipedia.org/wiki/Factorial
7+
"""
8+
9+
10+
def factorial(starting_number: int) -> int:
11+
if starting_number == 0 or starting_number == 1:
12+
return 1
13+
14+
# This is a recursive call: where it multiplies the previous with now - 1
15+
fib = starting_number * factorial(starting_number - 1)
16+
return fib
17+

PYTHON/ProblemsAndSolutions/fibonnacci.py

Whitespace-only changes.

0 commit comments

Comments
 (0)