13
Compute prime numbers
- #include <math.h>
- int isprime(int n) {
- double sn;
- int d;
- if (n < 2)
- return 0;
- if (n == 2)
- return 1;
- if (n % 2 == 0)
- return 0;
- sn = sqrt(n);
- for (d = 3; d <= sn; d += 2)
- if (n % d == 0)
- return 0;
- return 1;
- }
- #include <stdio.h>
- int main() {
- int n;
- for (n = 1; n < 100; n++)
- if (isprime(n) )
- printf("%d ", n);
- printf("\n");
- }
$ gcc -o prime prime.c -lm
$ ./prime
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Comments