Unstructured version:
program ex18;
var limit, i, f1, f2, nextfib : integer;
golden_ratio : real;
begin
f1 := 0; f2 := 1;
writeln('Enter the number of Fibonacci numbers you want to see');
readln(limit);
writeln ('Fibonacci numbers and Golden Ratio:');
for i := 1 to limit-1 do
begin
golden_ratio := f1 / f2;
writeln (f1, ' ', f2, ' ', golden_ratio:8:4);
nextfib := f1 + f2;
f1 := f2;
f2 := nextfib;
end;
readln;
end.
With Functions:
program Project1;
{$APPTYPE CONSOLE}
uses
SysUtils;
var limit, i, fib1, fib2, fib3: integer;
gs:real;
function fib(m,n:integer):integer;
begin
fib:=m+n;
end;
function goldensection(m,n:integer): real;
begin
goldensection:=m/n;
end;
begin
fib1:=0; fib2:=1;
writeln('Enter limit of Fibonacci sequence');
readln(limit);
writeln(fib1);
writeln(fib2);
for i := 3 to limit do
begin
fib3:=fib(fib1, fib2);
gs:=goldensection(fib1, fib2);
fib1:=fib2; fib2:=fib3;
writeln(fib3, gs:10:6);
end;
readln;
end.
The Fibonacci sequence begins with 0 and 1; each subsequent number is the sum of the two before, so the third is 0+1=1, the fourth is 1+1=2, the fifth is 2+1=3, the sixth is 3+2=5, etc.: 8, 13, 21...
The trick in the code is to calculate the next number in the sequence and then copy the second number into the first and the result into the second so that the next value can be calculated. Solving this sort of problem prepares you for similar and more difficult problems later.
The golden section is a ratio between two segments of a line such that the ratio of the length of the longer line segment to the length of the entire line is equal to the ratio of the length of the shorter line segment to the length of the longer line segment.
For example, the length of a line might be 13 units and the length of two segments 8 and 5. The question is whether 13:8 is equal to 8:5. 8:5 is 1.6 while 13:8 is 1.625. These values are not the same so the segments 8 and 5 do not define the golden section. We can use the Fibonacci sequence and a spreadsheet to find better and better approximations to the golden section. We won’t ever find an exact solution to the golden section by this method because each successive ratio is calculated from two different numbers.
The algorithm works by calculating the golden ratio from the two current values in the sequence and then switching the values via a third variable, next_fib. next_fib is set to the sum of the two current values, then f1 is set to f2 and f2 is set to next_fib. Thus, for example, with f1=3 and f2=5, next_fib becomes 8, f1 becomes 5 and f 2 becomes 8.
The second version of the code puts the calculation of each Fibonacci number and golden section value into a function. This gives the code more structure and prepares the ground for moving the functions into a separate unit or DLL (library) so that the functions could be incorporated into another program without having to use copy/paste or re-writing them. It will also make it easier to change the code to do things like store the Fibonacci and golden section values in a lookup array.