Advertisement
Patey

Untitled

Apr 14th, 2021
574
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int st[100], vf,st2[100],r=-1,i;
  5. int nr,b;
  6.  
  7. void Init(int k)
  8. {
  9.     st[k] = 0;
  10. }
  11.  
  12. int Succesor(int k) {
  13.     if (st[k] < nr)
  14.     {
  15.         st[k]++;
  16.         return 1;
  17.     }
  18.     else
  19.     {
  20.         return 0;
  21.     }
  22. }
  23.  
  24. int Valid(k) {
  25.     for (i = 1; i < k; i++)
  26.     {
  27.         if (st[i] == st[k])
  28.             return 0;
  29.     }
  30.     return 1;
  31. }
  32.  
  33. int Solution(int k)
  34. {
  35.     return (k == nr);
  36. }
  37.  
  38. int prim(int k)
  39. {
  40.     int d, ok = 1;
  41.     for (d = 2; d <= k / 2; d++)
  42.     {
  43.         if (k%d == 0)
  44.             ok = 0;
  45.     }
  46.     return ok;
  47. }
  48.  
  49. void Back() {
  50.     int isS, isV, x, aux, a[10], co, p = 1, co1 = 1;
  51.     for (x = 2; x < b; x++)
  52.     {
  53.         nr = 0;
  54.         a[1] = 0;
  55.         a[2] = 0;
  56.         a[3] = 0;
  57.         a[4] = 0;
  58.         a[5] = 0;
  59.         a[6] = 0;
  60.         aux = x;
  61.         co = x;
  62.         while (aux > 0)
  63.         {
  64.             nr++;
  65.             a[nr] = aux % 10;
  66.             aux = aux / 10;
  67.         }
  68.         vf = 1;
  69.         Init(vf);
  70.         while (vf > 0)
  71.         {
  72.             isS = 0;
  73.             isV = 0;
  74.             if (vf <= nr)
  75.             {
  76.                 do {
  77.                     isS = Succesor(vf);
  78.                     if (isS)
  79.                     {
  80.                         isV = Valid(vf);
  81.                     }
  82.                 } while (isS && !isV);
  83.             }
  84.             if (isS)
  85.             {
  86.                 if (Solution(vf))
  87.                 {
  88.                     if (co == co1)
  89.                     {
  90.                         aux = 0;
  91.                         if (p == 1) {
  92.                             for (i = 1; i <= nr; i++)
  93.                                 aux = aux * 10 + a[st[i]];
  94.                             p = prim(aux);
  95.                         }
  96.                     }
  97.                     else
  98.                     {
  99.                         if (p == 1 && co1 != 1)
  100.                         {
  101.                             r++;
  102.                             st2[r] = co1;
  103.                         }
  104.                         co1 = co;
  105.                         p = 1;
  106.                         aux = 0;
  107.                         for (i = 1; i <= nr; i++)
  108.                             aux = aux * 10 + a[st[i]];
  109.                         p = prim(aux);
  110.                     }
  111.                 }
  112.                 else
  113.                 {
  114.                     vf++;
  115.                     Init(vf);
  116.                 }
  117.             }
  118.             else
  119.             {
  120.                 vf--;
  121.             }
  122.         }
  123.     }
  124. }
  125.  
  126. void main()
  127. {
  128.     FILE *intrare = fopen("in.txt", "rt");
  129.     FILE *iesire = fopen("out.txt", "wt");
  130.     fscanf(intrare, "%d", &b);
  131.     Back();
  132.     for(i=0;i<=r;i++)
  133.         fprintf(iesire, "%d\n", st2[i]);
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement