Code: Alles auswählen
#include <stdio.h>
int main()
{
const int IntBits = sizeof(int) * 8;
int Zahl;
int i;
int j=0;
printf("\nBitte geben Sie eine Zahl ein!\n");
scanf("%i", &Zahl);
printf("Primzahlen von 2 bis %i:\n\n", Zahl);
unsigned int *Sieb;
Sieb = (unsigned int *) malloc(((Zahl) / IntBits + 1) * sizeof(int));
for (i=0; i <= Zahl / IntBits; i++) Sieb[i]=0;
for (i=2; i <= Zahl; i++)
{ if ((Sieb[i / IntBits] & (1<<(i % IntBits))) == 0)
{ printf("%i, ", i);
for (j=i; j<=Zahl; j+=i) Sieb[j / IntBits] |= (1 << (j % IntBits));
}
}
printf("\n");
return 0;
}