Hi, friends!
I got a serious problems... Tomorrow I need to send this assignment to my professor. I tried a lot of hours but still no useful results.. :( You guys are my last hope :(!

There is an integer n.
Find numbers that:
*can be expressed in the form 2^k-1 (http://en.wikipedia.org/wiki/Mersenne_prime#List_of_known_Mersenne_primes)
*and are less than n
*and are prime.

Please try to help me to solve it! This is very important to me! :(
Here is what I got:

#include <iostream>
#include <math.h>
using namespace std;

int main()

{
  int OKZ;
  int n = 0;
do
{
    cout << "INPUT n: " << endl;
    cin >> n; cout << endl;
    for (int k=0; k<n; ++k) // Part of program that generates integers of the form 2^k-1  
    {
int n=1; // subtract one
for (int i=0; i<k; i++)
    {
    n=n*2;
    }
    n=n-1; // subtract one
    }
    bool OK = true;
    for(int i = 0; i <= n; i++)
    {
        for(int j = 2; j <= n; j++)
        {
            if (i!=j && i % j == 0 )
                {
                OK=false;
                break;
                }
        }
        if (OK)
        {
            cout << i << endl;
        }
        OK = true;
    }
cout << " continue (1) or end program (0)?" << endl;
cin >> OKZ; //
    }
while (OKZ == 1);
return 0;
}

And here is a part to see whether or not the value is a prime number:

bool isprime(unsigned int n)
{
    if (n<2)
        return false;

    for (unsigned int j = 2; j < n; j++)
    {
        if (n % j == 0 )
            return false;
    }
    return true;
}

Edited 4 Years Ago by Growl: Mistake

So what is the problem you are having? If you want to generate all numbers less than n that equall 2^k-1 than you can do this

for(int k = 0; k < (int)pow((double)2,k) - 1; k++
{
    // check if (int)pow((double)2,k) - 1 is prime
}

This is how it compile:

IF n=12
PROGRAM PRINTS:
1
2
3
5
7
11

IF n=7
PROGRAM PRINTS:
1
2
3
5
7

IF n= -1 [ IF cin numbers in interval (-∞; 0] ]
PROGRAM PRINTS:
" " [empty space]

Ja n=1
PROGRAM PRINTS:
0
1

So is it true that 1 is prime? I know that 0 isn't but it prints out if I write 1,.. so something is not right... :(

And the main problem is that it print primes but I need Mersenne primes.

I have a problems to combine these parts into the one program:

#include <iostream>
#include <math.h>
using namespace std;

void p(int);
int main()
{
    cout << "INT n! " << endl;
    cout << endl;
    int n;
    cin >> n;
    cout << endl;
    cout << endl;
    p(n);
}
void p(int n)
{
    bool IR = true;
    for(int i = 0; i <= n; i++)
    {
        for(int j = 2; j <= n; j++)
        {
            if ( i!=j && i % j == 0 )
            {
                IR=false;
                break;
            }
        }
        if (IR)
{
cout << i << endl;
}
IR = true;
}
}

2

#include <iostream>

    using namespace std;

int main()
{
    // 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;
    }

    return 0;
}

Edited 4 Years Ago by Growl: Mistake

Technicly 1 is not prime so all you need to do is in your prime function check to see if it was passed 1 and if it was just exit the function.

This article has been dead for over six months. Start a new discussion instead.