How to resolve the algorithm Disarium numbers step by step in the JavaScript programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Disarium numbers step by step in the JavaScript 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 JavaScript programming language

The code snippet you provided is a JavaScript function that checks if a number is a Disarium number. A Disarium number is a number whose sum of its digits raised to their respective positions is the number itself. For example, 175 is a Disarium number because 1^1 + 7^2 + 5^3 = 175.

The function takes one parameter, num, which is the number to be checked. The function first converts num to a string and finds its length, which is stored in the len variable. The sum variable is initialized to 0.

The function then enters a while loop that continues as long as n is greater than 0. Inside the loop, the function adds the last digit of n raised to the power of len to sum. The last digit of n is found by taking the remainder of n when divided by 10. The function then removes the last digit of n by dividing n by 10 and converting the result to an integer. The len variable is also decremented by 1.

After the loop has finished, the function checks if num is equal to sum. If it is, then num is a Disarium number and the function returns true. Otherwise, the function returns false.

The code snippet also includes a while loop that prints the first 18 Disarium numbers. The loop starts with i = 1 and increments i by 1 each time through the loop. The loop continues until count is greater than or equal to 18. Inside the loop, the function checks if i is a Disarium number by calling the is_disarium function. If i is a Disarium number, then the function prints i to the console and increments count by 1.

Source code in the javascript programming language

function is_disarium (num) {
    let n = num
    let len = n.toString().length
    let sum = 0
    while (n > 0) {
        sum += (n % 10) ** len
        n = parseInt(n / 10, 10)
        len--
    }
    return num == sum
}
let count = 0
let i = 1
while (count < 18) {
    if (is_disarium(i)) {
        process.stdout.write(i + " ")
        count++
    }
    i++
}


  

You may also check:How to resolve the algorithm Canonicalize CIDR step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Find common directory path step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Zero to the zero power step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Range expansion step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Character codes step by step in the JavaScript programming language