📜  杜德尼数字

📅  最后修改于: 2021-05-05 00:29:59             🧑  作者: Mango

给定整数n ,任务是检查n是否为Dudeney数字。 Dudeney数字是一个正整数,是一个完美的立方体,因此其十进制数字的总和等于该数字的立方体根。

例子:

方法:

  • 检查n是否是一个完美的立方体,如果不是,则它不能是Dudeney数。
  • 如果n是一个完美的立方体,则计算其数字的总和。如果其数字的总和等于其立方根,则为Dudeney数字,否则为非。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function that returns true if
// n is a Dudeney number
bool isDudeney(int n)
{
    int cube_rt = int(round((pow(n, 1.0 / 3.0))));
  
    // If n is not a perfect cube
    if (cube_rt * cube_rt * cube_rt != n)
        return false;
  
    int dig_sum = 0;
    int temp = n;
    while (temp > 0) {
  
        // Last digit
        int rem = temp % 10;
  
        // Update the digit sum
        dig_sum += rem;
  
        // Remove the last digit
        temp /= 10;
    }
  
    // If cube root of n is not equal to
    // the sum of its digits
    if (cube_rt != dig_sum)
        return false;
  
    return true;
}
  
// Driver code
int main()
{
    int n = 17576;
    if (isDudeney(n))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}


Java
// Java implementation of the approach
import java.lang.Math;
  
class GFG
{
      
// Function that returns true if
// n is a Dudeney number
static boolean isDudeney(int n)
{
    int cube_rt = (int)(Math.round((Math.pow(n, 1.0 / 3.0))));
  
    // If n is not a perfect cube
    if (cube_rt * cube_rt * cube_rt != n)
        return false;
  
    int dig_sum = 0;
    int temp = n;
    while (temp > 0)
    {
  
        // Last digit
        int rem = temp % 10;
  
        // Update the digit sum
        dig_sum += rem;
  
        // Remove the last digit
        temp /= 10;
    }
  
    // If cube root of n is not equal to
    // the sum of its digits
    if (cube_rt != dig_sum)
        return false;
  
    return true;
}
  
// Driver code
public static void main(String[] args)
{
    int n = 17576;
    if (isDudeney(n))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
  
// This code is contributed by Code_Mech.


Python3
# Python implementation of the approach
  
# Function that returns true if 
# n is a Dudeney number
def isDudeney(n):
    cube_rt = int(round((pow(n, 1.0 / 3.0))))
  
    # If n is not a perfect cube
    if cube_rt * cube_rt * cube_rt != n:
        return False
  
    dig_sum = 0
    temp = n
    while temp>0:
          
        # Last digit
        rem = temp % 10
          
        # Update the digit sum
        dig_sum += rem
          
        # Remove the last digit
        temp//= 10
  
    # If cube root of n is not equal to 
    # the sum of its digits
    if cube_rt != dig_sum:
        return False
  
    return True
  
# Driver code
if __name__ == '__main__':
      
    n = 17576
    if isDudeney(n):
        print("Yes")
    else:
        print("No")


C#
// C# implementation of the approach
using System;
  
class GFG
{
      
// Function that returns true if
// n is a Dudeney number
static bool isDudeney(int n)
{
    int cube_rt = (int)(Math.Round((Math.Pow(n, 1.0 / 3.0))));
  
    // If n is not a perfect cube
    if (cube_rt * cube_rt * cube_rt != n)
        return false;
  
    int dig_sum = 0;
    int temp = n;
    while (temp > 0)
    {
  
        // Last digit
        int rem = temp % 10;
  
        // Update the digit sum
        dig_sum += rem;
  
        // Remove the last digit
        temp /= 10;
    }
  
    // If cube root of n is not equal to
    // the sum of its digits
    if (cube_rt != dig_sum)
        return false;
  
    return true;
}
  
// Driver code
public static void Main()
{
    int n = 17576;
    if (isDudeney(n))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
  
// This code is contributed 
// by Akanksha Rai


PHP
 0) 
    { 
  
        // Last digit 
        $rem = $temp % 10; 
  
        // Update the digit sum 
        $dig_sum += $rem; 
  
        // Remove the last digit 
        $temp = $temp/10; 
    } 
  
    // If cube root of n is not equal to 
    // the sum of its digits 
    if ($cube_rt != $dig_sum) 
        return false; 
  
    return true; 
} 
  
// Driver code 
$n = 17576; 
if (isDudeney($n)) 
    echo "Yes"; 
else
    echo "No";
  
// This code is contributed by Ryuga
?>


输出:
Yes