Page 1 of 1

Turing Help

Posted: Thu Feb 16, 2006 11:05 pm
by Anonymous
I'm trying to code out a simple program to display the first 20 numbers of Fibonacci's Number (1,1,2,3,5,8,13... *1+1 =2.. 2+1=3.. 3+2=5.. 5+3=8.. etc...*) Can anyone tell me why it isn't adding properly> I can get the program to display 1,1,2,3,5,8.. but then the rest isn't working properly. ^^;; This is what I have so far:

Code: Select all

var number : int := 1
var sum : int 
var sum2 : int


sum := number + number
sum2 := number + sum

put number,", ",number, ", " ..
put sum, ", " ..
put sum2, ", " ..

for i : 1 .. 20
        number := sum + sum2
        sum2 := sum2 + sum2
     
       

    put number , ", " ..
   end for[code] 

[color=#888888][size=85]Archived topic from Iceteks,  old topic ID:4184, old post ID:34055[/size][/color]

Turing Help

Posted: Fri Feb 17, 2006 1:37 am
by Death
what exactly isn't working after? Are the numbers not displaying? Are they higher than they are supposed to be?

Archived topic from Iceteks, old topic ID:4184, old post ID:34059

Turing Help

Posted: Fri Feb 17, 2006 7:37 am
by Anonymous
everything is displaying properly but the out come isn't right.

I'm trying to get the numbers to display as:
1, 1, 2, 3, 5, 8, 13, 21, 34, etc..

but it is appearing as :
1, 1, 2, 3, 5, 9, 17... blah blah blah.

The adding of the variables is working correctly.

Archived topic from Iceteks, old topic ID:4184, old post ID:34061

Turing Help

Posted: Fri Feb 17, 2006 12:28 pm
by Death
number := sum + sum2
sum2 := sum2 + sum2

That's where the problem lies. I would use this type of formula:

sum1 := sum1 + sum2
sum2 := sum1 + sum2
print sum1, ", ", sum2

Say you start off with both value's being 1.

The first loop through would give you 2, 3
The second loop through would give you 5, 8
The third loop through would give you 13, 21

(at least to my knowledge, this is all done in my head at the moment)
Try using that formula in place of what you are already using. Hope this helps :)

Archived topic from Iceteks, old topic ID:4184, old post ID:34062

Turing Help

Posted: Sun Mar 05, 2006 2:31 pm
by Anonymous
Thanks ^^. That worked ;D

Archived topic from Iceteks, old topic ID:4184, old post ID:34247