Skip to content

Commit dc98872

Browse files
Create Coding assignment 8.py
1 parent a969264 commit dc98872

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Coding assignment 8.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'''
2+
Check Fibonacci Number
3+
Given a number N, figure out if it is a member of fibonacci series or not. Return true if the number is member of fibonacci series else false.
4+
Fibonacci Series is defined by the recurrence : F(n) = F(n-1) + F(n-2) where F(0) = 0 and F(1) = 1, F(3) = 1+1=2, F(4) = 1+2=3, F(5) = 2+3=5, ....
5+
Constraints :
6+
0 <= n <= 10^4
7+
Sample Input 1 : 5
8+
Sample Output 1 : true
9+
Sample Input 2 : 14
10+
Sample Output 2 : false
11+
'''
12+
13+
import math
14+
def is_perfect_square(x):
15+
s=int(math.sqrt(x))
16+
return s*s==x
17+
def is_fibonacci(n):
18+
return is_perfect_square(5*n*n+4) or is_perfect_square(5*n*n-4)
19+
n=int(input())
20+
if is_fibonacci(n):
21+
print("true")
22+
else:
23+
print("false")
24+
25+
'''
26+
A number `n` is considered a Fibonacci number if and only if one (or both) of the following conditions is true:
27+
28+
1. `5 * n^2 + 4` is a perfect square, or
29+
2. `5 * n^2 - 4` is a perfect square.
30+
'''
31+

0 commit comments

Comments
 (0)