diff --git a/PYTHON/ProblemsAndSolutions/collatz_sequence.py b/PYTHON/ProblemsAndSolutions/collatz_sequence.py new file mode 100644 index 0000000..eefbe80 --- /dev/null +++ b/PYTHON/ProblemsAndSolutions/collatz_sequence.py @@ -0,0 +1,26 @@ +# collatz_sequence.py +""" +Description: +This is a mathematical phenomenon whereby starting with any number, +it will always stop at 1 using the following mathematical concepts. +base number = ? +if the base number is even; divide it by 2, and the answer, +reassign it to the base number +else 3 * base number + 1 +For more explanation: +Collatz conjecture. (2022, October 9). In Wikipedia. https://en.wikipedia.org/wiki/Collatz_conjecture +""" + + +def collatz(number: int) -> int: + # It stops when the number equals 1. + while number != 1: + if number % 2 == 0: + number //= 2 + else: + number = 3 * number + 1 + print(number, end=" ") + + +response: int = int(input("Enter a number: ").strip()) +collatz(response) diff --git a/PYTHON/ProblemsAndSolutions/colltaz_sequence.py b/PYTHON/ProblemsAndSolutions/colltaz_sequence.py new file mode 100644 index 0000000..e69de29 diff --git a/PYTHON/ProblemsAndSolutions/factorial.py b/PYTHON/ProblemsAndSolutions/factorial.py new file mode 100644 index 0000000..55dfd0a --- /dev/null +++ b/PYTHON/ProblemsAndSolutions/factorial.py @@ -0,0 +1,17 @@ +# fibonacci.py +""" +Factorial is a mathematical function of f(n!): +Mathematical formulae: n * (n - 1)! +Further explanation: Factorial. (2022, October 4). +In Wikipedia. https://en.wikipedia.org/wiki/Factorial +""" + + +def factorial(starting_number: int) -> int: + if starting_number == 0 or starting_number == 1: + return 1 + + # This is a recursive call: where it multiplies the previous with now - 1 + fib = starting_number * factorial(starting_number - 1) + return fib + diff --git a/PYTHON/ProblemsAndSolutions/fibonnacci.py b/PYTHON/ProblemsAndSolutions/fibonnacci.py new file mode 100644 index 0000000..e69de29