From d55f566a0053451175fe40b23175338a4cc9af9c Mon Sep 17 00:00:00 2001 From: abhijit Date: Sun, 30 May 2021 03:04:53 +0530 Subject: [PATCH] chore(Python): add fibonacci algorithms (#327) --- algorithms/Python/README.md | 6 +++ .../dynamic_programming/fibonacci_series.py | 43 +++++++++++++++++++ .../fibonacci_series_nth_term.py | 34 +++++++++++++++ .../fibonacci_series_sum.py | 42 ++++++++++++++++++ 4 files changed, 125 insertions(+) create mode 100644 algorithms/Python/dynamic_programming/fibonacci_series.py create mode 100644 algorithms/Python/dynamic_programming/fibonacci_series_nth_term.py create mode 100644 algorithms/Python/dynamic_programming/fibonacci_series_sum.py diff --git a/algorithms/Python/README.md b/algorithms/Python/README.md index 42bc1502..a2d4dd27 100644 --- a/algorithms/Python/README.md +++ b/algorithms/Python/README.md @@ -37,3 +37,9 @@ 4. [Remove Duplicates from a String](strings/remove_duplicates_from_a_string.py) 5. [First Non Repeating Character](strings/first_non_repeating_character.py) 6. [Longest Common Subsequence](strings/longest_common_subsequence.py) + +## Dynamic Programming +1. [Print Fibonacci Series Up To N-th Term](dynamic_programming/fibonacci_series.py) +2. [Sum Up To N-th Term Of Fibonacci Series](dynamic_programming/fibonacci_series_sum.py ) +3. [N-th Term Of Fibonacci Series](dynamic_programming/fibonacci_series_nth_term.py) + diff --git a/algorithms/Python/dynamic_programming/fibonacci_series.py b/algorithms/Python/dynamic_programming/fibonacci_series.py new file mode 100644 index 00000000..380edc38 --- /dev/null +++ b/algorithms/Python/dynamic_programming/fibonacci_series.py @@ -0,0 +1,43 @@ +# Print the fibonacci series up to term n using dynamic approach +# Fibonacci series starts from 0th term + +""" +Output: + +The fibonacci series up to term 20: 0 1 1 2 3 5 8 13 21 34 55 + 89 144 233 377 610 987 1597 2584 4181 6765 + +""" + +key = 20 + +if key < 0: + print("Please enter a valid term.") + exit() + +d = {0: 0, 1: 1} + +print(f"The fibonacci series up to term {key}: ", end=" ") + +if key == 0: + print(0) + exit() +if key == 1: + print(0, "\t", 1) + exit() + +print(0, "\t", 1, sep="", end="") + + +def fibo(n): + if n in d.keys(): + return d[n] + else: + d[n] = fibo(n - 1) + fibo(n - 2) + print("\t", d[n], end=" ") + return d[n] + + +fibo(key) + +print() \ No newline at end of file diff --git a/algorithms/Python/dynamic_programming/fibonacci_series_nth_term.py b/algorithms/Python/dynamic_programming/fibonacci_series_nth_term.py new file mode 100644 index 00000000..c3676a39 --- /dev/null +++ b/algorithms/Python/dynamic_programming/fibonacci_series_nth_term.py @@ -0,0 +1,34 @@ +# Find the nth term of fibonacci series using dynamic approach +# Fibonacci series starts from 0th term + +""" +Output: + +The term 10 of the fibonacci series is: 55 + +""" + +key = 10 + +if key < 0: + print("Please enter a valid term.") + exit() + +d = {0: 0, 1: 1} + +if key < 2: + print(f"The term {key} of the fibonacci series is: ", d[key]) + exit() + + +def fibo(n): + if n in d.keys(): + return d[n] + else: + d[n] = fibo(n - 1) + fibo(n - 2) + return d[n] + + +fibo(key) + +print(f"The term {key} of the fibonacci series is: ", d[key]) \ No newline at end of file diff --git a/algorithms/Python/dynamic_programming/fibonacci_series_sum.py b/algorithms/Python/dynamic_programming/fibonacci_series_sum.py new file mode 100644 index 00000000..7775c9be --- /dev/null +++ b/algorithms/Python/dynamic_programming/fibonacci_series_sum.py @@ -0,0 +1,42 @@ +# Find the sum up to nth term of fibonacci series using dynamic approach +# Fibonacci series starts from 0th term + +""" +Output: + +Sum up to term 10 of fibonacci series is: 143 + +""" + +key = 10 + +if key < 0: + print("Please enter a valid term.") + exit() + +d = {0: 0, 1: 1} + +if key == 0: + print(f"Sum up to term {key} of fibonacci series: ", 0) + exit() + +if key == 1: + print(f"Sum up to term {key} of fibonacci series: ", 1) + exit() + +sum = 1 + + +def fibo(n): + if n in d.keys(): + return d[n] + else: + d[n] = fibo(n - 1) + fibo(n - 2) + global sum + sum += d[n] + return d[n] + + +fibo(key) + +print(f"Sum up to term {key} of fibonacci series is: ", sum) \ No newline at end of file