fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int prime_local(int n, int m) {
  5. div_t d;
  6. return ((d = div(n, m)).rem == 0) ? 0 : (d.quot < m) ? 1 : prime_local(n, m + 1);
  7. }
  8.  
  9. int prime(int n) { return (n == 1) ? 0 : (n == 2) ? 1 : (prime_local(n, 2)); }
  10.  
  11. int main() {
  12. int n;
  13.  
  14. #if 0
  15. do { printf("n = "); scanf("%d", &n);
  16. } while (n <= 0);
  17. #else
  18. for (n = 2; n < 100; n++)
  19. #endif
  20. printf((prime(n) ? "%d prime number.\n" : "%d not prime\n"), n);
  21. return 0;
  22. }
  23. /* end */
  24.  
Success #stdin #stdout 0.01s 2724KB
stdin
Standard input is empty
stdout
2 prime number.
3 prime number.
4 not prime
5 prime number.
6 not prime
7 prime number.
8 not prime
9 not prime
10 not prime
11 prime number.
12 not prime
13 prime number.
14 not prime
15 not prime
16 not prime
17 prime number.
18 not prime
19 prime number.
20 not prime
21 not prime
22 not prime
23 prime number.
24 not prime
25 not prime
26 not prime
27 not prime
28 not prime
29 prime number.
30 not prime
31 prime number.
32 not prime
33 not prime
34 not prime
35 not prime
36 not prime
37 prime number.
38 not prime
39 not prime
40 not prime
41 prime number.
42 not prime
43 prime number.
44 not prime
45 not prime
46 not prime
47 prime number.
48 not prime
49 not prime
50 not prime
51 not prime
52 not prime
53 prime number.
54 not prime
55 not prime
56 not prime
57 not prime
58 not prime
59 prime number.
60 not prime
61 prime number.
62 not prime
63 not prime
64 not prime
65 not prime
66 not prime
67 prime number.
68 not prime
69 not prime
70 not prime
71 prime number.
72 not prime
73 prime number.
74 not prime
75 not prime
76 not prime
77 not prime
78 not prime
79 prime number.
80 not prime
81 not prime
82 not prime
83 prime number.
84 not prime
85 not prime
86 not prime
87 not prime
88 not prime
89 prime number.
90 not prime
91 not prime
92 not prime
93 not prime
94 not prime
95 not prime
96 not prime
97 prime number.
98 not prime
99 not prime