Disarium Number in Python
Tags: python, programming
Published: 2021-06-28 06:22:28

<strong>Disarium Number:</strong>

A Disarium number is one in which the sum of each digit raised to the power of its respective position equals the original number.

like 135 , 89 etc.

<strong>Example1:</strong>

<strong>Input:</strong>
<pre>number =135</pre>
<strong>Output:</strong>
<pre id="codeOutputPre">135 is disarium number</pre>
<strong>Explanation:</strong>
<pre>Here 1^1 + 3^2 + 5^3 = 135 so it is disarium Number</pre>
<strong>Example2:</strong>

<strong>Input:</strong>
<pre>number =79</pre>
<strong>Output:</strong>
<pre id="codeOutputPre">79 is not disarium number</pre>
<strong>Explanation:</strong>
<pre>Here 7^1 + 9^2 = 87 not equal to 79  so it is not disarium Number</pre>
<h2>Disarium Number in Python</h2>
Below are the ways to check Disarium number in python
<ul>
 	<li><a href="#Using_while_loop">Using while loop</a></li>
 	<li><a href="#By_converting_the_number_to_string_and_Traversing_the_string_to_extract_the_digits">By converting the number to string and Traversing the string to extract the digits</a></li>
</ul>
Explore more instances related to python concepts from <a href="https://btechgeeks.com/python-programming-examples-with-output/">Python Programming Examples</a> Guide and get promoted from beginner to professional programmer level in Python Programming Language.
<h3 id="Using_while_loop">Method #1: Using while loop</h3>
<strong>Algorithm:</strong>
<ul>
 	<li>Scan the number and calculate the size of the number.</li>
 	<li>Make a copy of the number so you can verify the outcome later.</li>
 	<li>Make a result variable (with a value of 0) and an iterator ( set to the size of the number)</li>
 	<li>Create a while loop to go digit by digit through the number.</li>
 	<li>On each iteration, multiply the result by a digit raised to the power of the iterator value.</li>
 	<li>On each traversal, increment the iterator.</li>
 	<li>Compare the result value to a copy of the original number.</li>
</ul>
<strong>Below is the implementation:</strong>
<pre class="EnlighterJSRAW" data-enlighter-language="python"># given number
num = 135
# intialize result to zero(ans)
ans = 0
# calculating the digits
digits = len(str(num))
# copy the number in another variable(duplicate)
dup_number = num
while (dup_number != 0):

    # getting the last digit
    remainder = dup_number % 10

    # multiply the result by a digit raised to the power of the iterator value.
    ans = ans + remainder**digits
    digits = digits - 1
    dup_number = dup_number//10
# It is disarium number if it is equal to original number
if(num == ans):
    print(num, "is disarium number")
else:
    print(num, "is not disarium number")
</pre>
<strong>Output:</strong>
<pre id="codeOutputPre">135 is disarium number</pre>
<h3 id="By_converting_the_number_to_string_and_Traversing_the_string_to_extract_the_digits">Method #2: By converting the number to string and Traversing the string to extract the digits</h3>
<strong>Algorithm:</strong>
<ul>
 	<li>Initialize a variable say <strong>ans</strong> to 0</li>
 	<li>Using a new variable, we must convert the given number to a string.</li>
 	<li>Take a temp count =1 and increase the count after each iteration.</li>
 	<li>Iterate through the string, convert each character to an integer, multiply the <strong>ans</strong> by a digit raised to the power of the count.</li>
 	<li>If the <strong>ans</strong> is equal to given number then it is disarium number</li>
</ul>
<strong>Below is the implementation:</strong>
<pre class="EnlighterJSRAW" data-enlighter-language="python"># given number
num = 135
# intialize result to zero(ans)
ans = 0
# make a temp count to 1
count = 1
# converting given number to string
numString = str(num)
# Traverse through the string
for char in numString:
    # Converting the character of string to integer
    # multiply the ans by a digit raised to the power of the iterator value.
    ans = ans+int(char)**count
    count = count+1
# It is disarium number if it is equal to original number
if(num == ans):
    print(num, "is disarium number")
else:
    print(num, "is not disarium number")
</pre>
<strong>Output:</strong>
<pre id="codeOutputPre">135 is disarium number</pre>