Determine if a number of two digits belongs to the fibonacci series

0

I need to determine if a two-digit number belongs to the fibonacci series, I have the following code:

'''Leer un número de dos dígitos y determinar si pertenece a la serie de Fibonacci. Con ciclos.'''

numero1 = int(raw_input("Digite un numero de dos digitos: "))

if numero1 > 9 and numero1 <=99:

    inicial = 0
    numero = 1
    ultimo = 0
    penultimo = 0

    while numero1 <= 100:
        penultimo = ultimo
        ultimo = numero
        numero = penultimo + ultimo 

    if numero1 == ultimo:
        print "Si pertenece a las serie Fibonacci."


    else:
        print "El numero no pertenece a la serie Fibonacci."

else:
    print "Debe ingresar un numero de dos digitos"

But I can not get it to show me what I need, it just does not print anything.

    
asked by Gustavo Adolfo Rodriguez 29.08.2018 в 03:10
source

2 answers

1

The problems are in the cycle while , this part of your program is the one that is really responsible for obtaining the values of the Fibonacci sequence iteratively.

The first problem comes on line while numero1 <= 100 . numero1 is the number from the input and is never modified in the cycle . As numero1 is always less than 100 for the if previous, numero1 <= 100 is always true and we have an infinite cycle always. As in each iteration of the cycle we obtain an item of the sequence and you want to know if numero1 is part of it, the conditional must be:

while ultimo < numero1:

where last is the last item of the sequence calculated in the last iteration. This causes the cycle to end if the last item of the calculated sequence is equal ( numero1 belongs to the sequence) or higher ( numero1 does not belong to the sequence).

On the other hand, you have complicated in the calculation of each item of the series, you only need two variables, one that stores the penultimate item of the sequence and another the last calculated. In each iteration, the new item is the sum of these two previous values and the penultimate one passes to sel the last last calculated value:

temporal = penultimo
penultimo = ultimo
ultimo = penultimo + ultimo

In Python you do not need the temporary variable, you can simply do:

penultimo, ultimo = ultimo, penultimo + ultimo 

The code may look like this:

numero1 = int(raw_input("Digite un numero de dos digitos: "))

if 9 < numero1 <= 99:
    penultimo = 0
    ultimo = 1

    while ultimo < numero1:
        penultimo, ultimo = ultimo, penultimo + ultimo

    if numero1 == ultimo:
        print "Si pertenece a las serie Fibonacci."

    else:
        print "El numero no pertenece a la serie Fibonacci."

else:
    print "Debe ingresar un numero de dos digitos"
    
answered by 29.08.2018 в 03:55
0

I would like to provide a different approach to the problem but also I take this opportunity to remember that day by day python 2 is closer to being declared obsolete, and he should have delegated his role in the learning and creating new code to python 3.

The approach I propose involves generating the complete list of values from the beginning and then you can simply check if that list contains the number searched.

To generate the list you can use a generating function like the next:

def generar_fibo_mayores_a_10(limite):
    a, b = 13, 21
    while a < limite:
        yield a
        a, b = b, a + b

Through which you can generate elements until you reach a "limit" established by its sole argument. In the case of this problem, the limit would be 100, so the complete list can be generated with:

serie = [f for f in generar_fibo_mayores_a_10(limite=100)]

Now if we want to know if a specific number belongs to the series, it's enough with doing:

if numero in serie:
    print('Si pertenece')
else:
    print('No pertenece')

After all, it would be practically the same if you were only going to consult a number as in this case, although it could be much more efficient in case of require to do the process several times.

In case it is not clear how to integrate these ideas in your code, here I leave you the complete example:

def generar_fibo_mayores_a_10(limite):
    a, b = 13, 21
    while a < limite:
        yield a
        a, b = b, a + b

serie = [f for f in generar_fibo_mayores_a_10(limite=100)]

numero = int(input('Digite un número de dos dígitos: '))
if 9 < numero <= 99:
    print('El número', ('si' if numero in serie else 'no'),
        'pertenece a la serie de fibonacci')
else:
    print('Debe ingresar un número de dos dígitos')
    
answered by 29.08.2018 в 04:25