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;
    }

int IsPrime(unsigned long m)

'm' is the parameter of the function isPrime, basically it's the number you want to check if it's prime.

find 2 ^ k
Seeing as you can't use the pow(...) function, you could just use bit shifts.

2 << (k - 1)

2 shift left (k -1 ) will give you 2 to the power of k, where k is an unsigned whole number.

You can right your own pow function like this

int power(int base, int exponent)
{
    int result = base;
    for (int i = 1; i < exponent; i++)
    {
        result *= base;
    }
    return result;
}
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.