How to resolve the algorithm Narcissistic decimal number step by step in the Mathematica/Wolfram Language programming language

Published on 22 June 2024 08:30 PM

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

This Wolfram code generates a sequence of narcissistic numbers up to a specified limit. Narcissistic numbers are numbers that are the sum of their own digits raised to the power of the number of digits. For example, 153 is a narcissistic number because 1^3 + 5^3 + 3^3 = 153.

The code uses the NestWhile function to generate the sequence of narcissistic numbers. The function takes a starting value, an update function, and a condition. In this case, the starting value is 0, the update function is # + 1, and the condition is Plus @@ (IntegerDigits[#]^IntegerLength[#]) != #.

The Plus @@ (IntegerDigits[#]^IntegerLength[#]) part of the condition calculates the sum of the digits of the number raised to the power of the number of digits. The != # part of the condition checks if the result of the calculation is not equal to the number itself. If the condition is met, the update function is applied to the current value of the sequence.

The narc /@ Range[25] part of the code applies the narc function to each number in the range from 1 to 25. The result is a list of the first 25 narcissistic numbers.

Here is the output of the code:

{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725, 4210818, 9800817, 9926315}

Source code in the wolfram programming language

narc[1] = 0;
narc[n_] := narc[n] = NestWhile[# + 1 &, narc[n - 1] + 1, Plus @@ (IntegerDigits[#]^IntegerLength[#]) != # &];
narc /@ Range[25]


  

You may also check:How to resolve the algorithm Permutations step by step in the EDSAC order code programming language
You may also check:How to resolve the algorithm Count in factors step by step in the Go programming language
You may also check:How to resolve the algorithm Sorting Algorithms/Circle Sort step by step in the Racket programming language
You may also check:How to resolve the algorithm Discordian date step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Terminal control/Ringing the terminal bell step by step in the PHP programming language