Dudeney Numbers
Last Updated :
09 Jun, 2022
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 = 27
Input: 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++
#include <bits/stdc++.h>
using namespace std;
bool isDudeney( int n)
{
int cube_rt = int (round(( pow (n, 1.0 / 3.0))));
if (cube_rt * cube_rt * cube_rt != n)
return false ;
int dig_sum = 0;
int temp = n;
while (temp > 0) {
int rem = temp % 10;
dig_sum += rem;
temp /= 10;
}
if (cube_rt != dig_sum)
return false ;
return true ;
}
int main()
{
int n = 17576;
if (isDudeney(n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.lang.Math;
class GFG
{
static boolean isDudeney( int n)
{
int cube_rt = ( int )(Math.round((Math.pow(n, 1.0 / 3.0 ))));
if (cube_rt * cube_rt * cube_rt != n)
return false ;
int dig_sum = 0 ;
int temp = n;
while (temp > 0 )
{
int rem = temp % 10 ;
dig_sum += rem;
temp /= 10 ;
}
if (cube_rt != dig_sum)
return false ;
return true ;
}
public static void main(String[] args)
{
int n = 17576 ;
if (isDudeney(n))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def isDudeney(n):
cube_rt = int ( round (( pow (n, 1.0 / 3.0 ))))
if cube_rt * cube_rt * cube_rt ! = n:
return False
dig_sum = 0
temp = n
while temp> 0 :
rem = temp % 10
dig_sum + = rem
temp / / = 10
if cube_rt ! = dig_sum:
return False
return True
if __name__ = = '__main__' :
n = 17576
if isDudeney(n):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static bool isDudeney( int n)
{
int cube_rt = ( int )(Math.Round((Math.Pow(n, 1.0 / 3.0))));
if (cube_rt * cube_rt * cube_rt != n)
return false ;
int dig_sum = 0;
int temp = n;
while (temp > 0)
{
int rem = temp % 10;
dig_sum += rem;
temp /= 10;
}
if (cube_rt != dig_sum)
return false ;
return true ;
}
public static void Main()
{
int n = 17576;
if (isDudeney(n))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
PHP
<?php
function isDudeney( $n )
{
$cube_rt = floor ( round ((pow( $n , 1.0 / 3.0))));
if ( $cube_rt * $cube_rt * $cube_rt != $n )
return false;
$dig_sum = 0;
$temp = $n ;
while ( $temp > 0)
{
$rem = $temp % 10;
$dig_sum += $rem ;
$temp = $temp /10;
}
if ( $cube_rt != $dig_sum )
return false;
return true;
}
$n = 17576;
if (isDudeney( $n ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function isDudeney(n)
{
let cube_rt = parseInt(
Math.round((Math.pow(n, 1.0 / 3.0))));
if (cube_rt * cube_rt * cube_rt != n)
return false ;
let dig_sum = 0;
let temp = n;
while (temp > 0)
{
let rem = temp % 10;
dig_sum += rem;
temp = parseInt(temp / 10);
}
if (cube_rt != dig_sum)
return false ;
return true ;
}
let n = 17576;
if (isDudeney(n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(logn)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...