-
Notifications
You must be signed in to change notification settings - Fork 0
/
fibonacci_sequence.py
36 lines (34 loc) · 997 Bytes
/
fibonacci_sequence.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
# Hi, here's your problem today. This problem was recently asked by Apple:
#
# The Fibonacci sequence is the integer sequence defined by the recurrence relation: F(n) = F(n-1) + F(n-2), where F(0) = 0 and F(1) = 1. In other words, the nth Fibonacci number is the sum of the prior two Fibonacci numbers. Below are the first few values of the sequence:
#
# 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144...
#
# Given a number n, print the n-th Fibonacci Number.
# Examples:
# Input: n = 3
# Output: 2
#
# Input: n = 7
# Output: 13
# Here's a starting point:
#
# class Solution():
# def fibonacci(self, n):
# # fill this in.
#
# n = 9
# print(Solution().fibonacci(n))
# 34
class solution():
def fibonacci(self, n):
signature = [0, 1]
signature = signature[:n]
for i in range(n-1):
signature.append(sum(signature[-2:]))
if len(signature) != 0:
return signature[-1]
else:
return
n = 3
print(solution().fibonacci(n))