How to print the first N numbers of the Fibonacci series in PseInt

How to print the first N numbers of the Fibonacci series in PseInt

In multiple practice exercises at school or university, at some point you will have to develop a program that prints the first N numbers of the famous Fibonacci series. p. ex: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34. This series is described in a short way, assuming that each number is obtained from the sum of the two previous numbers in the series .

This sequence was described by Fibonacci as the solution to a rabbit breeding problem: “A certain man has a pair of rabbits together in a closed place and he wants to know how many are created from this pair in a year when, according to his nature, each couple takes a month to grow old and each subsequent month procreates another couple ”(Laurence Sigler, Fibonacci's Liber Abaci, page 404).

The answer to this question is as follows:

  • We started with a couple of rabbits the first month.
  • The second month the couple ages but does not procreate.
  • The third month the couple procreates another couple (that is, we already have two partners).
  • In the fourth month, the first couple procreate again and the new couple ages without procreating (then we have three partners).
  • In the fifth month, the two oldest couples procreate again while the new couple does not procreate (five couples in total)

Starting from the numbers 0 and 1, the Fibonacci numbers are defined  by the function:

fn = (fn - 1) + (fn - 2)
f0 = 0
f1 = 1
f2 = f1 + f0 = 1
f3 = f2 + f1 = 2

Therefore, the simplest and easiest to understand implementation of the fibonacci series in PseInt would be:

Algoritmo SerieFibonacci
	Definir _numero, a, b, tmp como Entero
	Escribir "- Serie de Fibonacci -"
    Escribir "	Por favor ingrese la cantidad de números de la serie que se imprimirán:"
    Leer _numero
    b = 1
    Para i<-1 Hasta _numero Hacer
        Escribir a
        tmp = a + b
        a = b
        b = tmp

Happy coding ❤️!

This could interest you

Become a more social person