This problem lends itself very well to using an array and a for loop.
KONjbnj
Posted: Thu Apr 14, 2005 8:38 am Post subject: (No 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?
Martin
Posted: Thu Apr 14, 2005 9:10 am Post subject: (No 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.
lordofall
Posted: Thu Apr 14, 2005 9:36 am Post subject: (No 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
Drakain Zeil
Posted: Thu Apr 14, 2005 9:19 pm Post subject: (No subject)
I'd tell you if I could remember what they were... I haven't touched them in oh... over a year.
chrispaks
Posted: Fri Apr 15, 2005 3:03 pm Post subject: (No subject)
could you not use a mod or rem command,
Quote:
if x mod y = 0
then put
?
illu45
Posted: Sun Apr 17, 2005 6:31 pm Post subject: (No 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
Sponsor Sponsor
StarGateSG-1
Posted: Mon Apr 18, 2005 11:24 am Post subject: (No 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
Martin
Posted: Mon Apr 18, 2005 12:30 pm Post subject: (No 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).
StarGateSG-1
Posted: Mon Apr 18, 2005 2:50 pm Post subject: (No 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!
Martin
Posted: Mon Apr 18, 2005 3:06 pm Post subject: (No subject)
F(0) is considered to be equal to 0 simply due to convention. In any case, F(1) = 1.
StarGateSG-1
Posted: Tue Apr 19, 2005 11:13 am Post subject: (No 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.
c0bra54
Posted: Tue Apr 19, 2005 11:49 pm Post subject: (No 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 ..
but yeh we had to do this also
StarGateSG-1
Posted: Wed Apr 20, 2005 7:26 am Post subject: (No 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!