Question

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?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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;
}

Add a comment
Know the answer?
Add Answer to:
How do can I update this code (Code A): Code (A) #include using namespace std; int fibonacci(int n) { int a = 0,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT