How to resolve the algorithm Disarium numbers step by step in the Java programming language
How to resolve the algorithm Disarium numbers step by step in the Java programming language
Table of Contents
Problem Statement
A Disarium number is an integer where the sum of each digit raised to the power of its position in the number, is equal to the number.
135 is a Disarium number: 11 + 32 + 53 == 1 + 9 + 125 == 135 There are a finite number of Disarium numbers.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Disarium numbers step by step in the Java programming language
The code above is a program written in Java that prints the first 18 disarium numbers. A disarium number is a number where the sum of the digits raised to the power of their respective positions is the number itself.
The program starts by defining a method called is_disarium
that takes an integer as input and returns true if the number is a disarium number, and false otherwise.
The is_disarium
method first stores the input number in a variable n
.
Then, it calculates the length of the input number by converting it to a string and calling the length()
method on the string.
The length of the number is stored in a variable called len
.
Next, the method initializes a variable called sum
to 0 and a variable called i
to 1.
Then, it enters a while loop that continues as long as n
is greater than 0.
Inside the loop, the method calculates the sum of the digits of n
raised to the power of their respective positions.
It does this by calculating n % 10
to get the last digit of n
, and then raising it to the power of len - i + 1
.
The result of this calculation is then added to the variable sum
.
After calculating the sum of the digits of n
, the method divides n
by 10 to remove the last digit, and increments i
by 1.
Once the loop has finished, the method checks if the sum of the digits of n
is equal to n
.
If it is, then the method returns true, indicating that n
is a disarium number.
Otherwise, the method returns false.
The main
method of the program starts by initializing a variable called i
to 0 and a variable called count
to 0.
Then, it enters a while loop that continues as long as count
is less than or equal to 18.
Inside the loop, the method calls the is_disarium
method with i
as the input.
If the method returns true, then the method prints i
to the console and increments count
by 1.
After the loop has finished, the method prints a newline character to the console.
The output of the program is the following:
1 8 175 518 598 1336 19683 29921 41922 81289 93084 548834 550502 617442 635328 750932 764356 861794
Source code in the java programming language
import java.lang.Math;
public class DisariumNumbers {
public static boolean is_disarium(int num) {
int n = num;
int len = Integer.toString(n).length();
int sum = 0;
int i = 1;
while (n > 0) {
sum += Math.pow(n % 10, len - i + 1);
n /= 10;
i ++;
}
return sum == num;
}
public static void main(String[] args) {
int i = 0;
int count = 0;
while (count <= 18) {
if (is_disarium(i)) {
System.out.printf("%d ", i);
count++;
}
i++;
}
System.out.printf("%s", "\n");
}
}
You may also check:How to resolve the algorithm Accumulator factory step by step in the REXX programming language
You may also check:How to resolve the algorithm Password generator step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Primality by trial division step by step in the Logo programming language
You may also check:How to resolve the algorithm Morse code step by step in the PL/I programming language
You may also check:How to resolve the algorithm Product of min and max prime factors step by step in the Julia programming language