Turing Help

Computer builds, hardware and software discussion or troubleshooting, including peripherals. Essentially a general place to talk about desktop computers.
Locked
Anonymous

Turing Help

Post 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]
User avatar
Death
Posts: 7919
Joined: Thu Sep 30, 2004 10:12 pm

Turing Help

Post 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
Anonymous

Turing Help

Post 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
User avatar
Death
Posts: 7919
Joined: Thu Sep 30, 2004 10:12 pm

Turing Help

Post 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
Anonymous

Turing Help

Post by Anonymous »

Thanks ^^. That worked ;D

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