Python program to check given number is Disarium or not
In this post let us see an interesting concept called Disarium Number using python we will try to check if a given number satisfies the Disarium concept or not. Now First, let us get to know about what is Disarium number.
For example, we will consider the number 89. Now let us check if it is Disarium or not. Then we will implement the Python program to check the given number is Disarium or not.
89 = 8^1 + 9^2 = 89
Here 8 power 1 plus 9 power 2 will produce the same result as the number we took.
135 = 1^1 + 3^2 + 5^3 = 135
Here also the same procedure is followed and we get the original number as it was.
Check if a given number is Disarium or not in Python
def digits(n): l=0 while(n>0): n=n//10 l=l+1 return l def Disarium(x,n): Sum=0 while(n>0): r=n%10 Sum=Sum+(r**x) x=x-1 n=n//10 return Sum n=int(input()) x=digits(n) m=Disarium(x,n) if(m==n): print("Disarium") else: print("Not Disarium")
Input: 135 Output: Disarium Input:125 output: Not Disarium
We are defining a function called digits which will find the length or number of digits in a number.
Then the other function called Disarium will be used to implement our logic that is finding the remainder and then applying power to them starting from the length and going on decreasing.
For example when you pass a number 135 then first function digits will find the number of digits in the number by splitting it using remainder logic. Then we will try to return the total length so as to know till how many powers we have to go exactly. Then comes the Disarium logic which uses a while loop and finds the remainder and powers it up and again adds it up producing the final result. Finally, we will check if the given number matches the input number and if it does we will print output as yes.