]> mj.ucw.cz Git - misc.git/blob - prime.c
New utility for monitoring battery status.
[misc.git] / prime.c
1 /*
2 Generate Prime Number
3 */
4
5 int isprime(unsigned x)
6 {
7 unsigned z,y;
8         z = x%6U;
9         if (z!=1 && z!=5) return 0;
10         z = 5;
11         for (;;)
12                 {
13                 y = x / z;
14                 if (y < z || z == x) return 1;
15                 if (!(x % z)) return 0;
16                 z += 2;
17                 }
18 }
19
20 void calcprim(unsigned c)
21 {
22         while (c >= 2)
23                 {
24                 if (isprime(c))
25                         {
26                         printf("Found prime number %u.\n",c);
27                         exit(0);
28                         }
29                 c--;
30                 }
31         printf("Prime number not found!\n");
32         exit(1);
33 }
34
35 void main(int argc,char **argv)
36 {
37 int c;
38 char *k;
39         if (argc != 2)
40                 {
41                 printf("Usage: prime <number>\n");
42                 exit(1);
43                 }
44         c = strtoul(argv[1], &k, 0);
45         calcprim(c);
46 }