Advertisement
metallaro1980

Numeri Primi

Dec 30th, 2020
1,131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <unistd.h>
  4. #include <cstdio>
  5. #include <fstream>
  6. int main()
  7.  
  8. {
  9.   std::ofstream out1("results.txt");
  10.   long maxn;
  11.   long i;
  12.   long j;
  13.   long fi;
  14.   long *myarray;  
  15.   long cont;
  16.   long ind;
  17.   bool test1 = false;
  18.   bool test3 = false;
  19.  
  20.   std::cout << "Creazione listato numeri primi fino a:";
  21.   std::cin >> maxn;
  22.   ind = 0;
  23.   myarray = new long[maxn];
  24.  
  25.   for (i=0;i<maxn;i++)
  26.   {
  27.    myarray[i]=0;
  28.   }  
  29.  
  30.  
  31.   for(i=2;i<=maxn;i++)
  32.   {
  33.     test1 = false;
  34.     test3 = false;
  35.     cont = 0;
  36.  
  37.     if (i % 1 == 0 )
  38.     {
  39.       test1 = true;
  40.     }
  41.    
  42.     fi = i;
  43.    
  44.     for (j=2;j<=fi;j++)
  45.     {
  46.       if (cont > 1)    
  47.       {
  48.         test3 = false;
  49.         break;
  50.       }    
  51.    
  52.       if (i % j == 0)
  53.       {
  54.        cont += 1;  
  55.       }    
  56.     }
  57.    
  58.     if (cont == 1)
  59.     {
  60.      test3 = true;
  61.     } else {
  62.      test3 = false;
  63.     }
  64.    
  65.    
  66.     if (test1 == true && test3 == true)
  67.     {
  68.      myarray[ind] = i;
  69.      ind += 1;
  70.     }
  71.   }
  72.  
  73.  
  74.   out1 << "Creazione listato numeri primi fino a: " << maxn << std::endl;
  75.   for (i=0;i<maxn;i++)
  76.   {
  77.     if (myarray[i] == 0)
  78.     {
  79.       break;
  80.     }    
  81.     out1 << myarray[i] << std::endl;
  82.   }
  83.  
  84.   out1.close();
  85.  
  86.   return 0;
  87. }  
  88.  
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement