How to resolve the algorithm Narcissistic decimal number step by step in the Clojure programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Narcissistic decimal number step by step in the Clojure 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 Clojure programming language
Source code in the clojure programming language
(ns narcissistic.core
(:require [clojure.math.numeric-tower :as math]))
(defn digits [n] ;; digits of a number.
(->> n str (map (comp read-string str))))
(defn narcissistic? [n] ;; True if the number is a Narcissistic one.
(let [d (digits n)
s (count d)]
(= n (reduce + (map #(math/expt % s) d)))))
(defn firstNnarc [n] ;;list of the first "n" Narcissistic numbers.
(take n (filter narcissistic? (range))))
You may also check:How to resolve the algorithm Disarium numbers step by step in the Lua programming language
You may also check:How to resolve the algorithm Get system command output step by step in the J programming language
You may also check:How to resolve the algorithm Quine step by step in the Lisp programming language
You may also check:How to resolve the algorithm Sorting algorithms/Cocktail sort step by step in the Factor programming language
You may also check:How to resolve the algorithm Sorting algorithms/Bogosort step by step in the C++ programming language