fb

fgth
Last edited on
please
Original post:

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <iomanip>
#include <string>


using namespace 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)
		return false;
	int i; 
	for (i = 2; i <= n/2; i++){
		if (n % i == 0)
			return false;
	}
	return true;
}
// 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;
}
Last edited on
Thank you!!
Last edited on
I wasnt lying!!!
I deleted on my phone because I didnt see anyone responded, then I saw you posted this!!!
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <iomanip>

bool isPrime(long long n) {
  if (n <= 1)
    return false;
  long long i; 
  for (i = 2; i <= n/2; i++){
    if (n % i == 0)
      return false;
  }
  return true;
}

int main()
{
  long long 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 . . .
Last edited on
Regarding your deleted posts (which you also did on your last thread) check out this article:
http://cplusplus.com/articles/oGLN8vqX/
^Makes perfect sense. I never saw this so continued to do this. Was my worries exactly.
Topic archived. No new replies allowed.