recursive.py
python_old/recursive.py
# Recursive function to calculate Fibonacci number
# F(0) = 0, F(1) = 1
# F(n) = F(n - 1) + F(n - 2)
# Given n, find F(n)
# What is recursion?
# Recursion is a process in which a function calls itself as a subroutine.
# This allows the function to be repeated several times, as it can call itself during its execution.
def fibonacci_recursive(n):
if n <= 0:
return 0
elif n == 1:
return 1
else:
return fibonacci_recursive(n - 2) + fibonacci_recursive(n - 1)
if __name__ == "__main__":
# Ask user for number of terms
num_terms = 40
for i in range(num_terms):
print(fibonacci_recursive(i), end=" ")
Articoli correlati
01_classical_caesar_cipher.py
01_classical_caesar_cipher.py — python source code from the python old learning materials (python_old/Cryptography/01_classical_caesar_cipher.py).
Leggi l'articolo →02_classical_monoalphabetic.py
02_classical_monoalphabetic.py — python source code from the python old learning materials (python_old/Cryptography/02_classical_monoalphabetic.py).
Leggi l'articolo →03_classical_rail_fence.py
03_classical_rail_fence.py — python source code from the python old learning materials (python_old/Cryptography/03_classical_rail_fence.py).
Leggi l'articolo →04_frequency_analysis.py
04_frequency_analysis.py — python source code from the python old learning materials (python_old/Cryptography/04_frequency_analysis.py).
Leggi l'articolo →05_cryptographic_hashing.py
05_cryptographic_hashing.py — python source code from the python old learning materials (python_old/Cryptography/05_cryptographic_hashing.py).
Leggi l'articolo →06_password_hashing.py
06_password_hashing.py — python source code from the python old learning materials (python_old/Cryptography/06_password_hashing.py).
Leggi l'articolo →