Python Challenge - Fibonacci Finder

def fib_finder(n): # Write your code here temparr = [0,1] run = 1 answer = 0 while run < n-1: answer = temparr[-2] + temparr[-1] temparr.append(answer) run += 1 print(temparr) return temparr[n-1] print(fib_finder(6))
def fib_finder(n): if n == 1: return 0 elif n == 2: return 1 else: return fib_finder(n-1) + fib_finder(n-2)

The recursive method is my favorite way to do this.

def fib_finder(n): # Write your code here fib_nums = [0,1] for i in range(n-2): fib_nums.append(fib_nums[-1]+fib_nums[-2]) return fib_nums[n-1] print(fib_finder(6))
def fib_finder(EZ): return fibunachi(EZ-1) def fibunachi(EZ): if EZ <= 1: return EZ return (fibunachi(EZ-1) + fibunachi(EZ-2)) print(fib_finder(5))

recursion is overpowered B)
also the function that actually does stuff returns what the number is supposed to be +1 so the fib finder function fixes that

basically what that code means is that he is assigning two variables at once, which is useful if you want to swap variables

def fib_finder(n):
  x = [0,1]
  # Write your code here
  for i in range(n):
    if n > 0:
      if n == 1:
        return x[0]
      elif n == 2:
        return x[1] 
      else:
        for fib in range(2,n):
          prev_1 = x[fib-1]
          prev_2 = x[fib-2]
          next = prev_1 + prev_2
          x.append(next)
          print(x)
        return x[n-1]
print(fib_finder(5))
memo = {} def fib_finder(n): # Write your code here if n in memo.keys(): return memo[n] else: # base case if n <= 2: return n - 1 answer = fib_finder(n-1) + fib_finder(n-2) memo[n] = answer return answer print(fib_finder(6))
def fib_finder(n): fib_list = [0, 1] a = 0 b = 1 c = 0 for i in range(2, n): c = a + b a = b b = c fib_list.append(c) return fib_list[n - 1]

I used a deque for this.

from collections import deque

def fib_finder(n):
  if (n <= 1): 
    return 0
  elif (n == 2):
    return 1
  que = deque([0,1])
  for x in range(3, n + 1):
    current = sum(que)
    que.popleft()
    que.append(current)
  return que[-1]

I started with a list containing the first two elements in the sequence and then filled out the sequence up to length n, finally printing the last entry in the built sequence. Here’s what that looks like:

def fib_finder(n): seq = [0,1] if n == 1: return 0 while len(seq) < n: seq.append(seq[-1] + seq[-2]) return seq[-1] print(fib_finder(50))

def fib_finder(number):

Write your code here

store1 = 0
store2 = 1

x = number//2

if number%2 == 0:
for i in range(x-1):
store1 += store2
store2 += store1
print(store1,store2)
return(store2)

else:
for i in range(x):
store1 += store2
store2 += store1
return(store1)

print(fib_finder(44))