Dudeney Numbers
Given an integer n, the task is to check if n is a Dudeney number or not. A Dudeney number is a positive integer that is a perfect cube such that the sum of its decimal digits is equal to the cube root of the number.
Examples:
Input: N = 19683
Output: Yes
19683 = 273 and 1 + 9 + 6 + 8 + 3 = 27Input: N = 75742
Output: No
Approach:
- Check if n is a perfect cube, if not then it cannot be a Dudeney number.
- If n is a perfect cube then calculate the sum of its digits. If the sum of it’s digits is equal to its cube root then it is a Dudeney number else it is not.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> 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; } |
chevron_right
filter_none
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. |
chevron_right
filter_none
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") |
chevron_right
filter_none
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 |
chevron_right
filter_none
PHP
<?php // PHP implementation of the approach // Function that returns true if // n is a Dudeney number function isDudeney($n) { $cube_rt = floor(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 = $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 ?> |
chevron_right
filter_none
Output:
Yes
Recommended Posts:
- Permutation of numbers such that sum of two consecutive numbers is a perfect square
- Numbers less than N which are product of exactly two distinct prime numbers
- Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K
- Rearrange numbers in an array such that no two adjacent numbers are same
- Numbers within a range that can be expressed as power of two numbers
- Count numbers which can be constructed using two numbers
- Count numbers which are divisible by all the numbers from 2 to 10
- Maximum sum of distinct numbers such that LCM of these numbers is N
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Sum of first n even numbers
- Add two numbers using ++ and/or --
- Sum of numbers from 1 to N which are divisible by 3 or 4
- Taxicab Numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



