#include <stdio.h>
#include <stdlib.h>
#include <sys/timeb.h>
int main() {
int n=10000000, checkNum, total=1, i, j, found;
struct timeb begin, end;
int primes[1000000];
printf("Please input an integer:");
scanf("%d", &n);
if (n <= 1) {
printf("Please input an integer larger than 1.\n");
return 1;
}
ftime(&begin);
primes[0] = 2;
j = 0;
for (checkNum = 3; checkNum <= n; checkNum++) {
found = 1;
for (i = 0; i <= j; i++) {
if (checkNum%primes[i] == 0) {
found = 0;
break;
}
}
if (found) {
primes[total++] = checkNum;
if (primes[j]*primes[j] <= checkNum) {
j++;
}
}
}
ftime(&end);
printf("There are %d primes less than or equal %d, used %dms\n", total, n, end.time*1000+end.millitm-begin.time*1000-begin.millitm);
}
#include <stdio.h>
#include <stdlib.h>
#include <sys/timeb.h>
int main() {
unsigned int n=10000000, checkNum, multiple, total;
struct timeb begin, end;
unsigned char *dividable;
printf("Please input an integer:");
scanf("%ud", &n);
ftime(&begin);
dividable = (unsigned char *)calloc((n+7)/8,1);
if (dividable == NULL) {
printf("No enought memory.\n");
return;
}
total = 0;
for (checkNum = 2; checkNum <= n; checkNum++) {
if (!(dividable[checkNum/8] & 1 << checkNum % 8)) {
total++;
for (multiple = checkNum << 1; multiple <= n; multiple += checkNum) {
dividable[multiple/8] |= 1 << multiple % 8;
}
}
}
ftime(&end);
printf("There are %d primes less than or equal %d, used %dms\n", total, n, end.time*1000+end.millitm-begin.time*1000-begin.millitm);
}