Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/wait.h>
  4. #include <fcntl.h>
  5. #include <sys/stat.h>
  6. #include <unistd.h>
  7. #include <signal.h>
  8.  
  9. enum { COUNT = 4 };
  10. enum { SIMPL = 2 };
  11.  
  12. volatile int cnt = 0;
  13. int sim = 0;
  14.  
  15. void hand_SIGTERM(int sig) {
  16.     _exit(0);
  17. }
  18. void hand_SIGINT(int sig) {
  19.     signal(SIGINT, hand_SIGINT);
  20.     if (++cnt == COUNT) {
  21.         _exit(0);
  22.     }
  23.     printf("%d\n", sim);
  24. }
  25.  
  26. int simpl_check(int val) {
  27.     if (val < 0) {
  28.         return 0;
  29.     }
  30.     for (unsigned long long i = SIMPL; i * i <= val; ++i) {
  31.         if (!(val % i)) {
  32.             return 0;
  33.         }
  34.     }
  35.     return 1;
  36. }
  37.  
  38. int main(int argc, char **argv) {
  39.  
  40.     long long low, high;
  41.     scanf("%lld", &low);
  42.     scanf("%lld", &high);
  43.  
  44.     printf("%d\n", getpid());
  45.  
  46.     signal(SIGTERM, hand_SIGTERM);
  47.     signal(SIGINT, hand_SIGINT);
  48.  
  49.     for (unsigned long long i = low; i < high; ++i) {
  50.         if (simpl_check(i)) {
  51.             sim = i;
  52.         }
  53.     }
  54.     printf("-1\n");
  55.     _exit(0);
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement