# Disarium Number in Python

Hello there! Today let us learn something new and interesting, Disarium Number. We would be understanding what the number is and then implement a program to check if a number is a Disarium Number or not.

## What is a Disarium Number?

A number is a Disarium number if the sum of each digit raised to the power of their respective positions is equal to the original number.

Disarium Number definition is : abcd…(n-digits) = a^1 + b^2 + c^3 + d^4 + . . . . . and so on.

Recommended read: Implementing a Celsius to Fahrenheit converter in Python tkinter

## Examples of Disarium Number

### Example 1: 153

Calculation (digit – wise ) = 1^1 + 5^2 + 3^3 = 1 + 25+ 9= 35

The calculation done is not equal to the original number. Hence the number is not a Disarium number.

### Example 2: 175

Calculation (digit – wise ) = 1^1 + 7^2 + 5^3 = 1 + 49 + 125 = 175

The calculations done is directly equal to the original number. Hence the number is Disarium Number.

## Algorithm to check Disarium Number

All the steps involved in checking if a number is a Disarium number or not are:

1. Read the input number and calculate its size
2. Make a copy of the number to check the result later on.
3. Create a result variable ( set to 0 ) and an iterator ( set to the size of the number)
4. Create a while loop to traverse through the number digit by digit.
5. On every iteration increment result by digit raised to the power of the iterator value
6. Decrement iterator on every traversal
7. Check the result value with the copy of the original number

## PseudoCode for Identifying a Disarium Number

The code below shows the pseudo code to check if a number is a Disarium Number:

```READ n
CALCULATE SIZE OF NUMBER len_n
MAKE A COPY OF n
result=0
CREATE AN ITERATOR i = len_n
CHECK DIGIT BY DIGIT:
WHILE n!=0
GET CURRENT DIGIT : digit = n % 10
UPDATE RESULT : result = result + digit^(i)
TRIM THE LAST DIGIT : n = n / 10
DECREMENT i BY 1
ENDWHILE

CHECK FOR DISARIUM NUMBER:
IF result==COPY OF n
PRINT "DISARIUM NUMBER"
ELSE
PRINT "NOT A DISARIUM NUMBER"
```

## Implementing a Check for Disarium Number in Python

Now that we know about what the Disarium Number is and the steps to implement that, let’s implement the Disarium checking line by line.

### 1. Create the initial variables

We first take an input `n` and calculate the length of the number. We also store a copy of the input so that no matter how much we change the original number We also initialized the result as 0 and an iterator with the initial value as the length of the number.

The code for the same is shown below:

```n = input()
len_n = len(n)
n=int(n)
copy_n=n
result = 0
i = len_n
```

### 2. Traversing through the number and Updating Result

To access each digit we take the modulus of the number ( mod 10 ) to extract the last digit of the number. The next step involves updating the result as the sum of the previous result and the digit raised to the position of digits.

The last and final step we take is to divide the number by 10 to drop the last digit from the number. We also decrement the iterator by 1 on each iteration. The same process is repeated until there are no more digits are left in the number.

The code for the same is shown below:

```while(n!=0):
digit = n%10
result=result+pow(digit,i)
n=int(n/10)
i = i - 1
```

### 3. Checking if the number is a Disarium Number or not

The final step is to check the copy of the number we created earlier with the result calculated to finally tell if the number is a Disarium number or not. The code for the same is shown below:

```if(result==copy_n):
print("Disarium Number!")
else:
print("Not an Disarium Number!")
```

## Output Samples for the code

For now I tested the program for two inputs. Both of them are shown below:

### Number 1: 153

```153
Not a Disarium Number!
```

```121
Disarium Number!
```

## Conclusion

Congratulations! You have successfully learned about Disarium Number and implemented the same in Python! But don’t stop here! Keep Reading and Learning!

Recommended read: How to implement a GUI age checker in Python Tkinter?