# How do can I update this code (Code A): Code (A) #include using namespace std; int fibonacci(int n) { int a = 0,...

How do can I update this code (Code A):

```Code (A)
#include
using namespace std;

int fibonacci(int n) {
int a = 0, b = 1, c;
if (n <= 1)
return n;
for (int i = 2; i <= n; i++) {
c = a + b;
a = b;
b = c;
}
return b;
}

int fibonacciRecursive(int n) {
if (n <= 1) {
return n;
}
return fibonacciRecursive(n-1) + fibonacciRecursive(n-2);
}

int main() {
int n;
cout << "Enter a value for n: ";
cin >> n;
cout << n << "th fibonacci number is " << fibonacci(n) << endl;
cout << n << "th fibonacci number is " << fibonacciRecursive(n) << endl;
return 0;
}```

To do what code B does?

Code (B)

#include <iostream>
#include <ctime>
#include <cmath>
using namespace std;

bool prime(int sum);
double fibonacci (double x)
{
int i = 0;
double a = 1, b = 1, sum = 1;

while(i!=x)
{
sum = a + b;
a = b;
b = sum;

while (true)
{
if (prime(sum))
{
i+=1;
cout << sum << endl;
break;
}
else
break;
}
}
return b;
}

bool prime(int sum)
{
int i;
for (i = 2; i <=(sqrt(sum)); i++)
{
if (sum % i == 0) // The number leaves no remainder
return false; // as such n is even and not prime.
}
return true; //Otherwise return true. A prime number has been found!
}

int main()
{
double number;
clock_t t;
t = clock();

cout << "Enter the number of prime fibonacci numbers you wish to find: ";
cin >> number;
cout << "The " << number << "- th Fibonacci Number is: " << endl<< fibonacci(number) << endl ;

t = clock() -t;
double time_taken = ((double)t)/CLOCKS_PER_SEC;
cout << "the time taken for the function to execute the program in milliseconds is: "<< time_taken*1000 <<endl;

return 0;
}

In other words I woud like Code A to give me the same output as code B. What needs to be added to A to make this happen?

Basically what the 'code B' does is that it calculates 'n'th prime fibonacci number.

The prime numbers are 2,3,5,7,etc we need to find 'n'th prime number in fibonacci series.

The fibonacci series is 1,1,2,3,5,8,13,...

here first prime fibonacci number is 2, next is 3, followed by 5,13,...

To accomplish this using 'code A' we use a counter along with a function(named prime) that checks if a number is prime or not.

we iterate over the fibonacci series and check if the fibonacci number is prime or not. If it is prime we increment the counter. We stop the counter when it is equal to the required number 'n'.

The following code changes helps us accomplish this:

#include <iostream>
#include <ctime>
#include <cmath>
using namespace std;

bool prime(int n) //to check if number n is prime or not
{
int i;
for (i = 2; i <=(sqrt(n)); i++)
{
if (sum % i == 0) // The number leaves no remainder
return false; // as such n is even and not prime.
}
return true; //Otherwise return true. A prime number has been found!
}

int fibonacci(int n) {
int a = 1, b = 1, c; //start from a=1 and b=1 as our first prime in the series is 2
if (n <= 1)
return n;
int count=0;
while(true) { //iterate till we find the nth prime fibonacci number
c = a + b;
a = b;
b = c;
if(prime(b)) //if prime increment the counter
count++;
if(count==n) break; //if count equals 'n' then break;
}
return b; //return the fibonacci number
}

int fibonacciRecursive(int n) { //calculates nth fibonacci number recursively
if (n <= 1) {
return n;
}
return fibonacciRecursive(n-1) + fibonacciRecursive(n-2);
}
int primeFibonacciRecursive(int n){ //helper function to find nth prime fib number using recursive function
int ans=0,start=3,count=0; /*we start with 3as our first prime fibonacci is at third position in fib series 1,1,2,3,5...*/
while(true){ //iterate till we find nth prime fibonacci
ans=fibonacciRecursive(start);
if(prime(ans)) count++; //increment counter if prime fibonacci is found
if(count==n) break; //break when nth prime fib number is found
start++; //increment the counter to find next fib number in series
}
return ans;
}

int main() {
int n;
cout << "Enter a value for n: ";
cin >> n;
cout << n << "th fibonacci number is " << fibonacci(n) << endl;
cout << n << "th fibonacci number is " << primeFibonacciRecursive(n) << endl;
return 0;
}

#### Earn Coin

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
• ### #include <iostream> using namespace std; bool binarySearch(int arr[], int start, int end, int target){ //your code...

#include <iostream> using namespace std; bool binarySearch(int arr[], int start, int end, int target){ //your code here } void fill(int arr[], int count){ for(int i = 0; i < count; i++){ cout << "Enter number: "; cin >> arr[i]; } } void display(int arr[], int count){ for(int i = 0; i < count; i++){ cout << arr[i] << endl; } } int main() { cout << "How many items: "; int count; cin >> count; int * arr = new...

• ### Flow chart of this program #include <iostream> #include <cmath> using namespace std; int mainO double sum=0.0,...

