Problem

In mathematics, the Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, that is characterized by the fact that every number after the first two is the sum of the two preceding ones:

\[0,~1,~1,~2,~3,~5,~8,~13,~21,~34,~55,~89,~144,~\dots\]

with the following sequence equation,

\[\begin{eqnarray} Fib(n) &=& Fib(n-1) + Fib(n-2) ~, \\ Fib(0) &=& 0 ~, \\ Fib(1) &=& 1 ~. \end{eqnarray}\]

Write a function named fib that takes in an input argument which should be integer number n, and then calculates the $n$th number in the Fibonacci sequence and outputs it on the screen. Also, if the input argument is not a non-negative integer, it prints an error message on the screen and asks the user to re-enter a non-negative integer number. Also, when it is done with finding the requested Fibonacci number, it asks again the user to either input a new non-negative integer, or enter ‘stop’ to end the function, like the following,

fib
Please enter a non-negative integer or type stop: -123
The input argument is not a non-negative integer!
Please enter a non-negative integer or type stop: a
The input argument is not a non-negative integer!
Please enter a non-negative integer or type stop: amir
The input argument is not a non-negative integer!
Please enter a non-negative integer or type stop: 
The input argument is not a non-negative integer!
Please enter a non-negative integer or type stop: -12.3
The input argument is not a non-negative integer!
Please enter a non-negative integer or type stop: 0
fib(0) = 0
Please enter a non-negative integer or type stop: 1
fib(1) = 1
Please enter a non-negative integer or type stop: 2
fib(2) = 1
Please enter a non-negative integer or type stop: 3
fib(3) = 2
Please enter a non-negative integer or type stop: 4
fib(4) = 3
Please enter a non-negative integer or type stop: 5
fib(5) = 5
Please enter a non-negative integer or type stop: 6
fib(6) = 8
Please enter a non-negative integer or type stop: 7
fib(7) = 13
Please enter a non-negative integer or type stop: 8
fib(8) = 21
Please enter a non-negative integer or type stop: 9
fib(9) = 34
Please enter a non-negative integer or type stop: 10
fib(10) = 55
Please enter a non-negative integer or type stop: 11
fib(11) = 89
Please enter a non-negative integer or type stop: 12
fib(12) = 144
Please enter a non-negative integer or type stop: 22
fib(22) = 17711
Please enter a non-negative integer or type stop: 32
fib(32) = 2178309
Please enter a non-negative integer or type stop: stop

MATLAB

Hint:

  1. First write a function getFib(n_int) that finds the requested Fibonacci number for you, given a strictly non-negative integer input (for example, name it n_int).

  2. Then put this function inside another MATLAB function fib() that asks the user to input a number (which could be potentially anything: a string, a real number, a complex number, or an integer). You can do this using MATLAB’s builtin function input(). Read the input value as a string using this MATLAB function. Then check if the user’s input string is equivalent to ‘stop’ or not. If it is ‘stop’, then the program must return, otherwise, use str2double() to convert this string to MATLAB numeric type. Then check is the numeric-converted input value is a real number (as opposed to complex number) via MATLAB’s function isreal(). If the value is real, then check if it is a non-negative real number. Also, check if it is an integer by comparing the value with its rounded value using MATLAB’s function round(). Thus is the user-input number is really a non-negative integer, then your code should display the result message as given in the above output (by calling your nested function getFib(n_int)), and then call this function fib to ask the user to input another number again, to repeat this procedure. If the user-input number is not a non-negative integer, then your code should display the requested message above, and call the function fib again to ask the user for another input.
Python

Hint:

  1. First write a function fibo(n_int) that finds the requested Fibonacci number for you, given a non-negative integer input (for example, name it n_int).
  2. Then put this function inside another Python function fib(n) that checks the type of the input argument n and prints the appropriate error message as in the above and then asks the user to enter another number (and then again checks for its type to be an integer).
  3. Then if this number is an integer, this function fib(n) passes the integer number n to the function fibo(n_int) which is inside of itself (it is a nested function), to get the requested Fibonacci number.
  4. Finally, once the requested Fibonacci number is obtained, it prints the number value with the requested format as in the above example AND then asks again the user to input a new non-negative integer, or simply type stop to stop the function.

Note that, if you call the function as fib('stop') in the Python interpreter, it should return nothing to you, just like the following example,

fib('stop')

I highly recommend you to write your function in Jupyter notebook, test it there, and then get the results for the same input arguments as in the above example (a string, negative integer, float, and n=1,…,12, and also ‘stop’) and download all of the notebook as a Markdown file, and present this file as your final solution. Name the notebook, fib.md.

Solution

MATLAB

Here is an example implementation of fib.m,

function fib()

    n = input('Please enter a non-negative integer or type stop: ','s');
    if strcmp(n,'stop')
        return
    else
        n = str2double(n);
        if isreal(n)
            if n>=0 && round(n)==n
                disp(['fib(',num2str(n),') = ',num2str(getFib(n))]);
                fib()
                return
            end
        end
        disp('The input argument is not a non-negative integer!');
        fib()
    end
    
    function fib = getFib(n_int)
        if n_int == 0
            fib = 0;
        elseif n_int == 1
            fib = 1;
        else
            fib = getFib(n_int-1) + getFib(n_int-2);
        end
    end

end
Python
def fib(n):

    def fibo(n_int):
        if n_int==0: return 0
        elif n_int==1: return 1
        else:
            return fibo(n_int-1) + fibo(n_int-2)

    if n=='stop':
        return None
    elif not str(n).isdigit():    # Make sure n is integer, if not then
        print( 'The input argument {} is not a non-negative integer!'.format(n) )    
        n = input("Please enter an integer: ")  # Note that n is read as string!
        return fib(n)
    else:
        n=int(n) 
        print('Fib({}) = {}'.format(n,fibo(n)))
        n = input("Please enter another integer or type stop: ")  # Note that n is read as string!
        return fib(n)

Comments