I know you have heard of this scenario. How could I turn this program into Fibonacci prime is it is the exact layout to display the numbers.
I need them to display like this: n Fibonacci Prime
== ===============
1 2
2 3
3 5
4 13
5 89
6 1597
7 28657
8 514229
9 433494437
And I have this format my best friend gave me to work with. I would be ever so appreciative as I am at the hospital with my gf having a baby and do not have time to finish before deadline. My grade depends on this sole project alone and I cannot sit down to do it this week. I know this is frowned upon but it is a unique scenario.
I need to turn Mersenne Prime into Fibinacci Primes before Midnight. PLEASE HELP!
Here is what I have to work with:
#include <iostream>
#include <iomanip>
#include <string>
usingnamespace std;
// Next are my two function prototypes.
// The first function is used to find out if a number is a prime number.
// The second function is used to calculate 2^n.
bool isPrime(long n);
long power2(long n);
int main ()
{
string reply;
cout << "Mersenne Primes by " << endl;
cout << setw(2) << "n" << setw(28) << "Mersenne Prime" << endl;
cout << setw(2) << "==" << setw(28) << "==============";
cout << setfill(' ') << endl << endl;
// Next, start looking for Mersenne Primes. Look at values for x starting at 2
// and incrementing by one until the output of ((2^x)-1) is <= 1 million (that is
// the value specified in the assignment). If the number (x) is prime and the value
// when x is put into the equation ((2^x)-1)is also prime, then the program outputs
// both the x values and the Mersenne Prime number.
for (long x = 2; (power2(x)-1) <= 10000000; x++){
if (isPrime((power2(x))-1))
cout << setw(2)<< x << setw(28) << (power2(x)-1) << endl;
}
// The user decides when to quit the program.
cout << endl << "Press q (or any other key) followed by 'Enter' to quit: ";
getline(cin, reply);
return (0);
}
// Here is my function for finding a prime number. It takes in 'n' and tests for remainders.
bool isPrime(long n) {
if (n <= 1)
returnfalse;
int i;
for (i = 2; i <= n/2; i++){
if (n % i == 0)
returnfalse;
}
returntrue;
}
// Here is my function that returns the value of (2 to the power of n), when 'n' is given.
long power2(long n){
long power = 2;
for(long e = 1; e < n; e++)
power = power * 2;
return power;
}
Here's code for Fibonacci Prime numbers. If we go beyond 10, long long types are required for 64 bits. Beyond 11, faster prime functions or faster computers are required.
#include <iostream>
#include <iomanip>
bool isPrime(longlong n) {
if (n <= 1)
returnfalse;
longlong i;
for (i = 2; i <= n/2; i++){
if (n % i == 0)
returnfalse;
}
returntrue;
}
int main()
{
longlong nb_outputs, Fibonacci, last_value = 1, last_last_value = 1;
std::cout << "How far do you want to go? : ";
std::cin >> nb_outputs;
std::cout << " n Fibonacci Prime\n== ===============\n";
for ( int i = 1; i <= nb_outputs; ++i)
{
do
{
Fibonacci = last_value + last_last_value;
last_last_value = last_value;
last_value = Fibonacci;
} while ( !isPrime(Fibonacci) );
std::cout << std::setw(2) << i << ' ' << Fibonacci << std::endl;
}
return 0;
}
How far do you want to go? : 11
n Fibonacci Prime
== ===============
1 2
2 3
3 5
4 13
5 89
6 233
7 1597
8 28657
9 514229
10 433494437
11 2971215073
Press any key to continue . . .