Flow chart of this program #include <iostream> #include <cmath> using namespace std; int mainO double sum=0.0, ave-ee, int locmx, locmn int n; cout <<"Enter the number of students: "<<endl; cin >> ni double listln]; double max-0; double min-e; for (int i-e ; i<n ;i++) cout<s enter the gpa: "<cendli cin>>listli]; while (listlile i listli1>4) cout<s error,Try again "<cendl; cin>listlil: sun+=list[i]; N1 if (listli]>max) for(int isin itt) max=list [i]; 10cmx=1+1 ; else if (list [i] min) min=list [i]; locmn-i+1; ave sum/n;...

• ### In C++ ***//Cat.h//*** #ifndef __Cat_h__ #define __Cat_h__ #include <string> using namespace std; struct Cat { double...

In C++ ***//Cat.h//*** #ifndef __Cat_h__ #define __Cat_h__ #include <string> using namespace std; struct Cat { double length; double height; double tailLength; string eyeColour; string furClassification; //long, medium, short, none string furColours[5]; }; void initCat (Cat&, double, double, double, string, string, const string[]); void readCat (Cat&); void printCat (const Cat&); bool isCalico (const Cat&); bool isTaller (const Cat&, const Cat&); #endif ***//Cat.cpp//*** #include "Cat.h" #include <iostream> using namespace std; void initCat (Cat& cat, double l, double h, double tL, string eC,...

• ### Use the functions below to write a C++ program to find the smallest Fibonacci number greater...

Use the functions below to write a C++ program to find the smallest Fibonacci number greater than 1,000,000 and greater than 1,000,000,000. #include <iostream> using namespace std; int fibonacci(int n) { int a = 0, b = 1, c; if (n <= 1) return n; for (int i = 2; i <= n; i++) { c = a + b; a = b; b = c; } return b; } int fibonacciRecursive(int n) { if (n <= 1) { return...

• ### I NEED A PSEUDOCODE ALGORITHM FOR THIS CODE PLEASE C++: #include #include #include #include using...

I NEED A PSEUDOCODE ALGORITHM FOR THIS CODE PLEASE C++: #include #include #include #include using namespace std; int NumOfEmployees(); int TotDaysAbsent(int); double AverageAbsent(int, int); int main() {         cout << endl << "Calculate the average number of days a company's employees are absent." << endl << endl;      int numOfEmployees = NumOfEmployees();         TotDaysAbsent(numOfEmployees);    return 0; } int NumOfEmployees() {    int numOfEmployees = 0;     cout << "Please enter the number of employees in the company: ";         cin >> numOfEmployees;     while(numOfEmployees <= 0)     {            ...

• ### Watermelon Problem: The answer should not include any whitespace. #include<iostream> using namespace std; int main() {...

Watermelon Problem: The answer should not include any whitespace. #include<iostream> using namespace std; int main() {     double distance, gravity =9.8, height;     int time, t=0;     cout<<"Please input the time of fall in seconds:"<<endl;     [ A ] // Get the user input of the time of fall in seconds (Use cin>> method)     cout<<endl;     cout<<"Please input the height of the bridge in meters:"<<endl;     [ B ] // Get the user input of the height of the bridge in meters (Use cin>>...

• ### #include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car {...

#include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car { private: string reportingMark; int carNumber; string kind; bool loaded; string choice; string destination; public: Car() { reportingMark = ""; carNumber = 0; kind = "Others"; loaded = 0; destination = "NONE"; } ~Car() { } void setUpCar(string &reportingMark, int &carNumber, string &kind, bool &loaded, string &destination); }; void input(string &reportingMark, int &carNumber, string &kind, bool &loaded,string choice, string &destination); void output(string &reportingMark, int &carNumber,...

• ### Need a FLOW Chart for that code. #include <iostream > using namespace std; int PowerFive(int); //Function...

Need a FLOW Chart for that code. #include <iostream > using namespace std; int PowerFive(int); //Function prototype declaration int main() for (int i=-10 ; i(z10; 1++) {//for each # cout<<"("<< ǐ<<") ^5. "<<PowerFive(1)くくendl;// calling the defined Function int PowerFive (int a) //Function definition return a*a*a*a*a;

• ### One dimensional array What this code print #include <iostream> using namespace std; int main () {...

One dimensional array What this code print #include <iostream> using namespace std; int main () { const int SIZE = 7; int numbers [SIZE] = {1, 2, 4, 8): // Initialize first 4 elements cout << Here are the contents of the array:\n"; for (int index = 0; index < SIZE: index++} cout << numbers[index] <<  ; cout << endl; return 0; }

• ### Convert the below code into if else selection: #include <iostream> using namespace std; int main() {...

Convert the below code into if else selection: #include <iostream> using namespace std; int main() { int num; sin. >> num; switch (num) { case 1: cout << "Casel: Value is: << num << endl; break; case 2: break; case 3: cout << "Case3: Value is: " << num << endl; break; default: cout << "Default: Value is: << num << endl; break; } return; }

Free Homework App