Advertisement
amermo

Vrati vektor u kojem su prosti faktori broja n

Feb 28th, 2015
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. std::vector<int> ProstiFaktori(int n)
  5. {
  6.     std::vector<int> v;
  7.     if(n == 1)
  8.         v.push_back(n);
  9.     else
  10.     {
  11.         int a(n), b;
  12.         int c(a%2);
  13.         while(c == 0)
  14.         {
  15.             b = a/2;
  16.             v.push_back(2);
  17.             a = b;
  18.             c = a%2;
  19.         }
  20.         for(int i(3); i<=a; i=i+2)
  21.         {
  22.             int c(a%i);
  23.             while(c == 0)
  24.             {
  25.                 b = a/i;
  26.                 v.push_back(i);
  27.                 a = b;
  28.                 c = a%i;
  29.             }
  30.         }
  31.     }
  32.     return v;
  33. }
  34.  
  35. int main()
  36. {
  37.     std::vector<int> test = ProstiFaktori(100);
  38.     for(unsigned int i(0); i < test.size(); i++)
  39.         std::cout << test[i] << " ";
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement