Hallo!
Ich will mich auch nochmal des Problems 10 annehmen. Allerdings mit dem Atkins Sieb, wie auf Seite 1 beschrieben. Leider bekomme ich nicht das richtige Ergebnis und finde den Fehler nicht. Mein Ergebnis:
Anzahl: 3676 Laufzeit: 0.14 Sekunden
Wikipedia über das Atkins Sieb:
http://en.wikipedia.org/wiki/Sieve_of_Atkin
Der Code:
Code:
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <time.h>
using namespace std;
int main() {
int zaehler=3;
//unsigned long long sum=2;
bool is_prime[1000000];
is_prime[0] = 2;
is_prime[1] = 3;
is_prime[2] = 5;
unsigned long long n;
double time1=clock(), time2;
int limit=1000000;
double SQRTlimit= sqrt(1000000);
for (int x=1;x<SQRTlimit;x++)
{
for (int y=1;y<SQRTlimit;y++) //Das Feld X*Y wird bis zur Wurzel des Suchlimits abgearbeitet
{
n=4*(x^2)+(y^2);
if (n <= limit)
if (n%12 == 1 || n%12 == 5) is_prime[n]=true;
n=3*(x^2)+(y^2);
if (n <= limit)
if (n%12 == 7) is_prime[n]=true;
n=3*(x^2)-(y^2);
if (x>y)
if (n <= limit)
if (n%12 == 11) is_prime[n]=true;
}
}
int k;
int i,x;
for (i=5;i<=SQRTlimit;i++)
{
if (is_prime[i]==true)
{
for (k=1;k<SQRTlimit;k++) //normalerweise limit, das forciert aber nen Crash
{
x=k*(i^2);
is_prime[x]=false;
}
}
}
for (i=5;i<limit;i++)
if(is_prime[i]) zaehler++;
time2 = (clock() - time1) / CLOCKS_PER_SEC;
cout << "Anzahl: " << zaehler << " Laufzeit: " << time2 << " Sekunden" << endl;// << " Summe: " << sum
system("PAUSE");
return EXIT_SUCCESS;
}
Währe toll, wenn jemand den Wurm findet. Sitze nämlich schon gut 1,5Std daran...
Edit: Hab mal alle Primzahlen unter 100 ausgeben lassen:
5, 11, 13, 17, 23, 25, 29, 37, 41, 47, 53, 59, 61, 65, 71, 73, 79, 83, 85, 89, 97
Die Zahlen 19, 31, 43, 61, 67 fehlen und 25, 65, 85 sind keine Primzahlen!