Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

subject: Discrete structu Project Goal: The main goal of the project is to let students use their prior knowledge, try to use the skills they

subject: Discrete structu

Project Goal: The main goal of the project is to let students use their prior knowledge, try to use the skills they have learnt to solve real world problems. Using this assignment students are required to learn to use the skills they have learned in their previous classes, solve the problems given and reflect on how they can apply it to solve real world issues. Deliverables: The students are required to submit a written report along with the programs they have written. The report has to address all the prompts mentioned. The report should be properly presented by using appropriate font (Time new roman, preferably font size 12). Please keep the report ANONYMOUS without adding any name or course information. Tasks: 1. Write an iterative C++ function that inputs a nonnegative integer n and returns the nth Fibonacci number. 2. 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 fn is the nth Fibonacci number. What are times taken to find out the exact values? II. Find the smallest Fibonacci number (1) greater than 1,000,000, and (2) 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, 40up to 200 and draw a graph and see the pattern. Initial report submission to Discussions, due on Sunday April 16: 1. Prepare a project report in WORD document to describe how you implement above tasks, including (1) problem analysis and solution plan, (2) source code, (3) discussion on experimental results in tables or graphs, (4) reflection and conclusion. 2. Your reflection should address all of the below mentioned questions a) Describe the Fibonacci series and write briefly what you have done in the assignment. b) What are the different skills, programming techniques have you used in order to run the experiments? c) 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? d) 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? e) 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? Peer review at Discussions, due on Wednesday April 19: 3. Check at least two other students reflection, and post your comments to others work, by Monday April 17. 4. Read other students comments, and provide feedback to the comments. Final report revision and resubmission based on feedback, due on April 24: 5. Prepare a WORD document including the revised project report, remove all information related to the names, or class information, and submit the final version to Assessment.

Show transcribed image text View comments (1)

#include #include

using namespace std;

/* Fibonacci Series: recursive version */ int Fibonacci_Recursive(int n) { if(n<=0) return 0; else if(n==1) return 1; else return Fibonacci_Recursive(n-1)+Fibonacci_Recursive(n-2); }

// Fibonacci Series: iterative version int Fibonacci_Iterative(int n) { int fib[] = {0,1,1}; for(int i=2; i<=n; i++) { fib[i%3] = fib[(i-1)%3] + fib[(i-2)%3]; //cout << "fib(" << i << ") = " << fib[i%3] << endl; } return fib[n%3]; }

int main(void) { int n; int long recursiveResult = 0; int long iterativeresult = 0; cout << "n = "; cin>>n; if(n < 0){ cout << " kindly enter nonnegative integer :" << endl; return 0; } const long double sysTime = time(0); const long double sysTimeMS = sysTime*1000; cout << "System Time in milliseconds is " << sysTimeMS << "." << endl; // calculate the fib(i) from scratch for each i <= a using your recursive function cout << "nth Fibonacci number From recursive technique : " << "fib(" << n << ") = " << Fibonacci_Recursive(n) << endl; cout << endl;

const long double endSysTime = time(0); const long double endSysTimeMS = sysTime*1000; cout << " Recursive approch done " << endSysTimeMS << "." << endl; cout << "Time taken by recursive approch " << endSysTimeMS - sysTimeMS << endl;

// or calculate fib(a) once and output the intermediate results from the looping version cout << "Nth Fibonacci number from iterative technique : " << Fibonacci_Iterative(n) << endl; cout << endl; const long double end2SysTime = time(0); const long double end2SysTimeMS = sysTime*1000; cout << "Iterative approch done " << end2SysTimeMS << "." << endl; cout << " Time difference between both the approch :" << end2SysTimeMS - endSysTimeMS << endl; return 0; }

Description :

1. The above program is responsible for calculating Nth Fibonacci series number using both the techniques like recursive as well as iterative.

2. the above program caters following functinalities

A. Write an iterative C++ function that inputs a nonnegative integer n and returns the nth Fibonacci number.

B. Write a recursive C++ function that inputs a nonnegative integer n and returns the nth Fibonacci number.

Question 3: Compare the number of operations and time taken to compute Fibonacci numbers recursively versus that needed to compute them iteratively.

Answer : i have perfromed the testing of above program by taking maximum number and i found that when take n = 40 then it seems that recursive fuction is working find and gives the result but when i increase the value of n to 40+ or 50 , 60 , 80 and 100 above then the process takes too much time to calculate it so the nth number of series will become too large to supprort data types.Following output i got when i executed above program with n = 40 and 50

Output

----------

n = 50 will take too much times to perform the operation in case of recursive technique where as iin case of iterative technique, it will perform operation very fast.

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 fn is the nth Fibonacci number. What are times taken to find out the exact values? II. Find the smallest Fibonacci number (1) greater than 1,000,000, and (2) greater than 1,000,000,000. I. 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?

Answer :

f100 = 3736710778780434371 Iterative approch done 1492259751000 ms. f500 = 2171430676560690477 Iterative approch done 1492259780000 ms f1000 = 817770325994397771 Iterative approch done 1492259800000 ms

Time difference = f500 - f100 = 29000 ms or 29 sec Time difference = f100 - f500 = 1492259800000 - 1492259780000 = 20000ms

II. Find the smallest Fibonacci number (1) greater than 1,000,000, and (2) greater than 1,000,000,000.

31th element in series would be greater than 1,000,000 - 1346269 45th element in series would be greater than 1,000,000,000 - 1134903170

Note : in above code just take long long datat type instead of int and you can get any nth number in series.

i.e long long Fibonacci_Iterative(int n)

can you please fix it and do the rest please.

2. Your reflection should address all of the below mentioned questions a) Describe the Fibonacci series and write briefly what you have done in the assignment. b) What are the different skills, programming techniques have you used in order to run the experiments? c) 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? d) 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? e) 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?

