d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Simple Python Help > Fibonnaci Sequence Loop
Add Reply New Topic New Poll
Member
Posts: 31,702
Joined: Mar 21 2007
Gold: 4.00
Sep 23 2013 06:17pm
So I'm absolutely and utterly lost here. A billion google searches has not helped me so far.
I have never coded, or done anything with computers that required even slightly understanding codes, so forgive me if this is too damn easy.

I have the following code, which allows me to pick individual numbered terms from the Fibonacci sequence:
def Fib(n):
if n ==0:
return 0
elif n ==1:
return 1
else:
return Fib(n-1)+Fib(n-2)

How would I make this print out all values from the 1st to the 20th term of the Fibonacci sequence?
This has just been a huge headache for hours so far, and my text/the internet have not been helping.
What I want the output to look like is something along the lines of
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89...(20th term)

Does anybody here know Python? Help please!
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 23 2013 06:24pm
just add a print statement before you return it. i recommend you track which ones are already calculated for performance and also so you can track which ones you printed (unless you wanna print them multiple times)
Member
Posts: 31,702
Joined: Mar 21 2007
Gold: 4.00
Sep 23 2013 06:27pm
Quote (carteblanche @ Sep 23 2013 07:24pm)
just add a print statement before you return it. i recommend you track which ones are already calculated for performance and also so you can track which ones you printed (unless you wanna print them multiple times)


I am still absolutely lost. I can easily say print Fib(5) and get the fifth term of the fibonacci sequence, but I have no idea what kind of statement to put to make it print out all of the first twenty terms.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 23 2013 06:37pm
Quote (ringo794 @ Sep 23 2013 08:27pm)
I am still absolutely lost. I can easily say print Fib(5) and get the fifth term of the fibonacci sequence, but I have no idea what kind of statement to put to make it print out all of the first twenty terms.


dont print the function, print inside the function

Code

def Fib(n):
int returnvalue = 0
if n ==0:
returnvalue = 0
elif n ==1:
returnvalue = 1
else:
returnvalue = Fib(n-1)+Fib(n-2)

print returnvalue
return returnvalue


keep in mind you won't see each one just once with this method. you'll see it multiple times. to fix this, you need to calculate each fib(n) only once, then print it and cache it. every time you use it without calculating it (eg: pull from cache), you will not print it.
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
Sep 23 2013 10:25pm
Code
def FibSeq(n):
fib_seq=[]
fib_seq.append(0)
fib_seq.append(1)
for i in range(2,n):
fib_seq.append(fib_seq[i-1] + fib_seq[i-2])
return fib_seq

print FibSeq(15)


of course you should check to ensure input is integer greater than 1

This post was edited by Azrad on Sep 23 2013 10:27pm
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
Sep 24 2013 01:15am
Code
def FibSeq(n):
fib_seq=[0,1]
for i in range(2,n):
fib_seq.append(fib_seq[i-1] + fib_seq[i-2])
return fib_seq
print FibSeq(15)


There you go, even smaller :P
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll