Guest User

Untitled

a guest
Apr 27th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. bool is_perfect_square(int n) {
  2. if (n < 0)
  3. return false;
  4. int root(round(sqrt(n)));
  5. return n == root * root;
  6. }
  7.  
  8. bool is_perfect_cube(int n) {
  9. int root(round(cbrt(n)));
  10. return n == root * root * root;
  11. }
  12.  
  13. int n = 9;
  14. int a = (int) sqrt((double) n);
  15. if(a * a == n || (a+1) * (a+1) == n) // in case of an off-by-one float error
  16. cout << "It's a square!n";
  17.  
  18. bool is_nth_power(int a, int n) {
  19. if(n <= 0)
  20. return false;
  21. if(a < 0 && n % 2 == 0)
  22. return false;
  23. a = abs(a);
  24.  
  25. int b = pow(a, 1. / n);
  26. return pow((double) b, n) == a || pow((double) (b+1), n) == a;
  27. }
Add Comment
Please, Sign In to add comment