please help me and give me a correct answer.

Show transcribed image text

**************** i have fix it like this.......

Write an iterative C++ function that inputs a nonnegative integer n and returns the nth Fibonacci number:

/*

* C++ Program to Find Fibonacci Numbers using Iteration

*/

#include

#include

#include

#define ll long long

using namespace std;

/*

* Iterative function to find Fibonacci Numbers

*/

ll fibo_iter(int n)

{

int previous = 1;

int current = 1;

int next = 1;

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

{

next = current + previous;

previous = current;

current = next;

}

return next;

}

/*

* Main

*/

int main()

{

int n;

while (1)

{

cout<<"Enter the integer n to find nth fibonnaci no.: ";

cin>>n;

if (n == 0)

break;

cout<

}

return 0;

}

Output:

$ g++ fibo_iter.cpp

$ a.out

Enter the integer n to find nth fibonnaci no.: 1

1

Enter the integer n to find nth fibonnaci no.: 2

1

Enter the integer n to find nth fibonnaci no.: 3

2

Enter the integer n to find nth fibonnaci no.: 4

3

Enter the integer n to find nth fibonnaci no.: 5

5

Enter the integer n to find nth fibonnaci no.: 6

8

Enter the integer n to find nth fibonnaci no.: 7

13

Enter the integer n to find nth fibonnaci no.: 8

21

Enter the integer n to find nth fibonnaci no.: 9

34

Enter the integer n to find nth fibonnaci no.: 10

Write a recursive C++ function that inputs a nonnegative integer n and returns the nth Fibonacci number:

#include

using namespace std;

int fibonacci(int n)

{

if((n==1)||(n==0))

{

return(n);

}

else

{

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

}

}

int main()

{

int n,i=0;

cout<<"Input the number of terms for Fibonacci Series:";

cin>>n;

cout<<" Fibonacci Series is as follows ";

while(i

{

cout<<" "<

i++;

}

return 0;

}

Compare the number of operations and time taken to compute Fibonacci numbers recursively versus that needed to compute them iteratively:

Recursive Fibonacci:

Fibonacci can be defined recursively as follows. Each number is the sum of the previous two numbers in the sequence. The nth Fibonacci number is

That is, the nth number is formed by adding the two previous numbers together. The equation for Fib(n) is said to be the recurrence relation and the terms for Fib(0) and Fib(1) are the base cases.

Iterative Fibonacci:

The Fibonacci sequence can be defined iteratively by describing how to obtain the next value in the sequence. That is, if you start at the base cases of Fib(0) = Fib(1) = 1, you can build the sequence up to Fib(n) without using recursive calls.

For example, calculating Fib(5) may go as follows.

Fib(0) = 1

Fib(1) = 1

Fib(2) = Fib(1) + Fib(0) = 1 + 1 = 2

Fib(3) = Fib(2) + Fib(1) = 2 + 1 = 3

Fib(4) = Fib(3) + Fib(2) = 3 + 2 = 5

Fib(5) = Fib(4) + Fib(3) = 5 + 3 = 8

All that is needed for this calculation is a while loop!

4.

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:

#include #include

using namespace std;

/* Fibonacci Series: recursive version */ int Fibonacci_Recursive(int n) { if(n<=0) return 0; else if(n==1) return 1; else return Fibonacci_Recursive(n-1)+Fibonacci_Recursive(n-2); }

// Fibonacci Series: iterative version int Fibonacci_Iterative(int n) { int fib[] = {0,1,1}; for(int i=2; i<=n; i++) { fib[i%3] = fib[(i-1)%3] + fib[(i-2)%3]; //cout << "fib(" << i << ") = " << fib[i%3] << endl; } return fib[n%3]; }

int main(void) { int n; int long recursiveResult = 0; int long iterativeresult = 0; cout << "n = "; cin>>n; if(n < 0){ cout << " kindly enter nonnegative integer :" << endl; return 0; } const long double sysTime = time(0); const long double sysTimeMS = sysTime*1000; cout << "System Time in milliseconds is " << sysTimeMS << "." << endl; // calculate the fib(i) from scratch for each i <= a using your recursive function cout << "nth Fibonacci number From recursive technique : " << "fib(" << n << ") = " << Fibonacci_Recursive(n) << endl; cout << endl;

const long double endSysTime = time(0); const long double endSysTimeMS = sysTime*1000; cout << " Recursive approch done " << endSysTimeMS << "." << endl; cout << "Time taken by recursive approch " << endSysTimeMS - sysTimeMS << endl;

// or calculate fib(a) once and output the intermediate results from the looping version cout << "Nth Fibonacci number from iterative technique : " << Fibonacci_Iterative(n) << endl; cout << endl; const long double end2SysTime = time(0); const long double end2SysTimeMS = sysTime*1000; cout << "Iterative approch done " << end2SysTimeMS << "." << endl; cout << " Time difference between both the approch :" << end2SysTimeMS - endSysTimeMS << endl; return 0; }

Output:

f100 = 3736710778780434371 Iterative approch done 1492259751000 ms. f500 = 2171430676560690477 Iterative approch done 1492259780000 ms f1000 = 817770325994397771 Iterative approch done 1492259800000 ms

Time difference = f500 - f100 = 29000 ms or 29 sec Time difference = f100 - f500 = 1492259800000 - 1492259780000 = 20000ms

Find as many prime Fibonacci numbers as you can. It is unknown whether there are infinitely many of these:

#include

#include

#include

// Declaration of the function

int isprime(int);

main()

{

clrscr();

int p,a=1,b=2,c=0;

p=isprime(a);// calling function

if (p==1) cout<

p=isprime(b);// calling function

if (p==1) cout<

while(c<=10000)

{

c=a+b;

p=isprime(c);

if((p==1) && (c<=10000))

cout<

a=b;

b=c;

}

cout<

cout<<"Press any key to continue";

getch();

return 0;

}

// Defining the function

int isprime(int x)

{

int i;

if (x==1) return 0;

else

{

int pop=1;

for(i=2;i

{

if((x%i==0))

pop=2;

}

return pop;

}******************another way to

}#include #include #include #define int long long using namespace std; int iterativefibonacci(int input) { int before = 1; int present = 1; int later = 1; for (int i = 3; i <= input; ++i) { later = present + before; before = present; present = later; } return later; } int main() { int input; while (1) { cout<<"Enter the integer n to find nth fibonnaci no.Press 0 to exit: "; cin>>input; if (input == 0) break; cout<

Using Recursion :

#include using namespace std; int fibonacci(int input) { if((input==1)||(input==0)) { return(input); } else { return(fibonacci(input-1)+fibonacci(input-2)); } } int main() { int input,i=0; cout<<"Input the number of terms for Fibonacci Series:"; cin>>input; cout<<" Fibonacci Series is as follows "; while(i

******************************* base on this please answer those question. Think you are me and answer it in a paragraph. Answer it in a correct way. thanks a lot.

Prepare a project report in WORD document to describe how you implement above tasks, including (1) problem analysis and solution plan, (2) source code, (3) discussion on experimental results in tables or graphs, (4) reflection and conclusion.

I need this part please..

2. Your reflection should address all of the below mentioned questions a) Describe the Fibonacci series and write briefly what you have done in the assignment. b) What are the different skills, programming techniques have you used in order to run the experiments? c) 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? d) 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? e) 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?

*** Can someone please fix that all and please compare task 3 and task 4, graph and how we can use in real world problem.

Again,, I dont need source code,, please can someone plese finish the project perfectly

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intranet And Web Databases For Dummies

Authors: Paul Litwin

1st Edition

0764502212, 9780764502217

More Books

Students also viewed these Databases questions

Question

Belonging = lIInferiority Feelings. What does this ratio mean?

Answered: 1 week ago