How to print the first 12 terms of the Fibonacci sequence inverse in Pascal?

-1

How to print the first 12 terms of the Fibonacci sequence inverse in Pascal? Instead of being 1 to 12, from 12 to 1

program Exercicio_31;

var V:array[3..12]of integer;I,termo1,termo2,novoTermo:integer;

begin


     writeln('Serie de Fibonacci');
     writeln('==================');
     termo1 := 1;
     termo2 := 1;
     writeln(' 1o. termo = ', termo1);
     writeln(' 2o. termo = ', termo2);
     for I:=12 downto 3 do 
     begin
        novoTermo := termo1 + termo2; { o novo termo e a soma dos dois termos anteriores }
        writeln(I:2, 'o. termo = ', novoTermo);
        termo1 := termo2;   {o segundo termo e o primeiro termo no proximo passo }
        termo2:=novoTermo; { o novo termo e o segundo termo no proximo passo }
     end;
     writeln('==================');
     writeln;
     write('Pressione [algo] para prosseguir.');
     readkey;
end.
    
asked by anonymous 15.02.2017 / 15:59

1 answer

3

The simplest way is to begin the iteration already with the value of the 12th number and the 11th number and subtracting them until the initial values of the sequence are reached:

program Exercicio_31;

var V:array[3..12]of integer;
I,termo1,termo2,novoTermo:integer;

begin


     writeln('Serie de Fibonacci');
     writeln('==================');
     termo1 := 144;
     termo2 := 89;
     writeln('12o. termo = ', termo1);
     writeln('11o. termo = ', termo2);
     for I:= 10 downto 1 do 
     begin
        novoTermo := termo1 - termo2; { o novo termo e a soma dos dois termos anteriores }
        writeln(I, 'o. termo = ', novoTermo);
        termo1 := termo2;   {o segundo termo e o primeiro termo no proximo passo }
        termo2 := novoTermo; { o novo termo e o segundo termo no proximo passo }
     end;
     writeln('==================');
     writeln;
     write('Pressione [algo] para prosseguir.');
end.

Output

  

Fibonacci series

     

==================
  12th. term = 144
  11o. term = 89
  10th. term = 55
  9o. term = 34
  8o. term = 21
  7th. term = 13
  6th. term = 8
  5th. term = 5
  4th. term = 3
  3rd. term = 2
  2nd. term = 1
  1o. term = 1

     

==================

    
15.02.2017 / 16:41