Skip to main content

Fibonacci series in Python

 Fibonacci series in Python


The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1. The series is named after the Italian mathematician Leonardo Fibonacci, who introduced it to the Western world in his 1202 book "Liber Abaci."

The Fibonacci sequence has many interesting properties and appears in various areas of mathematics and science. It is often used as a simple example to introduce the concept of recursion (where a function calls itself).

Here is the first few terms of the Fibonacci series:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...

As you can see, each term is the sum of the previous two terms. For example, the third term (1) is the sum of the first two terms (0 and 1), the fourth term (2) is the sum of the second and third terms (1 and 1), and so on.

The Fibonacci series has many interesting properties and appears in various areas of mathematics and science. It is often used as a simple example to introduce the concept of recursion (where a function calls itself).

Sure! Here is a simple implementation of the Fibonacci series in Python: Program:

def fibonacci(n): if n < 0: print("Incorrect input") elif n == 1: return 0 elif n == 2: return 1 else: return fibonacci(n-1) + fibonacci(n-2) # Test the function print(fibonacci(9)) # 34

This implementation uses recursion to calculate the n-th term of the Fibonacci series. The function calls itself with the values n-1 and n-2 until it reaches the base case (n = 1 or n = 2), at which point it returns the value of the corresponding term in the series. This function will output the ninth term of the Fibonacci series (which is 34). You can easily modify it to print out the first n terms of the series by using a loop.

Comments

Popular posts from this blog

Armstrong number in Python

Armstrong number An Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits. For example, 153 is an Armstrong number because: 1^3 + 5^3 + 3^3 = 153 Other Armstrong numbers include 371, 407, and 1634. To check if a number is an Armstrong number, you can use the following algorithm: Calculate the number of digits in the number. For each digit in the number, raise it to the power of the number of digits and add it to a running total. If the running total is equal to the original number, the number is an Armstrong number. For example, to check if 371 is an Armstrong number: The number of digits in 371 is 3. 3^3 + 7^3 + 1^3 = 27 + 343 + 1 = 371. The running total is equal to the original number, so 371 is an Armstrong number. I hope this helps! Let me know if you have any other questions. Here is some sample Python code that checks if a number is an Armstrong number: Program: def is_armstrong_number(num): # convert the number to a st...