Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cmath>
- #include <stdexcept>
- std::vector<int> VektorProstihFaktora (int n) {
- std::vector<int> Vektor_Prostih_Faktora;
- int pomocna=n;
- while(pomocna%2==0) {
- Vektor_Prostih_Faktora.push_back(2);
- pomocna=pomocna/2;
- }
- for(int i=3; i<=sqrt(pomocna); i=i+2) {
- while(pomocna%i==0) {
- Vektor_Prostih_Faktora.push_back(i);
- pomocna=pomocna/i;
- }
- }
- if(pomocna>2) {
- Vektor_Prostih_Faktora.push_back(pomocna);
- }
- return Vektor_Prostih_Faktora;
- }
- void RastavaBroja(int n, int p, int q) {
- if(n<=0) throw std::domain_error ("Broj koji se rastavlja mora biti prirodan");
- std::vector<int> Vektor_Prostih_Faktora=VektorProstihFaktora(n);
- std::vector<int> Vektor_Prostih_Brojeva={2,3,5,7,11,13,17};
- int brojac=0;
- for(int i=0; i<Vektor_Prostih_Brojeva.size(); i++) {
- for(int j=0; j<Vektor_Prostih_Faktora.size(); j++) {
- if(Vektor_Prostih_Brojeva[i]==Vektor_Prostih_Faktora[j]) brojac++;
- }
- if(brojac%2==1) p=p*Vektor_Prostih_Brojeva[i];
- brojac=0;
- }
- int broj=n/p;
- q=sqrt(broj);
- std::cout << p << " " << q;
- }
- int main ()
- {
- int n;
- std::cout << "Unesite prirodan broj: ";
- std::cin >> n;
- int a=1;
- int b=1;
- try {
- RastavaBroja(n, a, b);
- std::cout << a << " " << b;
- }
- catch(std::domain_error izuzetak) {
- std::cout << izuzetak.what();
- }
- catch(...) {
- std::cout << "izuzetak:";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement