MERSENNE PRIMES
There is an integer n. Find primes that is < than n (p < n) and they can be expressed in the form 2^k-1.
Hey!
I'm done with connecting parts together - all works fine! But I need to replace
(pow(2,p)-1)
because my professor has forbidden
pow
...
BTW what does m means?
Actually I don't really understand this part (that works (can somebody explain what does this part do?)):
Misunderstooded part
#include <iostream>
#include <math.h>
using namespace std;
int IsPrime(unsigned long m)
{
for(unsigned long p=2;p<m/2;p++)
{
if(m%p==0)
return 0;
}
return 1;
}
n - integer
p - prime number (if cout << p it prints list of primes
m - [???]
#include <iostream>
#include <math.h>
using namespace std;
int IsPrime(unsigned long m)
{
for(unsigned long p=2;p<m/2;p++)
{
if(m%p==0)
return 0;
}
return 1;
}
int main()
{
int oky;
do{
int n=0;
cout << endl << "<<<<< Input integer n: >>>>> " << endl;
cin >> n;
for(unsigned long p=2;p<20;p++)
{
if (((pow(2,p)-1)<n) && IsPrime(pow(2,p)-1))
cout<<endl<<pow(2,p)-1;
}
cout << endl << endl << " continue (1) end (0)?" << endl;
cin >> oky;}
while (oky == 1);
return 0;
}
BTW program do not work as before If I replace pow with this
for(unsigned long p=2;p<20;p++)
{
if ((((2^p)-1)<n) && IsPrime((2^p)-1))
cout<<endl<<((2^p)-1);
}
This part looks fine but how can I shorten this part?
I want to replace this
for(unsigned long p=2;p<20;p++)
{
if (((pow(2,p)-1)<n) && IsPrime(pow(2,p)-1))
cout<<endl<<pow(2,p)-1;
}
To this:
// Generate integers of the form 2^k-1
for (int k=0; k<32; ++k)
{
int n = 1; // find 2 ^ k
for (int i=0; i<k; i++)
{
n = n * 2;
}
n = n - 1; // subtract one
cout << n << endl;
}