# Python Challenge - Fibonacci Finder

This community-built FAQ covers the “Fibonacci Finder” code challenge in Python. You can find that challenge here, or pick any challenge you like from our list.

## Top Discussions on the Python challenge Fibonacci Finder

There are currently no frequently asked questions or top answers associated with this challenge – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this challenge. Ask a question or post a solution by clicking reply () below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this challenge, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

## Join the Discussion. Help a fellow learner on their journey.

You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like () to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

def fib_finder(n):
x=0
y=0
list=[0,1]
if n==1 or n==2:
x=n-1
return x
if n > 2:

``````   for i in range(n-2):
y=list[-1]+list[-2]

list.append(y)
print(list)
``````

return y

print(fib_finder(3))

Space complexity, O(1); time complexity, O(N).

``````def fib_finder(n):
a = 0
b = 1
x = 1
while x < n:
a, b = b, a + b
x += 1
return a

print(fib_finder(6))    # 5
``````
1 Like

Did this code pass all the tests?

1 Like

This is my code, it may not be perfect but I really like how it is quite intuitive, the part that is kind of tricky is the range of the cycle for. I put the length but you can substitute it with a 2 because it just indicates that the function must not iterate through all the range of n, just n minus the number of values you have in the declaration of the list fibonacci. If you have any questions or have an idea of how I can improve my code please reply to my entry

def fib_finder(n): i = 0 fibonacci = [0,1] for i in range(n - len(fibonacci)): num1 = fibonacci[i] num2 = fibonacci[i + 1] fibonacci.append(num1 + num2) i += 1 return fibonacci[n-1] print(fib_finder(5))

I do not quite understand what means

a, b = b, a + b

Specifically the comas, but I really like your code. Very clean and efficient.

1 Like

Python permits sequence unpacking and assignment, which lends itself well to swapping, as it turns out.

``````a, b = b, a
``````

will swap the values between the two variables.

``````a = 6
b = 7

a, b = b, a

print (a, b)    //  (7, 6)
``````

Above, I wanted the code have minimal space complexity and hang on to only the most recent computed values. With each iteration of the while loop, the previous value for `b` is given to `a`, and the sum of both the old value of `a` and current value of `b` is given to `b`. We advance along that line until the loop expires. The value for `a` at that time will be the nth Fibonacci.

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

return fibs[n]

print(fib_finder(5))
print(fib_finder(2))
``````

def fib_finder(n):
a,b,c,i=0,1,1,1
if n==1:
print(‘Term’,n,‘of the Fibonacci sequence is’, str(a)+’.’)
return(a)
while i < (n-1):
c=a+b
a,b=b,c
i+=1
print(‘Term’,n,‘of the Fibonacci sequence is’, str(c)+’.’)
return(c)

fib_finder(1)
fib_finder(2)
fib_finder(6)
fib_finder(10)

def fib_finder(n): fib_start = [0, 1] ind1 = 0 ind2 = 1 if n <= 1: return 0 else: for n in range(1, n): fib_sum = fib_start[ind1] + fib_start[ind2] fib_start.append(fib_sum) ind1 += 1 ind2 += 1 return fib_start[n] print(fib_finder(5))

Not the cleanest code, but I’ve always had trouble processing the mathematical logic of the Fibonacci thingie…I wanted something easy to read. Hope I’ve achieved that…

My code…
I didn’t know about the tuple assignment thing. That said, I’m more familiar with JS, with it I’d use a destructuring assignment :

Js: ` [a, b] = [b, a + b]`

that is the same deal as:

Py: `a, b = b, a + b`

It’s nice to know there’s a similar solution in python. thanks @mtf !

ps.: codebytes are nice

def fib_finder(n): a = 0 b = x = 1 while x < n : temp_a = a a = b b = temp_a + b n -= 1 return a print(fib_finder(6))
1 Like
def fib_finder(n): # Write your code here if n == 1: return 0 if n == 2: return 1 pre_2, pre_1 = 0, 1 for i in range(3, n): pre_2, pre_1 = pre_1, pre_2 + pre_1 return pre_2 + pre_1 print(fib_finder(4))

Fibonnaci sequence:
def fibonnaci():
fibonnaci = [1, 1]
for i in range(850):
fibonnaci.append(fibonnaci[-2] + fibonnaci[-1])
print(fibonnaci)

fibonnaci()

``````def fib_finder(n):
l=[0,1]
f1=0
f2=1
for i in range(n):
f3=f1+f2
f1=f2
f2=f3
l.append(f3)
return(l[n-1])
print(fib_finder(6))
``````

Here is my code:

``````def fib_finder(n):
if n not in fib_dict:
fib_dict[n] = fib_finder(n - 1) + fib_finder(n - 2)
return fib_dict[n]

fib_dict = {1:0, 2:1}

print(fib_finder(5))
``````

I ilke this , I started on this path but went for a normal one ditching recursion as thinking too time consuming.

My solution, I love recursion and went for that as a default ( trying to write minimal lines) but it kinda broke with 35 as number thats when I realise the code is taking too much time. Went for a simpler ( code wise) solution for this.

``````def fib_finder(n):
x=0
a=0
b=1
c=0
if n==1 or n <= 0:
x=0
elif n==2:
x= 1
else:
for i in range(2,n):
c= a + b
a=b
b=c
x=b
return x
print(fib_finder(6))
``````

My code for this challenge:

``````def fib_finder(n):
fib_sequence = [0,1]
while len(fib_sequence) < n:
fib_sequence.append(fib_sequence[-2]+fib_sequence[-1])
return fib_sequence[n-1]

print(fib_finder(8))
``````
``````def fib_finder(n):
if n == 1:
return 0
elif n == 2:
return 1
else:
fib = [0, 1]
for i in range(n - 2):
fib.append(fib[-1] + fib[-2])
return(fib[-1])

print(fib_finder(6))
``````
def fib_finder(n): # Write your code here fib_string = [0, 1] for num in range(2, n + 1): new = fib_string[num - 2] + fib_string[num - 1] fib_string.append(new) return fib_string[n - 1] print(fib_finder(6))