Actually, the above suggestion is not
quite right.
Try this and see, there is no output:
1 2 3 4
|
for (int k = 0; k < (int)pow((double)2,k) - 1; k++)
{
cout << "k = " << k << endl;
}
|
But still, it has some of the right ingredients.
Re-arrange it like this, and you might be on to something.
1 2 3 4 5 6
|
for (int k = 0; k < 32; k++)
{
int m = (int)pow((double)2,k) - 1;
// check if m is prime
cout << "k = " << k << " m = " << m << endl;
}
|
In fact the values output from the above are the same as that from the code I posted six days ago:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
// 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 = " << n << endl;
}
|
All you need to do is to test whether the number is prime before outputting it. And in addition, a very simple check, make sure that it is less than some limit specified by input from the user.
If you're not sure how to check whether the number is prime, please see the
isprime()
function which I posted a few days ago.
I don't know any more. There's a saying, you can lead a horse to water, but you can't make it drink. All the answers are right there in front of your eyes. Just open them for a brief moment and look. Please. This is driving me crazy.
I also offer my apologies for seemingly having poisoned this thread, since no-one else seems willing to dip their toe in the water and join in.