Advertisement
Guest User

Untitled

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