How to resolve the algorithm Narcissistic decimal number step by step in the jq programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Narcissistic decimal number step by step in the jq programming language
Table of Contents
Problem Statement
A Narcissistic decimal number is a non-negative integer,
n
{\displaystyle n}
, that is equal to the sum of the
m
{\displaystyle m}
-th powers of each of the digits in the decimal representation of
n
{\displaystyle n}
, where
m
{\displaystyle m}
is the number of digits in the decimal representation of
n
{\displaystyle n}
.
Narcissistic (decimal) numbers are sometimes called Armstrong numbers, named after Michael F. Armstrong. They are also known as Plus Perfect numbers.
Generate and show here the first 25 narcissistic decimal numbers.
Note:
0
1
= 0
{\displaystyle 0^{1}=0}
, the first in the series.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Narcissistic decimal number step by step in the jq programming language
Source code in the jq programming language
def is_narcissistic:
def digits: tostring | explode[] | [.] | implode | tonumber;
def pow(n): . as $x | reduce range(0;n) as $i (1; . * $x);
(tostring | length) as $len
| . == reduce digits as $d (0; . + ($d | pow($len)) )
end;
# Input: [i, [0^i, 1^i, 2^i, ..., 9^i]]
# Output: [j, [0^j, 1^j, 2^j, ..., 9^j]]
# provided j is i or (i+1)
def powers(j):
if .[0] == j then .
else .[0] += 1
| reduce range(0;10) as $k (.; .[1][$k] *= $k)
end;
# Input: [n, [i, [0^i, 1^i, 2^i,...]]] where i is the number of digits in n.
def is_narcissistic:
def digits: tostring | explode[] | [.] | implode | tonumber;
.[1][1] as $powers
| .[0]
| if . < 0 then false
else . == reduce digits as $d (0; . + $powers[$d] )
end;
# If your jq has "while", then feel free to omit the following definition:
def while(cond; update):
def _while: if cond then ., (update | _while) else empty end;
_while;
# The first k narcissistic numbers, beginning with 0:
def narcissistic(k):
# State: [n, is_narcissistic, count, [len, [0^len, 1^len, ...]]]
# where len is the number of digits in n.
[0, true, 1, [1, [range(0;10)]]]
| while( .[2] <= k;
.[3] as $powers
| (.[0]+1) as $n
| ($n | tostring | length) as $len
| ($powers | powers($len)) as $powersprime
| if [$n, $powersprime] | is_narcissistic
then [$n, true, .[2] + 1, $powersprime]
else [$n, false, .[2], $powersprime ]
end )
| select(.[1])
| "\(.[2]): \(.[0])" ;
narcissistic(25)
jq -r -n -f Narcissitic_decimal_number.jq
1: 0
2: 1
3: 2
4: 3
5: 4
6: 5
7: 6
8: 7
9: 8
10: 9
11: 153
12: 370
13: 371
14: 407
15: 1634
16: 8208
17: 9474
18: 54748
19: 92727
20: 93084
21: 548834
22: 1741725
23: 4210818
24: 9800817
25: 9926315
You may also check:How to resolve the algorithm Web scraping step by step in the Perl programming language
You may also check:How to resolve the algorithm Van Eck sequence step by step in the COBOL programming language
You may also check:How to resolve the algorithm Old Russian measure of length step by step in the Python programming language
You may also check:How to resolve the algorithm Arrays step by step in the NewLISP programming language
You may also check:How to resolve the algorithm Terminal control/Display an extended character step by step in the 11l programming language