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,...
#include <iostream> using namespace std; int main() { int i,n; int counter=0; cin >> n; for (i = 1; i <= n; i++) { if (n % i == 0 ) { counter++; } /*else { counter++; i++; }*/ } if (counter == 2) cout << n <<...
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
#include <iostream> uisng namespace std; int main() { cout<<"Enter your three test scores and I will "; <<" average them: "; int score1, score2, score3, cin>>score1>>score2>>score3; double average; average = (score1 + score2 + score3) / 3.0; if (average = 100); perfectScore = true; // set the flag variable cout<<"Your average is "<<average <<endl; bool perfectScore; if (perfectScore); { cout<<"Congratulations!\n"; cout<<"That's a perfect score.\n"; ...
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) {...
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 <stack> #include <queue> using namespace std; void printFromStack(string expr){ stack<char> myStack; for(int i=0; i<expr.length(); i++){ //Insert code here to push each character onto the stack } cout << "My stack is popped in this order" << endl; while(!myStack.empty()){ //Insert code here to cout the top of the stack one by one //Pop each one after it’s printed out } cout << endl; } void printFromQueue(string expr){ queue<char> myQueue; //Insert code here to push each character onto the...