• 1
    Input and Output Data
    • Tasks
  • 2
    Conditions
    • Tasks
  • 3
    For Loop
    • Tasks
  • 4
    Strings
    • Tasks
  • 5
    While Loop
    • Tasks
  • 6
    Lists
    • Tasks
  • 7
    Two-Dimensional Arrays
    • Tasks
  • 8
    Dictionaries
    • Tasks
  • 9
    Sets
    • Tasks
  • 10
    Functions and Recursion
    • Tasks
  • к

Занятие 5. While cycle

Difficulty level:

Task«Leonardo Pizan»

the great mathematician Leonardo Pizan, known under the pseudonym Fibonacci, left us the famous numerical sequence. Each of its elements, starting with the third, is the sum of the previous two.

rn

the Fibonacci sequence is determined as follows:
& Phi; ₀ = 0
& Phi; ₁ = 1
& Phi; ₙ = & Phi; ₙ₋₁ + & PHI; ₙ₋₂ for n & gt; 1

rn

the beginning of the sequence looks like this: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...

rn

you are given a natural number A. Your task & mdash; Determine whether this number is part of the Fibonacci sequence. If yes, then withdraw its serial number N (index), such that & Phi; n = A. If the number 1 occurs in the sequence, assume that its index is 1. If the number a is not the number of fibonacci, remove -1.

Input format

The one is introduced by the whole non -negative number a (0 & le; a & le; 10 & sup1; ⁸).

Output format

Bring one whole number: serial number N for which & Phi; n = A. If such a number does not exist, derive -1.

Example

Input

55

Output

10

Hint

There will be no clue here, decide for yourself!

main.py
Test 1
Test 2
Test 3
Test 4
Test 5
Test 6
Test 7
Test 8
Test 9
Test 10
Developer’s solution
# Получаем на вход число A
a = int(input())

# Обрабатываем частный случай для 0
if a == 0:
    print(0)
else:
    # Инициализируем первые два члена последовательности и их индекс n.
    # f0 соответствует φ(n-1), f1 соответствует φ(n)
    f0, f1 = 0, 1
    n = 1
    
    # Используем цикл while для генерации чисел Фибоначчи,
    # пока текущее число f1 меньше искомого числа a.
    while f1 < a:
        # Это "магия" Python для параллельного присваивания.
        # Новое значение f0 становится старым f1,
        # а новое f1 становится суммой старых f0 и f1.
        # Это эквивалентно:
        # temp = f1
        # f1 = f0 + f1
        # f0 = temp
        f0, f1 = f1, f0 + f1
        n += 1
        
    # После выхода из цикла проверяем, почему он завершился.
    # Если f1 равно a, значит, мы нашли число.
    if f1 == a:
        print(n)
    # Если f1 стало больше a, значит, мы "проскочили" искомое число,
    # и его нет в последовательности.
    else:
        print(-1)

🎉 Congratulations! 🎉

You did an excellent job with the task! It was a challenging problem, but you found the correct solution. You are one step closer to mastering programming! Keep up the good work, because every stage you pass makes you even stronger.

AD

Advertisement

red-snake blue-snake green-snake

Running your code...

Помощник ИИ

Привет! Я твой помощник по программированию. Задавай любые вопросы по Python, я могу рассказать о функциях, методах, обьяснить то, что тебе не понятно, а так же о текущей задаче!