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;
}
How do can I update this code (Code A): Code (A) #include using namespace std; int fibonacci(int n) { int a = 0,...
Add comments on this Fibonacci C++ program in detail. #include <iostream> using namespace std; int fib(int n){ int a = 0,b = 1,c; if(n == 0 || n == 1){ return n; } while(n>2){ c = b + a; a = b; b = c; n--; } return c; } int main(){ int n; cout<<"Enter n: "; cin>>n; int result = fib(n); cout<<"Fib("<<n<<") = "<<result<<endl; return 0; }
what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...
#include <iostream> #include <string> using namespace std; int main() { int number; int sum = 0; while(true) { cout << "Please enter a number between 1 and 11: "; cin >> number; if (number >= 1 && number <= 11) { cout << number << endl; sum = sum + number; //only add the sum when number is in range: 1-11, so add wthin this if case } else { cout << number << endl; cout << "Out of range;...
#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...
#include<iostream> using namespace std; double hey(int x){ if(x==2) return 1; int i=0; if (x%2==0){ while(i<=2){ cout<<(x/2)<<endl; i++; break; } } if (x%2==1){ while(i<=2){ i=3*x+1; cout<<i<<endl; } return 1; } } int main() { int n; cout<<"insert a number to check how many steps to reduce down to 2"<<endl; cin>>n; cout<<hey(n)<<endl; return 0; } I couldn't
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;...
#include <iostream> #include <chrono> using namespace std; double improvedPow(double x, int y) { // To be implemented by you } int main() { cout << "To calculate x^y ..." << endl; double x; int y; cout << "Please enter x: "; cin >> x; cout << "Please enter y: "; cin >> y; if(x == 0) { if (y > 0) cout << 0 << endl; else cout << "x^y is not defined" <<endl; } else { cout << improvedPow(x,y)...
#include <iostream> using namespace std; int main() { int sumOdd = 0; // For accumulating odd numbers, init to 0 int sumEven = 0; // For accumulating even numbers, init to 0 int upperbound; // Sum from 1 to this upperbound // Prompt user for an upperbound cout << "Enter the upperbound: "; cin >> upperbound; // Use a loop to repeatedly add 1, 2, 3,..., up to upperbound int number =...
c++ #include using namespace std; int main() { int n, x, num, j = 0; cin >> n >> x; int*arr = new int[n]; for (int i = 0; i < n; i++) { cin >> num; if (num < x) { arr[j] = num; j++; } } for (int i = 0; i < j; i++) {...
Fix this code so only the function prototype comes before main. #include <iostream> using namespace std; bool isMultiple(int num1, int num2) { return num1 % num2 == 0; } int main() { char ch = 'Y'; int num1, num2; while(ch =='Y') // While ch is equal to Y { cout << "Enter two numbers(largest first): "; cin >> num1; // Getting 1st number cin >> num2; // Getting 2nd number if(isMultiple(num1, num2)) cout << num2 << " " << "IS...