Web & Social Media Analytics Previous Year Question Paper.pdf
Write a recursive C++ function that inputs a nonnegative integer n an.docx
1. Write a recursive C++ function that inputs a nonnegative
integer n and returns the nth Fibonacci number. 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. I.
Find the exact value of f100, f500, and f1000, where f_n is the
nth Fibonacci number. What are times taken to find out the
exact values? II. Find the smallest Fibonacci number greater
than 1,000,000 and greater than 1,000,000,000. III. 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. Report: Your report should address all of
the below mentioned questions 1. Describe the Fibonacci series
and write briefly what you have done in the assignment. 2.
What are the different skills, programming techniques have you
used in order to run the experiments? 3. What did you observe
when you did the comparisons in Task 3 and Task 4? Explain
the graph that you have drawn from Task 4.III? 4. List at least
three different applications of the Fibonacci numbers to
sciences and describe one of them in details. Think of situation
or a real world problem where you can apply concept of
Fibonacci numbers to solve it. Explain? 5. Write a paragraph,
explaining what you have done in this assignment. What were
the challenges you have faced when solving these problems.
How can you improve the programs you have written to solve
these problems?
Solution
2. 2. Recursive function to find the nth fibo no:
#include<bits/stdc++.h>
using namespace std;
int fib(int num)
{
if((num==1)||(num==0))
{
return(num);
}
else
{
return(fib(num-1)+fib(num-2));
}
}
int main()
{
int n,i=0,fibo;
cout<<"Enter val of n"<<endl;
cin>>n;
while(i<n)
{
fibo=fib(i);
3. i++;
}
cout<<n<<"th fibo no is "<<fibo<<endl;
return 0;
}
This is the program that returns that nth fibo number along
with the time it has taken to compute the work.. You can any
number you want and find the required result.
#include<bits/stdc++.h>
#include<ctime>
using namespace std;
unsigned long long int fib(int n)
{
unsigned long long int f[n+1];
unsigned long long int i;
f[0] = 0;
f[1] = 1;
for (i = 2; i <= n; i++)
{
f[i] = f[i-1] + f[i-2];
4. }
return f[n];
}
int main ()
{
clock_t t;
t = clock();
int n,i=0,fibo;
cout<<"Enter val of n"<<endl;
cin>>n;
cout<<fib(n)<<endl;
t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC;
cout<<"function took "<<time_taken<<" seconds to
execute"<<endl;
return 0;
}
Smallest fib number greater than 1000000 is 1346269
5. Smallest fib number greater than 1000000000 is 1134903170
The program that computed those values is as follows:
#include<bits/stdc++.h>
using namespace std;
int main()
{
unsigned long long int fibo;
vector<long long int> f;
int x=0;
f.push_back(x);
x=1;
f.push_back(x);
cout<<f[0]<<" "<<f[1]<<endl;
int i=2;
while(fibo<=1000000000)
{
fibo=f[i-1]+f[i-2];
f.push_back(fibo);
i++;
}
cout<<"fibo is "<<fibo<<endl;
return 0;
}
Fibonacci series:
The Fibonacci Sequence is the sequence of nos : 0, 1, 1, 2, 3, 5,
6. 8, 13, 21, 34,55,89 ... The following number iin the sequence
can be found by performing the summation of the previous 2
numbers.
I have written a program that finds the nth number in the
fibonacci series. The number can in the range of unsigned long
long int. Unsigned qualifier is used because the fibonacci series
is a positive series and doesnt have a ny negative numbers.
I have also written a program that finds the execution time of
the program to find the nth fibonacci number.
I have also written a program to find the smallest number in the
fibonacci series that is greater than 1000000,1000000000
respectively.