Jasmine wrote:
If I wanted unlimited integer sizes, I would represent numbers with an array of small integers.
If you imagine each element in the array is to hold one digit of a huge number. That array could be 1 million elements wide, which is representing a number 1 million digits long.
You can then write small algorithms to add numbers like this together. You'd have to consider place value, and carry over.
Three arrays would suffice to build up your fibonacci sequence.
Arrays A and B would both represent 1 initially.
Then add A and B, write to C.
Then add B and C, write to A.
Then add C and A, write to B.
Then add A and B, write to C.
Then add B and C, write to A.
Then add C and A, write to B.
and so on.
I only took a semester of programming in Pascal for my senior year I high school, and we didn't learn anything about arrays. I'll have to read about it or something, but from what it sounds like you can define this 'array' thing to hold an arbitrary amount of digits. Are arrays what computers use to deal with insanely large numbers?
The last part of your post is exactly how I wrote the code to find the Fibonacci numbers, except A and B are long integers, not these array thing-a-ma-jigs (except I just printed the first two terms for some reason).
Code:
program Fibonacci_Term_Generator;
{$APPTYPE CONSOLE}
{Daniel Young
08/10/2009
Fibonacci number generator
}
uses
SysUtils;
var
i, n: integer;
Phi : real;
Term1, Term2, Term3 : longint;
begin
writeln('How many Fibonacci terms would you like to print?':65);
readln(n);
writeln;
writeln;
writeln;
Term1 := 1;
Term2 := 1;
Term3 := 0;
i := 1;
write(Term1, ',', ' ', Term2, ',', ' ');
while (i <> (n + 1)) do
begin
Term3 := Term2 + Term1;
write(Term3, ',', ' ');
Term1 := Term3 + Term2;
write(Term1, ',', ' ');
Term2 := Term1 + Term3;
write(Term2, ',', ' ');
i := i + 1;
end;
Phi := Term2/Term1;
writeln;
writeln;
writeln('Phi is approximately: ', Phi:0:20);
readln;
end.