A Fibonacci number is a member of the Fibonacci sequence, a series of numbers where each number is the sum of the two preceding ones, usually starting with 0 and 1. The sequence begins:
Starting with 0 and 1, each subsequent Fibonacci number is obtained by adding the two numbers that precede it. Mathematically, it can be defined by the recurrence relation:
with initial conditions and .
The Fibonacci sequence has many interesting mathematical properties and applications, and it often appears in various areas of mathematics and nature, including the arrangement of leaves on a stem, the branching of trees, and the arrangement of seeds in a sunflower.
Program :
is_perfect_square(n)
Function:- This function is a helper function that takes a number
n
as input and checks if it is a perfect square. - It calculates the square root of
n
using the exponentiation (**
) operator. - It then checks if the square of the calculated root is equal to the original number
n
. If it is, then the number is a perfect square.
- This function is a helper function that takes a number
is_fibonacci_number(number)
Function:- This function takes a number
number
as input and determines whether it belongs to the Fibonacci sequence. - It checks if either
(5 * number^2 + 4)
or(5 * number^2 - 4)
is a perfect square using theis_perfect_square
function. - If either of these expressions results in a perfect square, the input number is considered part of the Fibonacci sequence.
- This function takes a number
User Input:
- The program prompts the user to enter a number using
input()
. - The entered number is converted to an integer using
int()
.
- The program prompts the user to enter a number using
Checking and Output:
- The program then calls the
is_fibonacci_number
function with the user-entered number. - If the number is determined to be a Fibonacci number, the program prints that the number is a Fibonacci number. Otherwise, it prints that the number is not a Fibonacci number.
- The program then calls the
For example, if the user enters 5
, the program will output that 5
is a Fibonacci number, because 5
is part of the Fibonacci sequence (0, 1, 1, 2, 3, 5, ...
).
No comments:
Post a Comment