Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. #include <stdexcept>
  5.  
  6. std::vector<int> VektorProstihFaktora (int n) {
  7. std::vector<int> Vektor_Prostih_Faktora;
  8. int pomocna=n;
  9. while(pomocna%2==0) {
  10. Vektor_Prostih_Faktora.push_back(2);
  11. pomocna=pomocna/2;
  12. }
  13. for(int i=3; i<=sqrt(pomocna); i=i+2) {
  14. while(pomocna%i==0) {
  15. Vektor_Prostih_Faktora.push_back(i);
  16. pomocna=pomocna/i;
  17. }
  18. }
  19. if(pomocna>2) {
  20. Vektor_Prostih_Faktora.push_back(pomocna);
  21. }
  22. return Vektor_Prostih_Faktora;
  23. }
  24.  
  25. void RastavaBroja(int n, int p, int q) {
  26. if(n<=0) throw std::domain_error ("Broj koji se rastavlja mora biti prirodan");
  27. std::vector<int> Vektor_Prostih_Faktora=VektorProstihFaktora(n);
  28. std::vector<int> Vektor_Prostih_Brojeva={2,3,5,7,11,13,17};
  29. int brojac=0;
  30. for(int i=0; i<Vektor_Prostih_Brojeva.size(); i++) {
  31. for(int j=0; j<Vektor_Prostih_Faktora.size(); j++) {
  32. if(Vektor_Prostih_Brojeva[i]==Vektor_Prostih_Faktora[j]) brojac++;
  33. }
  34. if(brojac%2==1) p=p*Vektor_Prostih_Brojeva[i];
  35. brojac=0;
  36. }
  37. int broj=n/p;
  38. q=sqrt(broj);
  39. std::cout << p << " " << q;
  40. }
  41.  
  42. int main ()
  43. {
  44. int n;
  45. std::cout << "Unesite prirodan broj: ";
  46. std::cin >> n;
  47. int a=1;
  48. int b=1;
  49. try {
  50. RastavaBroja(n, a, b);
  51. std::cout << a << " " << b;
  52. }
  53. catch(std::domain_error izuzetak) {
  54. std::cout << izuzetak.what();
  55. }
  56. catch(...) {
  57. std::cout << "izuzetak:";
  58. }
  59. return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement