mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Merge pull request #447 from rafagarciac/master
Improve and Refactor the fibonnaciSeries.py (Recursion)
This commit is contained in:
commit
7418ee7b4e
18
Maths/FibonacciSequenceRecursion.py
Normal file
18
Maths/FibonacciSequenceRecursion.py
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
# Fibonacci Sequence Using Recursion
|
||||||
|
|
||||||
|
def recur_fibo(n):
|
||||||
|
return n if n <= 1 else (recur_fibo(n-1) + recur_fibo(n-2))
|
||||||
|
|
||||||
|
def isPositiveInteger(limit):
|
||||||
|
return limit >= 0
|
||||||
|
|
||||||
|
def main():
|
||||||
|
limit = int(input("How many terms to include in fibonacci series: "))
|
||||||
|
if isPositiveInteger(limit):
|
||||||
|
print(f"The first {limit} terms of the fibonacci series are as follows:")
|
||||||
|
print([recur_fibo(n) for n in range(limit)])
|
||||||
|
else:
|
||||||
|
print("Please enter a positive integer: ")
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
15
Maths/GreaterCommonDivisor.py
Normal file
15
Maths/GreaterCommonDivisor.py
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
# Greater Common Divisor - https://en.wikipedia.org/wiki/Greatest_common_divisor
|
||||||
|
def gcd(a, b):
|
||||||
|
return b if a == 0 else gcd(b % a, a)
|
||||||
|
|
||||||
|
def main():
|
||||||
|
try:
|
||||||
|
nums = input("Enter two Integers separated by comma (,): ").split(',')
|
||||||
|
num1 = int(nums[0]); num2 = int(nums[1])
|
||||||
|
except (IndexError, UnboundLocalError, ValueError):
|
||||||
|
print("Wrong Input")
|
||||||
|
print(f"gcd({num1}, {num2}) = {gcd(num1, num2)}")
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
|
@ -1,16 +0,0 @@
|
|||||||
# Fibonacci Sequence Using Recursion
|
|
||||||
|
|
||||||
def recur_fibo(n):
|
|
||||||
if n <= 1:
|
|
||||||
return n
|
|
||||||
else:
|
|
||||||
return(recur_fibo(n-1) + recur_fibo(n-2))
|
|
||||||
|
|
||||||
limit = int(input("How many terms to include in fibonacci series: "))
|
|
||||||
|
|
||||||
if limit <= 0:
|
|
||||||
print("Please enter a positive integer: ")
|
|
||||||
else:
|
|
||||||
print(f"The first {limit} terms of the fibonacci series are as follows")
|
|
||||||
for i in range(limit):
|
|
||||||
print(recur_fibo(i))
|
|
12
Maths/gcd.py
12
Maths/gcd.py
@ -1,12 +0,0 @@
|
|||||||
def gcd(a, b):
|
|
||||||
if a == 0 :
|
|
||||||
return b
|
|
||||||
|
|
||||||
return gcd(b%a, a)
|
|
||||||
|
|
||||||
def main():
|
|
||||||
print(gcd(3, 6))
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
main()
|
|
Loading…
Reference in New Issue
Block a user