How to resolve the algorithm Disarium numbers step by step in the AWK programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Disarium numbers step by step in the AWK 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 AWK programming language
Source code in the awk programming language
# syntax: GAWK -f DISARIUM_NUMBERS.AWK
BEGIN {
stop = 19
printf("The first %d Disarium numbers:\n",stop)
while (count < stop) {
if (is_disarium(n)) {
printf("%d ",n)
count++
}
n++
}
printf("\n")
exit(0)
}
function is_disarium(n, leng,sum,x) {
x = n
leng = length(n)
while (x != 0) {
sum += (x % 10) ^ leng
leng--
x = int(x/10)
}
return((sum == n) ? 1 : 0)
}
You may also check:How to resolve the algorithm Jewels and stones step by step in the SETL programming language
You may also check:How to resolve the algorithm Split a character string based on change of character step by step in the Phix programming language
You may also check:How to resolve the algorithm Machine code step by step in the Racket programming language
You may also check:How to resolve the algorithm Simple database step by step in the ToffeeScript programming language
You may also check:How to resolve the algorithm Queue/Usage step by step in the NetRexx programming language