-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfind_fibonacci.py
75 lines (71 loc) · 1.27 KB
/
find_fibonacci.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# -----------------------------------------------
# Author : Mohit Chaudhari
# Created Date : 11/07/21
# -----------------------------------------------
# ***** Find Fibonacci *****
# Problem Description
#
# The Fibonacci numbers are the numbers in the following integer sequence.
#
# 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ……..
#
# In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation:
#
# Fn = Fn-1 + Fn-2
#
# Given a number A, find and return the Ath Fibonacci Number.
#
# Given that F0 = 0 and F1 = 1.
#
#
#
# Problem Constraints
# 0 <= A <= 20
#
#
#
# Input Format
# First and only argument is an integer A.
#
#
#
# Output Format
# Return an integer denoting the Ath term of the sequence.
#
#
#
# Example Input
# Input 1:
#
# A = 2
# Input 2:
#
# A = 9
#
#
# Example Output
# Output 1:
#
# 1
# Output 2:
#
# 34
#
#
# Example Explanation
# Explanation 1:
#
# f(2) = f(1) + f(0) = 1
# Explanation 2:
#
# f(9) = f(8) + f(7) = 21 + 13 = 34
class Solution:
# @param A : integer
# @return an integer
def findAthFibonacci(self, A):
if A == 1 or A == 2:
return 1
return self.findAthFibonacci(A-2) + self.findAthFibonacci(A-1)
s = Solution()
print(s.findAthFibonacci(9))
# OUTPUT: 34