Ask Question
21 April, 20:27

The Fibonacci numbers are the numbers

(1,1,2,3,5,8,13,21,34)

where the first two numbers are 1 and 1, and each number after that is the sum of the two previous numbers. (So $2 = 1+1$, $3 = 2+1$, and so on.)

Write an algorithm, in English, that takes a positive integer $n$ as input and then outputs the $n$th Fibonacci number. For example, if we input to the algorithm $n=2,$ your algorithm should output 1. If we input $n=6,$ your algorithm should output 8.

Remember that an algorithm should consist of a series of unambiguous steps. Use variable names to identify what values you are referring to. You can assume that the computer can do basic arithmetic.

Above, we have given two input-output examples. Be sure to test your algorithm to see if it can successfully reproduce these examples and any others you care to test.

+5
Answers (1)
  1. 21 April, 20:36
    0
    function fibonacci (n):

    if n is equal to 1 or n is equal to 2:

    return 1

    else:

    return fibonacci (n-1) + fibonacci (n-2)

    end of the function

    Input the n

    Print fibonacci (n)

    Explanation:

    * The above algorithm (pseudocode) is written considering Python.

    Create a function called fibonacci that takes one parameter, n

    If n is equal to 1 or 2, return 1 (When n is 1 or 2, the fibonacci numbers are 1)

    Otherwise, return the sum of the two previous numbers (When n is not 1 or 2, the fibonacci number is equal to sum of the two previous numbers)

    Ask the user for n

    Call the function, pass n as a parameter and print the result
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “The Fibonacci numbers are the numbers (1,1,2,3,5,8,13,21,34) where the first two numbers are 1 and 1, and each number after that is the sum ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers