Advertisement
Patey

Untitled

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