Disarium Number in Java
A number is said to be the Disarium number when the sum of its digits raised to the power of their respective positions is equal to the number itself. In other words, if a number is written as abcd..., where a, b, c, d, etc., are its digits, then it is considered a disarium number if it satisfies the following equation:
a^1 + b^2 + c^3 + d^4 + ... = abcd.....
Example 1:
Input:
int N = 89
Output:
Yes, the given number is the Disarium Number.
Explanation:
The given number is 89, and now the equation can be of the form 8^1 + 9^2 = 8 + 81 = 89. Hence, the obtained sum is equal to the given number. Thus, 89 is a disarium number.
Example 2:
Input:
int N = 518
Output:
Yes, the given number is the Disarium Number.
Explanation:
The given number is 518; now the equation can be of the form 5^1 + 1^2 + 8^3 = 5 + 1 + 512 = 518. Hence, the obtained sum is equal to the given number. Thus, 89 is a disarium number.
Example 3:
Input:
int N = 80
Output:
No, the given number is the Disarium Number.
Explanation:
The given number is 80, and now the equation can be of the form 8^1 + 0^2 = 8. Hence, the obtained sum is not equal to the given number. Thus, 80 is not a disarium number.
Example 4:
Input:
int N = 135
Output:
Yes, the given number is the Disarium Number.
Explanation:
The given number is 135, and now the equation can be of the form 1^1 + 3^2 + 5^3 = 1 + 9 + 125 = 135. Hence, the obtained sum is equal to the given number. Thus, 89 is a disarium number.
Approach: Naïve Approach
The following program can determine the Disarium number of a given number. The calculateLength() function is used to determine the length of the integer. After that, iterating through each digit and raising it to the power of its position results in an increase in the sum. In order to determine whether the total is a Disarium number, it finally compares it to the original number. In the case that they don't match, the number is printed as not being a Disarium number.
Algorithm:
Step 1: calculateLength() counts the digits present in a number.
Step 1.1: Use a while loop to check whether the number is not equal to 0.
Step 1.2: Divide the number by 10 and increment the variable length by 1.
Step 1.3: Return length.
Step 2: Define and initialize variable num.
Step 3: Make a copy of num by storing the value of num in n.
Step 4: Using the while loop, calculates remainder rem repeatedly by dividing num with 10.
Step 5: Calculate the value of rem raised to power its position, i.e., remain, and store the computed value in a variable sum.
Step 6: Check whether the sum is equal to the number. If yes, then the given number is a Disarium number. Otherwise, it is not a Disarium number.
Implementation:
FileName: DisariumNumber.java
import java.io.*; import java.util.*; public class DisariumNumber { //calculateLength() will count the digits present in a number public static int calculateLength(int n) { int length = 0; while(n != 0) { length = length + 1; n = n/10; } return length; } public static void main(String[] args) { // Compute the sum of terms like digit multiplied by // power of position int num = 135, sum = 0; int rem = 0, n; int len = calculateLength(num); //Makes a copy of the original number num n = num; //Calculates the sum of digits powered with their respective position while(num > 0) { rem = num%10; // Sum the digits by powering according to // the positions sum = sum + (int)Math.pow(rem,len); num = num/10; len--; } //Checks whether the sum is equal to the number itself if(sum == n) { System.out.println(n + " is a disarium number"); } else { System.out.println(n + " is not a disarium number"); } } }
Output:
135 is a disarium number
Complexity Analysis:
The time complexity is O(logN), and the space complexity is O(N), where N represents the given number.