Kat H.

asked • 05/23/21

Fibonacci functions. You will write three functions, each of which computes Fibonacci numbers.

Fibonacci functions. You will write three functions, each of which computes Fibonacci numbers.


Write the assembly code for the functions fibonacci_recursive.s, fibonacci_iterative.s, fibonacci_closed.s. Provide the assembly code as the answers for all three functions separately.


main.c code


#include <stdio.h>

#include <inttypes.h>

#include <math.h>

uint64_t fibonacci_recursive(uint64_t n);

uint64_t fibonacci_iterative(uint64_t n);

uint64_t fibonacci_closed(uint64_t n);

double phi;

int main() {

uint64_t n;

scanf("%" SCNu64, &n);

phi = (1.0 + sqrt(5.0)) / 2.0;

printf("Recursive: %" PRIu64 "\n", fibonacci_recursive(n));

printf("Iterative: %" PRIu64 "\n", fibonacci_iterative(n));

printf("Closed: %" PRIu64 "\n", fibonacci_closed(n));

return 0;

}


Code for the functions:



#include <stdio.h>

#include <inttypes.h>

#include <math.h>

uint64_t fibonacci_recursive(uint64_t n);

uint64_t fibonacci_iterative(uint64_t n);

uint64_t fibonacci_closed(uint64_t n);

double phi;

int main() {

    uint64_t n;

    scanf("%" SCNu64, &n);

    phi = (1.0 + sqrt(5.0)) / 2.0;

    

    printf("Recursive: %" PRIu64 "\n", fibonacci_recursive(n));

    printf("Iterative: %" PRIu64 "\n", fibonacci_iterative(n));

    printf("Closed: %" PRIu64 "\n", fibonacci_closed(n));

    

    return 0;

}

uint64_t fibonacci_recursive(uint64_t n)

{

    if(n == 0)

        return 0;

    else if (n == 1)

        return  1;

    return (fibonacci_recursive(n - 1) + fibonacci_recursive(n - 2));

}

uint64_t fibonacci_iterative(uint64_t n)

{

    uint64_t fn = 1, fn_1 = 0;

    for(int i = 2; i <= n; i++)

    {

        uint64_t temp = fn + fn_1;

        fn_1 = fn;

        fn = temp;

    }

    return fn;

}

uint64_t fibonacci_closed(uint64_t n)

{

    return (pow(phi,n) - pow(-1,n)/pow(phi,n))/sqrt(5);

}


1 Expert Answer

By:

Rize S. answered • 03/23/23

Tutor
New to Wyzant

25 Yrs Exp + MISM: General Computer Expert

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.