Given a number “n”, find if it is Disarium or not. A number is called Disarium if sum of its digits powered with their respective positions is equal to the number itself.
Examples:
Input : n = 135
Output : Yes
1^1 + 3^2 + 5^3 = 135
Therefore, 135 is a Disarium number
Input : n = 89
Output : Yes
8^1+9^2 = 89
Therefore, 89 is a Disarium number
Input : n = 80
Output : No
8^1 + 0^2 = 8
The idea is to first count digits in given numbers. Once we have count, we traverse all digits from right most (using % operator), raise its power to digit count and decrement the digit count.
Below is the implementation of above idea.
C++
#include<bits/stdc++.h>
using namespace std;
int countDigits( int n)
{
int count_digits = 0;
int x = n;
while (x)
{
x = x/10;
count_digits++;
}
return count_digits;
}
bool check( int n)
{
int count_digits = countDigits(n);
int sum = 0;
int x = n;
while (x)
{
int r = x%10;
sum = sum + pow (r, count_digits--);
x = x/10;
}
return (sum == n);
}
int main()
{
int n = 135;
if ( check(n))
cout << "Disarium Number" ;
else
cout << "Not a Disarium Number" ;
return 0;
}
|
Java
class Test
{
static boolean check( int n)
{
int count_digits = Integer.toString(n).length();
int sum = 0 ;
int x = n;
while (x!= 0 )
{
int r = x% 10 ;
sum = ( int ) (sum + Math.pow(r, count_digits--));
x = x/ 10 ;
}
return (sum == n);
}
public static void main(String[] args)
{
int n = 135 ;
System.out.println(check(n) ? "Disarium Number" : "Not a Disarium Number" );
}
}
|
Python3
import math
def check(n) :
count_digits = len ( str (n))
sum = 0
x = n
while (x! = 0 ) :
r = x % 10
sum = ( int ) ( sum + math. pow (r, count_digits))
count_digits = count_digits - 1
x = x / / 10
if sum = = n :
return 1
else :
return 0
n = 135
if (check(n) = = 1 ) :
print ( "Disarium Number" )
else :
print ( "Not a Disarium Number" )
|
C#
using System;
class GFG{
static bool check( int n)
{
int count_digits = n.ToString().Length;
int sum = 0;
int x = n;
while (x != 0)
{
int r = x % 10;
sum = ( int )(sum + Math.Pow(
r, count_digits--));
x = x / 10;
}
return (sum == n);
}
public static void Main( string [] args)
{
int n = 135;
Console.Write(check(n) ? "Disarium Number" :
"Not a Disarium Number" );
}
}
|
Javascript
<script>
function check(n)
{
var count_digits = n.toString().length;
var sum = 0;
var x = n;
while (x!=0)
{
var r = x%10;
sum = (sum + Math.pow(r, count_digits--));
x = x/10;
}
return (sum == n);
}
var n = 135;
document.write(check(n) ? "Disarium Number" : "Not a Disarium Number" );
</script>
|
Time complexity : O(logn)
Auxiliary Space : O(1)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!