Access over 20 million homework & study documents
search

3) Compare the number of operations and time taken to compute Fibona

Content type

User Generated

Rating

Showing Page:
1/3
3) Compare the number of operations and time taken to
compute Fibonacci numbers recursively versus that
needed to compute them iteratively.
4) Use the above functions to write a C++ program for
solving each of the following computational problems.
* Find the exact value of f100, f500, and f1000, where fn is
the nth Fibonacci number. What are times taken to find out
the exact values?
* Find the smallest Fibonacci number greater than
1,000,000 and greater than 1,000,000,000.
* Find as many prime Fibonacci numbers as you can. It is
unknown whether there are infinitely many of these. Find
out the times taken to find first 10, 20, 30, 40up to 200
and draw a graph and see the pattern.
Solution
Desired Program is given, here we can see that time taken
is less then a second and its very fast.
#include<stdio.h>
#include<conio.h>
#include<time.h>
int calSumOfFib(int n)
{
int a,b,c = 0, i,sum =0;

Sign up to view the full document!

lock_open Sign Up
Showing Page:
2/3
a=0;
b=1;
sum=1;
for(i=1;i<=n-2;i++)
{
c=a+b;
sum=sum+c;
a=b;
b=c;
}
return sum;
}
int main ()
{
long int fibSum = 0;
double totalTimeTaken;
clock_t t;
t = clock();
fibSum = calSumOfFib(10);
t = clock() - t;
totalTimeTaken = ((double)t)/CLOCKS_PER_SEC;
printf(\"The value of f(10) is %d \ \", fibSum );
printf(\"f(10) takes %f seconds to execute \ \",
totalTimeTaken);

Sign up to view the full document!

lock_open Sign Up
Showing Page:
3/3

Sign up to view the full document!

lock_open Sign Up

Unformatted Attachment Preview

3) Compare the number of operations and time taken to compute Fibonacci numbers recursively versus that needed to compute them iteratively. 4) Use the above functions to write a C++ program for solving each of the following computational problems. * Find the exact value of f100, f500, and f1000, where fn is the nth Fibonacci number. What are times taken to find out the exact values? * Find the smallest Fibonacci number greater than 1,000,000 and greater than 1,000,000,000. * Find as many prime Fibonacci numbers as you can. It is unknown whether there are infinitely many of these. Find out the times taken to find first 10, 20, 30, 40…up to 200 and draw a graph and see the pattern. Solution Desired Program is given, here we can see that time taken is less then a second and its very fast. #include #include #include int calSumOfFib(int n) { int a,b,c = 0, i,sum ...
Purchase document to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Anonymous
Great content here. Definitely a returning customer.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4