Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <stdexcept>
- using namespace std;
- void RastavaBroja(long n,int &p, int &q)
- {
- if(n<=0) throw std::domain_error("Broj koji se rastavlja mora biti prirodan");
- int brojac=0;
- q=1;
- p=1;
- while(n%2==0) {
- brojac++;
- n=n/2;
- }
- if(brojac!=0) {
- if(brojac%2!=0) {
- p=p*2;
- brojac--;
- }
- if(brojac>=2) {
- brojac=brojac/2;
- for(int j=0; j<brojac ; j++) {
- q=q*2;
- }
- }
- }
- for (int i=3; i<=n; i=i+2) {
- int brojac=0;
- while(n%i==0) {
- brojac++;
- n=n/i;
- }
- if(brojac!=0) {
- if(brojac%2!=0) {
- p=p*i;
- brojac--;
- }
- if(brojac>=2) {
- brojac=brojac/2;
- for(int j=0; j<brojac; j++) {
- q=q*i;
- }
- }
- }
- }
- }
- int main ()
- {
- int n;
- int p;
- int q;
- cout<<"Unesite prirodan broj "<<endl;
- cin>>n;
- try {
- RastavaBroja(n,p,q);
- cout<<p<<" "<<q;
- } catch(std::domain_error &izuzetak) {
- std::cout<<"Izuzetak: ";
- std::cout<<izuzetak.what()<<"!";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement