Computer Science Canada

Fibonacci help

Author:  KONjbnj [ Thu Apr 14, 2005 8:28 am ]
Post subject:  Fibonacci help

Write a program which calculates the Fibonacci series. Your program should print out the first 25 fibonacci numbers.

That's what I'm supposed to do, but I can't find a way to do it with variables instead of using put statements.

Author:  Martin [ Thu Apr 14, 2005 8:33 am ]
Post subject: 

Well, the first two numbers of the Fibonacci sequence are 1 and 1

The n'th term of the Fibonacci sequence is simply the sum of the previous two terms. That is,

F(1) = 1
F(2) = 1
F(3) = F(1) + F(2) = 2
F(4) = F(2) + F(3) = 3
...
F(N) = F(N-2) + F(N-1)

This problem lends itself very well to using an array and a for loop.

Author:  KONjbnj [ Thu Apr 14, 2005 8:38 am ]
Post subject: 

I know that for Fibonacci you add the last two numbers to get it.

Sorry, but I don't get what you said >_<.

Here's what I tried.

[quote]
var f1 : int := 0
var f2 : int := 1
var f3 : int
var f4 : int

var total : int := 0

for x: 1..25
f3 := f1 + f2
put f3
f4 := f3 + f2
put f4


end for[/quote]

I've tried to do that, but then I realized that I would have to retype that to get every number. Is there anyway so that I only need one or two equations and it will do the rest?

Author:  Martin [ Thu Apr 14, 2005 9:10 am ]
Post subject: 

You've got the right idea. However, you should use an array.

instead of:
var F1, F2, F3, ... , F25 : int

use

var F : array 1 .. 25 of int

These are essentially the same thing.

Now, instead of calling F1 when you want to get the first one, you call F(1). Instead of F2, it's F(2), and so on. For more info, type array and press F10 and search the forums for an arrays tutorial.

Author:  lordofall [ Thu Apr 14, 2005 9:36 am ]
Post subject: 

Seems more like a for loop

code:

var a, b, c : int := 1

put a, " ", b, " " ..
for i : 3 .. 25
    c := a + b
    a := b
    b := c
    put c, " " ..
end for

Author:  Drakain Zeil [ Thu Apr 14, 2005 9:19 pm ]
Post subject: 

I'd tell you if I could remember what they were... I haven't touched them in oh... over a year.

Author:  chrispaks [ Fri Apr 15, 2005 3:03 pm ]
Post subject: 

could you not use a mod or rem command,

Quote:

if x mod y = 0
then put

?

Author:  illu45 [ Sun Apr 17, 2005 6:31 pm ]
Post subject: 

Here's what I got:

code:

var first, prev, last : int
first := 1
prev := 1
put first, prev


for i : 1 .. 25
    last := prev + first
    put last
    first := prev
    prev := last   
end for

Author:  StarGateSG-1 [ Mon Apr 18, 2005 11:24 am ]
Post subject: 

I feel kinda bad about doing your work but this is a rather hard on to do rigfht without cheats. I know that you will use this properly and will not hand this in as your own.

code:

% Chris Harshman
% Fibonacci series
% Outputs the first 25 number of the Fibonacci series
var Fib : array 1..25 of int
Fib (1) := 0
Fib (2) := 1
put Fib (1)," "..
put Fib (2)," "..
for i : 3..25
Fib (i) := Fib (i-2) + Fib (i-1)
put Fib (i)," "..
end for

9 lines and 123 characters without comments
O ya by the way it does work for the numbers after 8 but your not supposed to use them, It is a series and asking for the first 25 is pointless, but any how there it is. The first number in the series is 0 not 1 and 1.
Here it is:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610... etc

Author:  Martin [ Mon Apr 18, 2005 12:30 pm ]
Post subject: 

Awesome. One thing though: the first two terms are 1, not 0 and 1. (although all that this means is that the sequence is just pushed back one term).

Author:  StarGateSG-1 [ Mon Apr 18, 2005 2:50 pm ]
Post subject: 

The first 2 terms are 0 and 1 go look it up on google!
You wouldn't be able to get the second 1 without a zero!

Author:  Martin [ Mon Apr 18, 2005 3:06 pm ]
Post subject: 

http://mathworld.wolfram.com/FibonacciNumber.html
http://en.wikipedia.org/wiki/Fibonacci_number

F(0) is considered to be equal to 0 simply due to convention. In any case, F(1) = 1.

Author:  StarGateSG-1 [ Tue Apr 19, 2005 11:13 am ]
Post subject: 

if you do it that way techinally yes but you couldn't have the sequence without zero, I went and did research on it, and the sequence is
0 1 are the two number you are given
0 + 1 = 1
0 1 1 now you have
1 + 1 = 2
0 1 1 now you have
etc... etc... etc..
that is how it works no ifs and s and buts.
you get 1 number really though since 0 isn't really considered one. you can get x+1 = 1 without zero.

Author:  c0bra54 [ Tue Apr 19, 2005 11:49 pm ]
Post subject: 

we had to do this for class before.. but it wasn't the first 25 terms.. it was like ten or sumthing, my code looks alot simpler then yours.. here it is!!



code:

var x1 : int := 1
var x2 : int := 1

for i :1..10
put x1:5..
put x2:5..
x1 := x2 +x1
x2 := x2 +x1
end for
 


it's a litte bit different then what you are doing, since it does not calculate one number, return it, then do another, it return two at the same time.. but yes.. it' works fairly well, and this oes the first 20 or so, since there are two put's and the loop counts to ten/...

enjoy, i knwo that this is a help forum, but it seems the answerws been posted alredy Razz..

but yeh we had to do this also

Author:  StarGateSG-1 [ Wed Apr 20, 2005 7:26 am ]
Post subject: 

That is one way to do it but, it really is infect nice job though, it works now you just need to make it nicer!

Author:  Martin [ Wed Apr 20, 2005 7:56 am ]
Post subject: 

code:
var prev1, prev2, term : int := 1%N -1 and N -2 respectively
var n : int := 25 %whatever term you want to find
for i : 3 .. n
     term = prev1 + prev2
     prev2 := prev1
     prev1 = term
     put "F(", i, ")", prev1, " " ..
end for

Author:  c0bra54 [ Wed Apr 20, 2005 8:02 pm ]
Post subject: 

well ty

Author:  TheEvilOne [ Thu Apr 21, 2005 9:02 am ]
Post subject: 

Heres one using functions and recursion, if you really want to impress Razz

code:

var FibNumber : int := 25
   
function Fib (x : int):int
if x = 1 or x = 0 then
    result x
else
    result Fib(x-1) + Fib(x-2)
end if
end Fib

put Fib(FibNumber)


NOTE: This one uses 1 1 2 3 5 8 13 etc, rather than 0 1 1 2 3 5 8 13, you can do changing if you want to, but I doubt youll use it

Author:  Martin [ Thu Apr 21, 2005 9:09 am ]
Post subject: 

Don't use recursion for the fibonacci sequence. Trace your program and you'll see why.

Author:  TheEvilOne [ Thu Apr 21, 2005 9:12 am ]
Post subject: 

yeah I know why its bad, because puts tons and tons on the stack Confused
its just fun to do it that way, the more inefficient it is, the more fun, ne?

Author:  c0bra54 [ Thu Apr 21, 2005 10:26 pm ]
Post subject: 

lol Rob, i thought we both agreed in class that recusion was useless for Fibonaci Razz... lol

and at the very least do it rich's way, his cut execution time in almos thalf lol Razz

but yeh i dunno if there is a better way to do the Fibonacci then the ons posted.. someone would need to Time.Elapsed it and i am to laz/need to finish eng Razz

well yes.. i hope you got the assignment done after all that


: