My recent assignment is to find Mersenne Primes between 2 and 1,000,000. For the first part of my code, I have a function to find out if a number is prime or not. The second part of my program runs a Lucas-Lehmer test to find the Mersenne number.

My problems are many: How do I get the prime numbers in the first part of my program to loop through the second part, then display as I've formatted in my final cout statements?

This is CS1 so I'm sure the instructor is trying to weed out the less dedicated. This is my nth version, I can't do it without help anymore!

Thanks

#include <iostream>
#include <cmath>
#include <iomanip>
 
using namespace std;
 
const int LIM = 19;
long power2 (long n);
bool isPrime(int n);
 
int main()
{
     int s,n,i;
     long p; //p is an odd prime  
     int num;
     int mPrimes;
 
 
     while (num >= 1 && num <=LIM)
     {
          if (num%2!=0 && num%3 !=0 && num%5 !=0)
          {
          num = p;
          num+=2;
 
          }
     }
 
//Mersenne calculations. 
 
 s=4; //s is the Lucas-Lehmer number where  s(0)=4
 p+=2;
 n=2;
 for (i=1 ; i<p ; i++) //This for loop assigns to n the value (2^p)-1.
 {
  n=n*2;  
 }
 n=n-1;
 for (i=3 ; i<=(p) ; i++)  //Tests this many times to see if s%n==0
 {
  s=s*s;
  s-=2;
  s=s%n;
  if (s==0);
           num == mPrimes;
    } 
 
 
cout << "Mersenne Primes by Sheila Benware" << endl;
cout << setw (2) << "n" << setw (21) << "Mercenne Prime" << endl;
cout << setw (2) << "==" << setw (21) << "=============="
<< endl;
cout << setfill (' ');
cout << setw (2) << p <<setw (21) << mPrimes <<endl;
 
        char reply;
        cout << "Press q to quit: ";
        cin >> reply;
        return 0;
}

Recommended Answers

All 2 Replies

> I have a function to find out if a number is prime or not.
Which isn't working.

> bool isPrime(int n);
Where is your implementation of this function?

This is step 1

bool isPrime(int n);
int main ( ) {
  for ( int i = 1 ; i <= LIM ; i++ ) {
    if ( isPrime(i) ) {
      cout << i << " is prime" << endl;
    }
  }
  return 0;
}

bool isPrime(int n) {
  // add your code here
  return true;
}

WHEN that works, then you can expand it to

bool isPrime(int n);
bool isMersenne(int n);
 int main ( ) {
  for ( int i = 1 ; i <= LIM ; i++ ) {
    if ( isPrime(i) && isMersenne(i) ) {
      cout << i << " is Mersenne prime" << endl;
    }
  }
  return 0;
}

bool isPrime(int n) {
  // add your code here
  return true;
}

bool isMersenne(int n) {
   // add your code here
   return true;
 }

Thank you so much. I consulted with a tutor who asked the same thing: Where is the function? I figured it out from there. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.