Dieser Thread ist zwar schon etwas^^ älter, aber ich gehe mal davon aus, dass man die Programmieraufgaben jederzeit hochholen kann, da sie ja sozusagen nie gelöst sind, jedenfalls verstehe ich das so
Also hier meine recht schnelle Lösung, da mit dem Satz des Erastotenes:
Code:
#include <iostream>
using namespace std;
int main()
{
int max;
cout << " Primpaare " << endl
<< "---------------" << endl;
do {
cout << "Max: ";
cin >> max;
} while(max < 0 || max > 1000000);
bool *isPrime = new bool[max];
int lastPrime = -1;
// Define every number (ex. 0+1) as true
for(int i = 2; i < max; i++)
{
isPrime[i] = true;
}
for(int i = 2; i < max; i++)
{
if(isPrime[i])
{
// remove multiples
for(int k = i * 2; k < max; k += i)
{
isPrime[k] = false;
}
if (lastPrime + 2 == i)
{
cout << lastPrime << ", " << i << endl;
}
lastPrime = i;
}
}
delete[] isPrime;
}