# Python Challenge - Fibonacci Finder

My code seems OK but i have 2/5 tests passed.

def fib_finder(n): # Write your code here # We initiate our Fibonacci list with 0 and 1. fib_list = [0, 1] # We iterate to add an element until the nth number in the if n < 0 : return "{} isn't right! You should try with a positive integer.".format(n) elif (n == 0) or (n == 1) : return 0 elif n == 2: return 1 else: while len(fib_list) <= n: new_fib_item = fib_list[-1] + fib_list[-2] fib_list.append(new_fib_item) #return the nth number which is the last one. return fib_list[-1] print(fib_finder(5))

Lists start with an index of 0, but there’s no 0th Fibonnacci number for this excercise,
so if you’re using the nth index for the nth Fibonnaci number,
your initial list should have an extra element at index 0 to account for that;
like this

``````  fib_list = [0, 0, 1]
``````

The problem is easy to solve by using recursion:

def fib_finder(n): if n == 1: return 0 if n == 2: return 1 return (fib_finder(n-2) + fib_finder(n-1)) print(fib_finder(5))

[codebyte language=python]
def fib_finder(n):

f = [0,1]
if n <= 0:
return “Invalid placement request”
elif n == 1:
return 0
elif n >= 1:

``````for i in range(1, n-1):
f.append(f[i-1] + f[i])
#print(f)
``````

return f[len(f) - 1]

fib_finder(0)

print(fib_finder(2))[/codebyte]

here is my solution

``````def fib_finder(n):

fib = [0,1]
for x in range (1,n):
fib.append(fib[-1]+fib[-2])
return fib[n-1]
print(fib_finder(5))
``````

Hi all,

Here is my solution. It works, but the limitation is if someone wants to find the 1,001th number in the sequence. Here it is:

`````` # Write your code here:
fib_seq = [0,1]
def create_seq(num):
end = num - 1
for x in range(end):
third = fib_seq[x] + fib_seq[x+1]
fib_seq.append(third)

def fib_finder(number):
return(fib_seq[number-1])

create_seq(1000)

print(fib_finder(1))
print(fib_finder(2))
print(fib_finder(6))

``````

Any critique is welcomed!

def fib_finder(n): new_list = [] for num in range(1, n+1, 1): new_list.append(num) fib_list = [0, 1] for i in range(len(new_list)-2): fib_list.append(fib_list[i] + fib_list[i+1]) print(f'The Fibonacci list of number {n} is: {fib_list}\nThe {n}th number of this sequence is: {fib_list[n-1]}') return fib_list[n-1] fib_finder(6)
def fib_finder(n): # Write your code here fib = [0,1] if n == 1: return 0 else: while len(fib) < n: fib.append(fib[-2] + fib[-1]) desired_number = fib[-1] return desired_number try: print(fib_finder(1)) print(fib_finder(2)) print(fib_finder(6)) except: print("You must choose a positive integer.")

For some reason I did not trust myself at the beginning of this challenge, though it was so easy to accomplish in the end!

``````def fib_finder(n):
fib_sequence = [0, 0, 1]
for a in range(3, n+1):
fib_sequence.append(fib_sequence[a-2]+fib_sequence[a-1])
return fib_sequence[n]
``````

Had to do some workarounds due to the zero-indexing system in python
In my opinion, the most intuitive way to do this.

Zero plus zero is not one. That initialization is not intuitive. Consider starting the sequence like so,

``````fib_sequence  = [0, 1]
``````

``````fib_sequence = [1, 1]
``````

The the nth term of the sequence in the latter setup is offset from the former by -1.

Just for fits and giggles, let’s consider a version that only returns the nth term of either setup:

``````>>> def nth_fib(n, a=False):
...     a, b, n, m = int(a), 1, n - a, int(not a)
...     while n > m:
...         b, a, n = a + b, b, n - 1
...     return a
...
>>> nth_fib(5, True)  #  t(1) is 1
5                     #  1, 1, 2, 3, 5
>>> nth_fib(5)        #  t(1) is 0
3                     #  0, 1, 1, 2, 3
>>>
``````

Maybe not exactly intuitive because it takes some sussing out of the logic.

It just keeps chugging away until a conclusion is met, and holds the most recent value as a return, with no need for a data structure that could take up a whack of memory just to obtain the last value appended. Why store any of the predecessor values? Well, we have the whole sequence, in print; but, on consideration, if we want to print we could do it with the above algorithm and still not use a data structure (though the OS might have a say in how this happens; anything to the display is a huge disruption); and, do it repeatedly.

def fib_finder(n): # Write your code here a = 0 b = 1 lst = [0, 1] for i in range(n): if n > 2: suma = a + b lst.append(suma) a = b b = suma return lst[n-1] print(fib_finder(5))