How to resolve the algorithm Narcissistic decimal number step by step in the Raku programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Narcissistic decimal number step by step in the Raku 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 Raku programming language

Source code in the raku programming language

sub is-narcissistic(Int $n) { $n == [+] $n.comb »**» $n.chars }
my @N = lazy (0..∞).hyper.grep: *.&is-narcissistic;
@N[^25].join(' ').say;


sub kigits($n) {
    my int $i = $n;
    my int $b = 1000;
    gather while $i {
        take $i % $b;
        $i = $i div $b;
    }
}

for (1..*) -> $d {
    my @t = 0..9 X** $d;
    my @table = @t X+ @t X+ @t;
    sub is-narcissistic(\n) { n == [+] @table[kigits(n)] };
    state $l = 2;
    FIRST say "1\t0";
    say $l++, "\t", $_ if .&is-narcissistic for 10**($d-1) ..^ 10**$d;
    last if $l > 25
};


  

You may also check:How to resolve the algorithm Burrows–Wheeler transform step by step in the J programming language
You may also check:How to resolve the algorithm Matrix-exponentiation operator step by step in the jq programming language
You may also check:How to resolve the algorithm Align columns step by step in the BQN programming language
You may also check:How to resolve the algorithm Hamming numbers step by step in the Quackery programming language
You may also check:How to resolve the algorithm Bioinformatics/base count step by step in the MATLAB / Octave programming language