hi
can any one help with Mersenne Prime numbers calculator.
A prime number is defined as being a Mersenne Prime if it is of the form 2^n – 1,
where n is any integer greater than 1
display all the Mersenne primes from 2 through 1,000,000. Your results should be as presented below, under testing.


2 3
3 7
5 31
7 127
13 8191
17 131071
19 524287

please provide me with some examples

hi
can any one help with Mersenne Prime numbers calculator.
A prime number is defined as being a Mersenne Prime if it is of the form 2^n – 1,
where n is any integer greater than 1
display all the Mersenne primes from 2 through 1,000,000. Your results should be as presented below, under testing.


2 3
3 7
5 31
7 127
13 8191
17 131071
19 524287

please provide me with some examples

Here is the your solution :

#include <iostream.h>
#include <math.h>

//using namespace std;

int isprime(unsigned long n)
{
   for(unsigned long i=2;i<n/2;i++)
   {
   if(n%i==0)
   return 0;
   }
   return 1;

}

int main()
{
    for(unsigned long i=2;i<1000;i++)
    {

    if (isprime(pow(2,i)-1))
    cout<<endl<<i<<'\t'<<pow(2,i)-1;

    }

    return 0;
} //end main

that didn't work plz help the assignment due today.

See, first of all we, in this community are there to help you not because you have paid us or we are servents or something.
You cant just say "it is not working". You need to specify what is the errors shown by the compiler or what is the undesired in the output.

Here on my machine i am having exactly the output you requested. But if you are not getting it, then you must tell us what is the problem.

Again I am giving you the program:

#include <iostream.h>
#include <math.h>

//using namespace std;

int isprime(unsigned long n)
{
   for(unsigned long i=2;i<n/2;i++)
   {
   if(n%i==0)
   return 0;
   }
   return 1;

}

int main()
{
    for(unsigned long i=2;i<1000;i++)
    {

    if (isprime(pow(2,i)-1))
    cout<<endl<<i<<'\t'<<pow(2,i)-1;

    }

    return 0;
} //end main

See, first of all we, in this community are there to help you not because you have paid us or we are servents or something.
You cant just say "it is not working". You need to specify what is the errors shown by the compiler or what is the undesired in the output.

Here on my machine i am having exactly the output you requested. But if you are not getting it, then you must tell us what is the problem.

Again I am giving you the program:

#include <iostream.h>
#include <math.h>

//using namespace std;

int isprime(unsigned long n)
{
   for(unsigned long i=2;i<n/2;i++)
   {
   if(n%i==0)
   return 0;
   }
   return 1;

}

int main()
{
    for(unsigned long i=2;i<1000;i++)
    {

    if (isprime(pow(2,i)-1))
    cout<<endl<<i<<'\t'<<pow(2,i)-1;

    }

    return 0;
} //end main

That's wrong.:D

i am really sorry.
maybe it is working in other compiler but i'm using visual studio. and the message is very long
the instructor advised us to use prototypes

bool isPrime (long n);
// Returns true if n is a prime, else false

long power2 (long n);
// Returns 2 raised to the power of n.
// Remember you need to use integer arithmetic throughout…
// so you cannot use the built-in C++ pow() function.

i really appreciate your help.

TRY THIS OUT

#include <iostream.h>
#include <conio.h>


int isPrime(unsigned long);
unsigned long power2( long);

void main()
{ clrscr();
for(int i=2;i<20;i++)
{
unsigned long val=power2(i) -1;

if (isPrime(val)==1)
{
cout<<i<<'\t'<<val<<'\n';

}

}


} //end main


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

}

unsigned long power2(long n)
{
unsigned long r=1;
for(int i=0;i<n;i++)
r*=2;

return r;

}

ok.
Maybe your right.
Actually I too was frustated as dese ppl dont want to work. They just are trying to exploit the service.
I will remember this.
Thankx

ops, you misunderstood me. i almost finished my program. but the problem is number 11 is counted as prime number and the example given doesn't include 11.
i didn't even try to just copy the code that you gave me.
here is my program please help me with it:

//Meserene primes (assingment no.5)
//written by: Gehad Shaat
//Date: 03 november 2007
//sources: none
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
bool isPrime (long n);
long power2 (long n);
double k;
double merPrime;
bool isPrime(long n)
{
  bool isPrime=true;
				for ( long i = 2; i < n; i++)
				{
					if ( n % i == 0 )
					{
						isPrime = false;
						return 0;
					}
				}
				//return n;
				if (isPrime)
				{
					k=n;
					merPrime = pow(2,k)-1;
				    cout << n <<setw(20)<< merPrime << endl;
				}
			isPrime=true;
			return isPrime;
}
int main ( ) 
{
	cout << "Meserene primes by Gehad " << endl ;
    cout << "n" << setw(20) << "Meserene primes " << endl ;
    cout << "=" << setw(20) << "=============== " << endl ;
	for ( long n = 2 ; n <= 19 ; n++ ) 
	{
		isPrime(n);
		//cout << n << endl;
	}
	char reply;
    cout << "Enter q to quit: ";
    cin >> reply;
    return 0;
}
This article has been dead for over six months. Start a new discussion instead.