Advertisement
amermo

djelioci, prosti faktori, prosti blizanci, prosti brojevi

Mar 1st, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. bool Prost(int broj)
  5. {
  6.     int counter(0);
  7.     for(int i = 1; i<=broj; i++)
  8.     {
  9.         if(broj % i == 0)
  10.             counter++;
  11.     }
  12.     if(counter == 2)
  13.         return true;
  14.     return false;
  15. }
  16.  
  17. std::vector<int> ProstiBrojevi(int n)
  18. {
  19.     int counter(0), pocetni(2);
  20.     std::vector<int> rez;
  21.     while(counter < n)
  22.     {
  23.         if(Prost(pocetni))
  24.         {
  25.             rez.push_back(pocetni);
  26.             counter++;
  27.         }
  28.         pocetni++;
  29.     }
  30.     return rez;
  31. }
  32.  
  33. std::vector<int> ProstiBrojeviBlizanci(int n)
  34. {
  35.     int counter(0), pocetni(2);
  36.     std::vector<int> rez;
  37.     while(counter < n)
  38.     {
  39.         if(Prost(pocetni) && Prost(pocetni+2))
  40.         {
  41.             rez.push_back(pocetni);
  42.             rez.push_back(pocetni+2);
  43.             counter++;
  44.             pocetni+=2;
  45.         }
  46.         pocetni++;
  47.     }
  48.     return rez;
  49. }
  50.  
  51. std::vector<int> ProstiFaktori(int n)
  52. {
  53.     std::vector<int> rez;
  54.     if(n == 1)
  55.         rez.push_back(n);
  56.     else
  57.     {
  58.         int a(n), b;
  59.         int c(a%2);
  60.         while(c == 0)
  61.         {
  62.             b = a/2;
  63.             rez.push_back(2);
  64.             a = b;
  65.             c = a%2;
  66.         }
  67.         for(int i(3); i<=a; i=i+2)
  68.         {
  69.             int c(a%i);
  70.             while(c == 0)
  71.             {
  72.                 b = a/i;
  73.                 rez.push_back(i);
  74.                 a = b;
  75.                 c = a%i;
  76.             }
  77.         }
  78.     }
  79.     return rez;
  80. }
  81.  
  82. std::vector<int> Djelioci(int n)
  83. {
  84.     std::vector<int> rez;
  85.     for(int i(1); i<=n; i++)
  86.     {
  87.         if(n % i == 0)
  88.             rez.push_back(i);
  89.     }
  90.     return rez;
  91. }
  92.  
  93. int main()
  94. {
  95.     std::vector<int> v1 = ProstiBrojevi(5);
  96.     for(unsigned int i(0); i < v1.size(); i++)
  97.         std::cout << v1[i] << " ";
  98.     std::cout << std::endl;
  99.     v1 = ProstiBrojeviBlizanci(3);
  100.     for(unsigned int i(0); i < v1.size(); i++)
  101.         std::cout << v1[i] << " ";
  102.     std::cout << std::endl;
  103.     v1 = ProstiFaktori(178);
  104.     for(unsigned int i(0); i < v1.size(); i++)
  105.         std::cout << v1[i] << " ";
  106.     std::cout << std::endl;
  107.     v1 = Djelioci(81);
  108.     for(unsigned int i(0); i < v1.size(); i++)
  109.         std::cout << v1[i] << " ";
  110.     std::cout << std::endl;
  111.     return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement