June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -1,6 +1,8 @@
A &nbsp; [http://mathworld.wolfram.com/NarcissisticNumber.html Narcissistic decimal number] &nbsp; is a non-negative integer, &nbsp; <math>n</math>, &nbsp; that is equal to the sum of the &nbsp; <math>m</math>-th &nbsp; powers of each of the digits in the decimal representation of &nbsp; <math>n</math>, &nbsp; where &nbsp; <math>m</math> &nbsp; is the number of digits in the decimal representation of &nbsp; <math>n</math>.
Narcissistic (decimal) numbers are sometimes called &nbsp; '''Armstrong''' &nbsp; numbers, named after Michael F. Armstrong.
<br>They are also known as &nbsp; '''Plus Perfect''' &nbsp; numbers.
;An example:
@ -16,4 +18,10 @@ Generate and show here the first &nbsp; '''25''' &nbsp; narcissistic decimal num
Note: &nbsp; <math>0^1 = 0</math>, &nbsp; the first in the series.
;See also:
* &nbsp; the &nbsp;OEIS entry: &nbsp; &nbsp; [http://oeis.org/A005188 Armstrong (or Plus Perfect, or narcissistic) numbers].
* &nbsp; MathWorld entry: &nbsp; [http://mathworld.wolfram.com/NarcissisticNumber.html Narcissistic Number].
* &nbsp; Wikipedia entry: &nbsp; &nbsp; [https://en.wikipedia.org/wiki/Narcissistic_number Narcissistic number].
<br><br>

View file

@ -0,0 +1,57 @@
PROGRAM-ID. NARCISSIST-NUMS.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 num-length PIC 9(2) value 0.
01 in-sum PIC 9(9) value 0.
01 counter PIC 9(9) value 0.
01 current-number PIC 9(9) value 0.
01 narcissist PIC Z(9).
01 temp PIC 9(9) value 0.
01 modulo PIC 9(9) value 0.
01 answer PIC 9 .
PROCEDURE DIVISION.
MAIN-PROCEDURE.
DISPLAY "the first 20 narcissist numbers:" .
MOVE 20 TO counter.
PERFORM UNTIL counter=0
PERFORM 000-NARCISSIST-PARA
IF answer = 1
SUBTRACT 1 from counter
GIVING counter
MOVE current-number TO narcissist
DISPLAY narcissist
END-IF
ADD 1 TO current-number
END-PERFORM
STOP RUN.
000-NARCISSIST-PARA.
MOVE ZERO TO in-sum.
MOVE current-number TO temp.
COMPUTE num-length =1+ FUNCTION Log10(temp)
PERFORM UNTIL temp=0
DIVIDE temp BY 10 GIVING temp
REMAINDER modulo
COMPUTE modulo=modulo**num-length
ADD modulo to in-sum GIVING in-sum
END-PERFORM.
IF current-number=in-sum
MOVE 1 TO answer
ELSE MOVE 0 TO answer
END-IF.
END PROGRAM NARCISSIST-NUMS.

View file

@ -0,0 +1,15 @@
USING: io kernel lists lists.lazy math math.functions
math.text.utils prettyprint sequences ;
IN: rosetta-code.narcissistic-decimal-number
: digit-count ( n -- count ) log10 floor >integer 1 + ;
: narcissist? ( n -- ? ) dup [ 1 digit-groups ]
[ digit-count [ ^ ] curry ] bi map-sum = ;
: first25 ( -- seq ) 25 0 lfrom [ narcissist? ] lfilter
ltake list>array ;
: main ( -- ) first25 [ pprint bl ] each ;
MAIN: main

View file

@ -0,0 +1,22 @@
Narc:=proc(i)
local num,len,j,sums:
sums:=0:
num := parse~(StringTools:-Explode((convert(i,string)))):
len:=numelems(num):
for j from 1 to len do
sums:=sums+(num[j]^(len)):
end do;
if sums = i then
return i;
else
return NULL;
end if;
end proc:
i:=0:
NDN:=[]:
while numelems(NDN)<25 do
NDN:=[op(NDN),(Narc(i))]:
i:=i+1:
end do:
NDN;

View file

@ -7,14 +7,12 @@ sub kigits($n) {
}
}
constant narcissistic = 0, (1..*).map: -> $d {
for (1..*) -> $d {
my @t = 0..9 X** $d;
my @table = @t X+ @t X+ @t;
sub is-narcissistic(\n) { n == [+] @table[kigits(n)] }
gather take $_ if is-narcissistic($_) for 10**($d-1) ..^ 10**$d;
}
for narcissistic {
say ++state $n, "\t", $_;
last if $n == 25;
}
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
};

View file

@ -1,17 +1,17 @@
/*REXX program generates and displays a number of narcissistic (Armstrong) numbers. */
numeric digits 39 /*be able to handle largest Armstrong #*/
parse arg N .; if N=='' | N=="," then N=25 /*obtain the number of narcissistic #'s*/
parse arg N . /*obtain optional argument from the CL.*/
if N=='' | N=="," then N=25 /*Not specified? Then use the default.*/
N=min(N, 89) /*there are only 89 narcissistic #s. */
#=0 /*number of narcissistic numbers so far*/
do j=0 until #==N; L=length(j) /*get length of the J decimal number.*/
$=left(j,1)**L /*1st digit in J raised to the L pow.*/
$=left(j, 1) **L /*1st digit in J raised to the L pow.*/
do k=2 for L-1 until $>j /*perform for each decimal digit in J.*/
$=$ + substr(j, k, 1) ** L /*add digit raised to power to the sum.*/
end /*k*/ /* [↑] calculate the rest of the sum. */
do k=2 for L-1 until $>j /*perform for each decimal digit in J.*/
$=$ + substr(j, k, 1) ** L /*add digit raised to power to the sum.*/
end /*k*/ /* [↑] calculate the rest of the sum. */
if $\==j then iterate /*does the sum equal to J? No, skip it*/
#=#+1 /*bump count of narcissistic numbers. */
#=# + 1 /*bump count of narcissistic numbers. */
say right(#, 9) ' narcissistic:' j /*display index and narcissistic number*/
end /*j*/ /* [↑] this list starts at 0 (zero).*/
/*stick a fork in it, we're all done. */
end /*j*/ /*stick a fork in it, we're all done. */

View file

@ -1,23 +1,23 @@
/*REXX program generates and displays a number of narcissistic (Armstrong) numbers. */
numeric digits 39 /*be able to handle largest Armstrong #*/
parse arg N .; if N=='' | N=="," then N=25 /*obtain the number of narcissistic #'s*/
parse arg N . /*obtain optional argument from the CL.*/
if N=='' | N=="," then N=25 /*Not specified? Then use the default.*/
N=min(N, 89) /*there are only 89 narcissistic #s. */
do w=1 for 39 /*generate tables: digits ^ L power. */
do i=0 for 10; @.w.i=i**w /*build table of ten digits ^ L power. */
do p=1 for 39 /*generate tables: digits ^ P power. */
do i=0 for 10; @.p.i= i**p /*build table of ten digits ^ P power. */
end /*i*/
end /*w*/ /* [↑] table is a fixed (limited) size*/
#=0 /*number of narcissistic numbers so far*/
do j=0 until #==N; L=length(j) /*get length of the J decimal number.*/
_=left(j, 1) /*select the first decimal digit to sum*/
$=@.L._ /*sum of the J dec. digits ^ L (so far)*/
do k=2 for L-1 until $>j /*perform for each decimal digit in J.*/
_=substr(j, k, 1) /*select the next decimal digit to sum.*/
$=$ + @.L._ /*add dec. digit raised to power to sum*/
end /*k*/ /* [↑] calculate the rest of the sum. */
do k=2 for L-1 until $>j /*perform for each decimal digit in J.*/
_=substr(j, k, 1) /*select the next decimal digit to sum.*/
$=$ + @.L._ /*add dec. digit raised to power to sum*/
end /*k*/ /* [↑] calculate the rest of the sum. */
if $\==j then iterate /*does the sum equal to J? No, skip it*/
#=#+1 /*bump count of narcissistic numbers. */
#=# + 1 /*bump count of narcissistic numbers. */
say right(#, 9) ' narcissistic:' j /*display index and narcissistic number*/
end /*j*/ /* [↑] this list starts at 0 (zero).*/
/*stick a fork in it, we're all done. */
end /*j*/ /*stick a fork in it, we're all done. */

View file

@ -1,28 +1,31 @@
/*REXX program generates and displays a number of narcissistic (Armstrong) numbers. */
numeric digits 39 /*be able to handle largest Armstrong #*/
parse arg N .; if N=='' | N=="," then N=25 /*obtain the number of narcissistic #'s*/
parse arg N . /*obtain optional argument from the CL.*/
if N=='' | N=="," then N=25 /*Not specified? Then use the default.*/
N=min(N, 89) /*there are only 89 narcissistic #s. */
@.=0 /*set default for the @ stemmed array. */
#=0 /*number of narcissistic numbers so far*/
do w=0 for 39+1; if w<10 then call tell w /*display the 1st 1─digit dec. numbers.*/
do i=1 for 9; @.w.i=i**w /*build table of ten digits ^ L power. */
do p=0 for 39+1; if p<10 then call tell p /*display the 1st 1─digit dec. numbers.*/
do i=1 for 9; @.p.i= i**p /*build table of ten digits ^ P power. */
end /*i*/
end /*w*/ /* [↑] table is a fixed (limited) size*/
end /*p*/ /* [↑] table is a fixed (limited) size*/
/* [↓] skip the 2─digit dec. numbers. */
do j=100; L=length(j) /*get length of the J decimal number.*/
parse var j _1 2 _2 3 m '' -1 _R /*get 1st, 2nd, middle, last dec. digit*/
do j=100; L=length(j) /*get length of the J decimal number.*/
parse var j _1 2 _2 3 m '' -1 _R /*get 1st, 2nd, middle, last dec. digit*/
$=@.L._1 + @.L._2 + @.L._R /*sum of the J decimal digs^L (so far).*/
do k=3 for L-3 until $>j /*perform for other decimal digits in J*/
parse var m _ +1 m /*get next dec. dig in J, start at 3rd.*/
$=$ + @.L._ /*add dec. digit raised to pow to sum. */
end /*k*/ /* [↑] calculate the rest of the sum. */
do k=3 for L-3 until $>j /*perform for other decimal digits in J*/
parse var m _ +1 m /*get next dec. dig in J, start at 3rd.*/
$=$ + @.L._ /*add dec. digit raised to pow to sum. */
end /*k*/ /* [↑] calculate the rest of the sum. */
if $==j then call tell j /*does the sum equal to J? Show the #*/
if $==j then do; call tell j /*does the sum equal to J? Show the #*/
if #==n then leave /*does the sum equal to J? Show the #*/
end
end /*j*/ /* [↑] the J loop list starts at 100*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
tell: #=#+1 /*bump the counter for narcissistic #s.*/
tell: #=# + 1 /*bump the counter for narcissistic #s.*/
say right(#,9) ' narcissistic:' arg(1) /*display index and narcissistic number*/
if #==N then exit /*stick a fork in it, we're all done. */
if #==n & n<11 then exit /*finished showing of narcissistic #'s?*/
return /*return to invoker & keep on truckin'.*/

View file

@ -0,0 +1,40 @@
/*REXX program generates and displays a number of narcissistic (Armstrong) numbers. */
numeric digits 39 /*be able to handle largest Armstrong #*/
parse arg N . /*obtain optional argument from the CL.*/
if N=='' | N=="," then N=25 /*Not specified? Then use the default.*/
N=min(N, 89) /*there are only 89 narcissistic #s. */
@.=0 /*set default for the @ stemmed array. */
#=0 /*number of narcissistic numbers so far*/
do p=0 for 39+1; if p<10 then call tell p /*display the 1st 1─digit dec. numbers.*/
do i=1 for 9; @.p.i= i**p /*build table of ten digits ^ P power. */
zzj= '00'j; @.p.zzj= @.p.j /*assign value for a 3-dig number (LZ),*/
end /*i*/
do j=10 to 99; parse var j t 2 u /*obtain 2 decimal digits of J: T U */
@.p.j = @.p.t + @.p.u /*assign value for a 2─dig number. */
zj= '0'j; @.p.zj = @.p.j /* " " " " 3─dig " (LZ),*/
end /*j*/ /* [↑] T≡ tens digit; U≡ units digit.*/
do k=100 to 999; parse var k h 2 t 3 u /*obtain 3 decimal digits of J: H T U */
@.p.k= @.p.h + @.p.t + @.p.u /*assign value for a three-digit number*/
end /*k*/ /* [↑] H≡ hundreds digit; T≡ tens ···*/
end /*p*/ /* [↑] table is a fixed (limited) size*/
/* [↓] skip the 2─digit dec. numbers. */
do j=100; L=length(j) /*get length of the J decimal number.*/
parse var j _ +3 m /*get 1st three decimal digits of J. */
$=@.L._ /*sum of the J decimal digs^L (so far).*/
do while m\=='' /*do the rest of the dec. digs in J. */
parse var m _ +3 m /*get the next 3 decimal digits in M. */
$=$ + @.L._ /*add dec. digit raised to pow to sum. */
end /*while*/ /* [↑] calculate the rest of the sum. */
if $==j then do; call tell j /*does the sum equal to J? Show the #*/
if #==n then leave /*does the sum equal to J? Show the #*/
end
end /*j*/ /* [↑] the J loop list starts at 100*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
tell: #=# + 1 /*bump the counter for narcissistic #s.*/
say right(#,9) ' narcissistic:' arg(1) /*display index and narcissistic number*/
if #==n & n<11 then exit /*finished showing of narcissistic #'s?*/
return /*return to invoker & keep on truckin'.*/

View file

@ -1,28 +1,10 @@
class Integer
def narcissistic?
return false if self < 0
len = to_s.size
n = self
sum = 0
while n > 0
n, r = n.divmod(10)
sum += r ** len
end
sum == self
return false if negative?
digs = self.digits
m = digs.size
digs.map{|d| d**m}.sum == self
end
end
numbers = []
n = 0
while numbers.size < 25
numbers << n if n.narcissistic?
n += 1
end
# or
# numbers = 0.step.lazy.select(&:narcissistic?).first(25) # Ruby ver 2.1
max = numbers.max.to_s.size
g = numbers.group_by{|n| n.to_s.size}
g.default = []
(1..max).each{|n| puts "length #{n} : #{g[n].join(", ")}"}
puts 0.step.lazy.select(&:narcissistic?).first(25)