-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprosti_brojevi.c
71 lines (59 loc) · 1.12 KB
/
prosti_brojevi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#define _XOPEN_SOURCE
#define _XOPEN_SOURCE_EXTENDED
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/time.h>
#include <math.h>
int pauza = 0;
unsigned long broj = 1000000001;
unsigned long zadnji = 1000000001;
void periodicki_ispis(int sig)
{
printf("%lu\n", zadnji);
}
void postavi_pauzu()
{
pauza = 1 - pauza;
}
void prekini()
{
printf("[SIGTERM] %lu\n", zadnji);
exit(0);
}
int prost(unsigned long n)
{
unsigned long i, max;
if ((n & 1) == 0)
return 0;
max = sqrt(n);
for (i = 3; i <= max; i += 2)
{
if ((n % i) == 0)
{
return 0;
}
}
return 1; // prost
}
int main()
{
sigset(SIGTERM, prekini);
sigset(SIGINT, postavi_pauzu);
struct itimerval t;
sigset(SIGALRM, periodicki_ispis);
t.it_value.tv_sec = 5;
t.it_value.tv_usec = 0;
t.it_interval.tv_sec = 5;
t.it_interval.tv_usec = 0;
setitimer(ITIMER_REAL, &t, NULL);
while (1)
{
if (prost(broj) == 1)
zadnji = broj;
broj++;
while (pauza == 1)
pause();
}
}