Perfect square and perfect cube

前端 未结 8 2124
离开以前
离开以前 2020-12-09 12:27

Is there any predefined function in c++ to check whether the number is square of any number and same for the cube..

相关标签:
8条回答
  • 2020-12-09 12:48

    We could use the builtin truc function -

    #include <math.h>
    
    // For perfect square
    bool is_perfect_sq(double n) {
        double r = sqrt(n);
        return !(r - trunc(r));
    }
    
    // For perfect cube
    bool is_perfect_cube(double n) {
        double r = cbrt(n);
        return !(r - trunc(r));
    }
    
    0 讨论(0)
  • 2020-12-09 12:52

    sqrt(x), or in general, pow(x, 1./2) or pow(x, 1./3)

    For example:

    int n = 9;
    int a = (int) sqrt((double) n);
    if(a * a == n || (a+1) * (a+1) == n)  // in case of an off-by-one float error
        cout << "It's a square!\n";
    

    Edit: or in general:

    bool is_nth_power(int a, int n) {
      if(n <= 0)
        return false;
      if(a < 0 && n % 2 == 0)
        return false;
      a = abs(a);
    
      int b = pow(a, 1. / n);
      return pow((double) b, n) == a || pow((double) (b+1), n) == a;
    }
    
    0 讨论(0)
  • 2020-12-09 12:52

    No, there are no standard c or c++ functions to check whether an integer is a perfect square or a perfect cube.

    If you want it to be fast and avoid using the float/double routines mentioned in most of the answers, then code a binary search using only integers. If you can find an n with n^2 < m < (n+1)^2, then m is not a perfect square. If m is a perfect square, then you'll find an n with n^2=m. The problem is discussed here

    0 讨论(0)
  • 2020-12-09 12:54

    For perfect square you can also do:

    if(sqrt(n)==floor(sqrt(n)))
        return true;
    else
        return false;
    

    For perfect cube you can:

    if(cbrt(n)==floor(cbrt(n)))
        return true;
    else
        return false;
    

    Hope this helps.

    0 讨论(0)
  • 2020-12-09 12:57

    No, but it's easy to write one:

    bool is_perfect_square(int n) {
        if (n < 0)
            return false;
        int root(round(sqrt(n)));
        return n == root * root;
    }
    
    bool is_perfect_cube(int n) {
        int root(round(cbrt(n)));
        return n == root * root * root;
    }
    
    0 讨论(0)
  • 2020-12-09 12:59

    For identifying squares i tried this algorithm in java. With little syntax difference you can do it in c++ too. The logic is, the difference between every two consecutive perfect squares goes on increasing by 2. Diff(1,4)=3 , Diff(4,9)=5 , Diff(9,16)= 7 , Diff(16,25)= 9..... goes on. We can use this phenomenon to identify the perfect squares. Java code is,

        boolean isSquare(int num){
             int  initdiff = 3;
             int squarenum = 1;
             boolean flag = false;
             boolean square = false;
             while(flag != true){
    
                    if(squarenum == num){
    
                        flag = true;
                        square = true;
    
                    }else{
    
                        square = false;
                     }
                    if(squarenum > num){
    
                        flag = true;
                    }
                squarenum = squarenum + initdiff;
                initdiff = initdiff + 2;
       }
                  return square;
     }  
    

    To make the identification of squares faster we can use another phenomenon, the recursive sum of digits of perfect squares is always 1,4,7 or 9. So a much faster code can be...

      int recursiveSum(int num){
         int sum = 0;   
         while(num != 0){
         sum = sum + num%10;
         num = num/10;         
         }
         if(sum/10 != 0){         
            return recursiveSum(sum);     
         }
         else{
             return sum;
         }
    
     }
      boolean isSquare(int num){
             int  initdiff = 3;
             int squarenum = 1;
             boolean flag = false;
             boolean square = false;
             while(flag != true){
    
                    if(squarenum == num){
    
                        flag = true;
                        square = true;
    
                    }else{
    
                        square = false;
                     }
                    if(squarenum > num){
    
                        flag = true;
                    }
                squarenum = squarenum + initdiff;
                initdiff = initdiff + 2;
       }
                  return square;
     }  
    
       boolean isCompleteSquare(int a){
        // System.out.println(recursiveSum(a));
         if(recursiveSum(a)==1 || recursiveSum(a)==4 || recursiveSum(a)==7 || recursiveSum(a)==9){
    
             if(isSquare(a)){
    
                 return true;
    
             }else{
                 return false;
             }
    
    
         }else{
    
             return false;
    
    
         }
    
      }
    
    0 讨论(0)
提交回复
热议问题