完美的正方形和完美的立方体

发布于 2024-08-08 06:03:38 字数 48 浏览 5 评论 0原文

C++ 中是否有任何预定义函数来检查该数字是否是任何数字的平方以及立方体是否相同。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

一江春梦 2024-08-15 06:03:38

不,但是写一个很容易:

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;
}

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;
}
独自唱情﹋歌 2024-08-15 06:03:38

sqrt(x),或者一般来说,pow(x, 1./2)pow(x, 1./3)

对于示例:

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";

编辑: 或一般情况:

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;
}

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;
}
岁月苍老的讽刺 2024-08-15 06:03:38

不,没有标准的 C 或 C++ 函数来检查整数是否是完美的平方或完美的立方。

如果您希望它速度快并避免使用大多数答案中提到的浮点/双精度例程,则仅使用整数编写二分搜索代码。如果你能找到一个 n 且 n^2 < m< (n+1)^2,则m不是完全平方数。如果 m 是完全平方数,那么您将找到 n,其中 n^2=m。 此处讨论了该问题

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

阳光①夏 2024-08-15 06:03:38

试试这个:

#include<math.h>
int isperfect(long n)
{
    double xp=sqrt((double)n);
    if(n==(xp*xp))
        return 1;
    else
        return 0;
}

Try this:

#include<math.h>
int isperfect(long n)
{
    double xp=sqrt((double)n);
    if(n==(xp*xp))
        return 1;
    else
        return 0;
}
不知在何时 2024-08-15 06:03:38

为了识别正方形,我在java中尝试了这个算法。几乎没有语法差异,你也可以用 C++ 来完成它。
逻辑是,每两​​个连续完全平方数之间的差继续增加 2。 Diff(1,4)=3 、 Diff(4,9)=5 、 Diff(9,16)= 7 、 Diff(16,25 )= 9..... 继续。
我们可以利用这种现象来识别完美的正方形。
Java代码是,

    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;
 }  

为了更快地识别平方,我们可以使用另一个现象,完美平方的递归数字和总是1,4,7或9。
所以更快的代码可以......

  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;


     }

  }

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;


     }

  }
千秋岁 2024-08-15 06:03:38

最有效的答案可能是

    int x=sqrt(num)
    if(sqrt(num)>x){
    Then its not a square root}
    else{it is a perfect square}

这个方法之所以有效,是因为 x 是一个 int,并且它会删除小数部分以仅存储整数部分。如果一个数是整数的完全平方,则其平方根将是整数,因此 x 和 sqrt(x) 将相等。

The most efficient answer could be this

    int x=sqrt(num)
    if(sqrt(num)>x){
    Then its not a square root}
    else{it is a perfect square}

This method works because of the fact that x is an int and it will drop down the decimal part to store only the integer part. If a number is perfect square of an integer, its square root will be an integer and hence x and sqrt(x) will be equal.

红衣飘飘貌似仙 2024-08-15 06:03:38

对于完美的正方形,您还可以这样做:

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

对于完美的立方体,您可以:

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

希望这会有所帮助。

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.

别把无礼当个性 2024-08-15 06:03:38

我们可以使用内置的 truc 函数 -

#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));
}

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));
}
深海蓝天 2024-08-15 06:03:38
bool isSquare(int n) {
    return floor(sqrt(n)) == ceil(sqrt(n));
}

bool isQube(int n) {
    return floor(cbrt(n)) == ceil(cbrt(n));
}
bool isSquare(int n) {
    return floor(sqrt(n)) == ceil(sqrt(n));
}

bool isQube(int n) {
    return floor(cbrt(n)) == ceil(cbrt(n));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文