Own digits power sum: Difference between revisions
m
→{{header|Wren}}: Changed to Wren S/H
m (→{{header|Wren}}: Changed to Wren S/H) |
|||
(45 intermediate revisions by 18 users not shown) | |||
Line 1:
{{
; Description
Line 13:
Optionally, do the same for '''N''' = '''9''' which may take a while for interpreted languages.
;Related:
* [[Narcissistic_decimal_number]]
<br><br>
=={{header|11l}}==
{{trans|Python}}
<syntaxhighlight lang="11l">V MAX_BASE = 10
V POWER_DIGIT = (0 .< MAX_BASE).map(_ -> (0 .< :MAX_BASE).map(_ -> 1))
V USED_DIGITS = (0 .< MAX_BASE).map(_ -> 0)
[Int] NUMBERS
F calc_num(=depth, &used)
‘ calculate the number at a given recurse depth ’
V result = Int64(0)
I depth < 3
R
L(i) 1 .< :MAX_BASE
I used[i] > 0
result += Int64(used[i]) * :POWER_DIGIT[depth][i]
I result != 0
V (num, rnum) = (result, Int64(1))
L rnum != 0
rnum = num I/ :MAX_BASE
used[Int(num - rnum * :MAX_BASE)]--
num = rnum
depth--
I depth == 0
V i = 1
L i < :MAX_BASE & used[i] == 0
i++
I i >= :MAX_BASE
:NUMBERS.append(Int(result))
F next_digit(=dgt, depth) -> N
‘ get next digit at the given depth ’
I depth < :MAX_BASE - 1
L(i) dgt .< :MAX_BASE
:USED_DIGITS[dgt]++
next_digit(i, depth + 1)
:USED_DIGITS[dgt]--
I dgt == 0
dgt = 1
L(i) dgt .< :MAX_BASE
:USED_DIGITS[i]++
calc_num(depth, ©(:USED_DIGITS))
:USED_DIGITS[i]--
L(j) 1 .< MAX_BASE
L(k) 0 .< MAX_BASE
POWER_DIGIT[j][k] = POWER_DIGIT[j - 1][k] * k
next_digit(0, 0)
print(NUMBERS)
NUMBERS = Array(Set(NUMBERS))
NUMBERS.sort()
print(‘Own digits power sums for N = 3 to 9 inclusive:’)
L(n) NUMBERS
print(n)</syntaxhighlight>
{{out}}
<pre>
[9800817, 9800817, 24678050, 24678050, 146511208, 146511208, 146511208, 4210818, 24678051, 24678051, 8208, 370, 93084, 93084, 370, 370, 370, 370, 407, 407, 407, 407, 912985153, 1741725, 9926315, 153, 371, 1634, 1634, 1634, 153, 371, 153, 371, 371, 371, 92727, 92727, 92727, 472335975, 472335975, 472335975, 534494836, 534494836, 548834, 88593477, 88593477, 54748, 54748, 9474, 9474, 9474]
Own digits power sums for N = 3 to 9 inclusive:
153
370
371
407
1634
8208
9474
54748
92727
93084
548834
1741725
4210818
9800817
9926315
24678050
24678051
88593477
146511208
472335975
534494836
912985153
</pre>
=={{header|ALGOL 68}}==
Non-recursive, generates the possible combinations ands the own digits power sums in reverse order, without duplication.<br>
Uses ideas from various solutions on this page, particularly the observation that the own digits power sum is independent of the order of the digits. Uses the minimum possible highest digit for the number of digits (width) and maximum number of zeros for the width to avoid some combinations. This trys 73 359 combinations.
<
# counts of used digits, check is a copy used to check the number is an own digit power sum #
[ 0 : 9 ]INT used, check; FOR i FROM 0 TO 9 DO check[ i ] := 0 OD;
Line 157 ⟶ 245:
OD;
print( ( "Considered ", whole( try count, 0 ), " digit combinations" ) )
END</
{{out}}
<pre>
Line 185 ⟶ 273:
Considered 73359 digit combinations
</pre>
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">loop 3..8 'nofDigits ->
loop (10 ^ nofDigits-1)..dec 10^nofDigits 'n ->
if n = sum map digits n => [& ^ nofDigits] ->
print n</syntaxhighlight>
{{out}}
<pre>153
370
371
407
1634
8208
9474
54748
92727
93084
548834
1741725
4210818
9800817
9926315
24678050
24678051
88593477</pre>
=={{header|C}}==
Line 190 ⟶ 306:
Takes about 1.9 seconds to run (GCC 9.3.0 -O3).
{{trans|Wren}}
<
#include <math.h>
Line 245 ⟶ 361:
}
return 0;
}</
{{out}}
Line 255 ⟶ 371:
{{trans|Pascal}}
Down now to 14ms.
<
#include <string.h>
Line 346 ⟶ 462:
for (i = 0; i <= j; ++i) printf("%lld\n", numbers[i]);
return 0;
}</
{{out}}
Line 352 ⟶ 468:
Same as before.
</pre>
=={{header|C++}}==
<syntaxhighlight lang="c++">
#include <cmath>
#include <cstdint>
#include <iostream>
#include <vector>
int main() {
std::vector<uint32_t> powers = { 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 };
std::cout << "Own digits power sums for N = 3 to 9 inclusive:" << std::endl;
for ( uint32_t n = 3; n <= 9; ++n ) {
for ( uint32_t d = 2; d <= 9; ++d ) {
powers[d] *= d;
}
uint64_t number = std::pow(10, n - 1);
uint64_t maximum = number * 10;
uint32_t last_digit = 0;
uint32_t digit_sum = 0;
while ( number < maximum ) {
if ( last_digit == 0 ) {
digit_sum = 0;
uint64_t copy = number;
while ( copy > 0 ) {
digit_sum += powers[copy % 10];
copy /= 10;
}
} else if ( last_digit == 1 ) {
digit_sum++;
} else {
digit_sum += powers[last_digit] - powers[last_digit - 1];
}
if ( digit_sum == number ) {
std::cout << number << std::endl;
if ( last_digit == 0 ) {
std::cout << number + 1 << std::endl;
}
number += 10 - last_digit;
last_digit = 0;
} else if ( digit_sum > number ) {
number += 10 - last_digit;
last_digit = 0;
} else if ( last_digit < 9 ) {
number++;
last_digit++;
} else {
number++;
last_digit = 0;
}
}
}
}
</syntaxhighlight>
{{ out }}
<pre>
Own digits power sums for N = 3 to 9 inclusive:
153
370
371
407
1634
8208
9474
54748
92727
93084
548834
1741725
4210818
9800817
9926315
24678050
24678051
88593477
146511208
472335975
534494836
912985153
</pre>
=={{header|D}}==
===Slow===
<syntaxhighlight lang="d">
import std.stdio;
import std.conv;
import std.algorithm;
import std.range;
bool isOwnDigitsPowerSum(uint n)
{
auto nStr = n.text;
return nStr.map!(d => (d - '0') ^^ nStr.length).sum == n;
}
void main()
{
iota(10^^2, 10^^9).filter!isOwnDigitsPowerSum.writeln;
}
</syntaxhighlight>
{{out}}
<pre>
[153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725, 4210818, 9800817, 9926315, 24678050, 24678051, 88593477, 146511208, 472335975, 534494836, 912985153]
</pre>
===Faster===
{{trans|Delphi}}
<syntaxhighlight lang="d">
import std.stdio;
const int[10][10] powerTable = [
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 2, 4, 8, 16, 32, 64, 128, 256, 512],
[1, 3, 9, 27, 81, 243, 729, 2_187, 6_561, 19_683],
[1, 4, 16, 64, 256, 1_024, 4_096, 16_384, 65_536, 262_144],
[1, 5, 25, 125, 625, 3125, 15_625, 78_125, 390_625, 1_953_125],
[1, 6, 36, 216, 1_296, 7_776, 46_656, 279_936, 1_679_616, 10_077_696],
[1, 7, 49, 343, 2_401, 16_807, 117_649, 823_543, 57_64_801, 40_353_607],
[1, 8, 64, 512, 4_096, 32_768, 262_144, 2_097_152, 16_777_216, 134_217_728],
[1, 9, 81, 729, 6_561, 59_049, 531_441, 4_782_969, 43_046_721, 387_420_489]
];
void digitsPowerSum(ref int Number, ref int DigitCount, int Level, int Sum) {
if (Level == 0) {
for (int Digits = 0; Digits <= 9; ++Digits) {
if (((Sum + powerTable[Digits][DigitCount]) == Number) && (Number >= 100)) {
writefln("%s: %s", DigitCount, Number);
}
++Number;
switch (Number) {
case 10:
case 100:
case 1_000:
case 10_000:
case 100_000:
case 1_000_000:
case 10_000_000:
case 100_000_000:
++DigitCount;
break;
default:
break;
}
}
}
else {
for (int Digits = 0; Digits <= 9; ++Digits) {
digitsPowerSum(Number, DigitCount, Level - 1, Sum + powerTable[Digits][DigitCount]);
}
}
}
void main() {
int Number = 0;
int DigitCount = 1;
//
digitsPowerSum(Number, DigitCount, 9-1, 0);
}
</syntaxhighlight>
{{out}}
<pre>
3: 153
3: 370
3: 371
3: 407
4: 1634
4: 8208
4: 9474
5: 54748
5: 92727
5: 93084
6: 548834
7: 1741725
7: 4210818
7: 9800817
7: 9926315
8: 24678050
8: 24678051
8: 88593477
9: 146511208
9: 472335975
9: 534494836
9: 912985153
</pre>
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
Started with brute force method which took over a minute to do the 8-digit problem. Then borrowed ideas from everywhere, especially the XPL0 implementation.
<syntaxhighlight lang="Delphi">
{Table to speed up power(X,Y) calculation }
const PowerTable: array [0..9] of array [0..9] of integer = (
(1, 0, 0, 0, 0, 0, 0, 0, 0, 0),
(1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
(1, 2, 4, 8, 16, 32, 64, 128, 256, 512),
(1, 3, 9, 27, 81, 243, 729, 2187, 6561, 19683),
(1, 4, 16, 64, 256, 1024, 4096, 16384, 65536, 262144),
(1, 5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125),
(1, 6, 36, 216, 1296, 7776, 46656, 279936, 1679616, 10077696),
(1, 7, 49, 343, 2401, 16807, 117649, 823543, 5764801, 40353607),
(1, 8, 64, 512, 4096, 32768, 262144, 2097152, 16777216, 134217728),
(1, 9, 81, 729, 6561, 59049, 531441, 4782969, 43046721, 387420489)
);
procedure DigitsPowerSum(Memo: TMemo; var Number, DigitCount: integer; Level, Sum: integer);
{Recursively process DigitCount power}
var Digits: integer;
begin
{Finish when recursion = Level Zero}
if Level = 0 then
begin
for Digits:= 0 to 9 do
begin
{Test combinations of digits and previous sum against number}
if ((Sum + PowerTable[Digits, DigitCount]) = Number) and
(Number >= 100) then
begin
Memo.Lines.Add(IntToStr(DigitCount)+' '+IntToStr(Number));
end;
Inc(Number);
{Keep track of digit count - increases on even power of 10}
case Number of
10, 100, 1000, 10000, 100000,
1000000, 10000000, 100000000: Inc(DigitCount);
end;
end;
end
else for Digits:= 0 to 9 do
begin
{Recurse through all digits/levels}
DigitsPowerSum(Memo, Number, DigitCount,
Level-1, Sum + PowerTable[Digits, DigitCount]);
end;
end;
procedure ShowDigitsPowerSum(Memo: TMemo);
var Number, DigitCount: integer;
begin
Number:= 0;
DigitCount:= 1;
DigitsPowerSum(Memo, Number, DigitCount, 9-1, 0);
end;
</syntaxhighlight>
{{out}}
<pre>
3 153
3 370
3 371
3 407
4 1634
4 8208
4 9474
5 54748
5 92727
5 93084
6 548834
7 1741725
7 4210818
7 9800817
7 9926315
8 24678050
8 24678051
8 88593477
9 146511208
9 472335975
9 534494836
9 912985153
Elapsed Time: 3.058 Sec.
</pre>
=={{header|EasyLang}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang=easylang>
fastfunc next curr n limit .
repeat
curr += 1
if curr = limit
return -1
.
sum = 0
tmp = curr
repeat
dig = tmp mod 10
tmp = tmp div 10
h = n
r = 1
while h > 0
r *= dig
h -= 1
.
sum += r
until tmp = 0
.
until sum = curr
.
return curr
.
for n = 3 to 8
curr = pow 10 (n - 1)
repeat
curr = next curr n pow 10 n
until curr = -1
print curr
.
.
</syntaxhighlight>
=={{header|F_Sharp|F#}}==
<
// Own digits power sum. Nigel Galloway: October 2th., 2021
let fN g=let N=[|for n in 0..9->pown n g|] in let rec fN g=function n when n<10->N.[n]+g |n->fN(N.[n%10]+g)(n/10) in (fun g->fN 0 g)
{3..9}|>Seq.iter(fun g->let fN=fN g in printf $"%d{g} digit are:"; {pown 10 (g-1)..(pown 10 g)-1}|>Seq.iter(fun g->if g=fN g then printf $" %d{g}"); printfn "")
</syntaxhighlight>
{{out}}
<pre>
Line 369 ⟶ 805:
9 digit are: 146511208 472335975 534494836 912985153
</pre>
=={{header|FreeBASIC}}==
<
dim as uinteger N, curr, temp, dig, sum
Line 385 ⟶ 822:
next curr
next N
</syntaxhighlight>
{{out}}
<pre>
Line 396 ⟶ 833:
{{libheader|Go-rcu}}
Takes about 16.5 seconds to run including compilation time.
<
import (
Line 447 ⟶ 884:
}
}
}</
{{out}}
Line 457 ⟶ 894:
Down to about 128 ms now including compilation time. Actual run time only 8 ms!
{{trans|Pascal}}
<
import "fmt"
Line 556 ⟶ 993:
fmt.Println(n)
}
}</
{{out}}
Line 565 ⟶ 1,002:
=={{header|Haskell}}==
Using a function from the Combinations with Repetitions task:
<
------------------- OWN DIGITS POWER SUM -----------------
Line 608 ⟶ 1,045:
digits :: Show a => a -> [Int]
digits n = (\x -> read [x] :: Int) <$> show n</
{{Out}}
<pre>N ∈ [3 .. 8]
Line 635 ⟶ 1,072:
534494836
912985153</pre>
=={{header|J}}==
See [[Narcissistic_decimal_number#J]]
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public final class OwnDigitsPowerSum {
public static void main(String[] args) {
List<Integer> powers = IntStream.rangeClosed(0, 9).boxed().map( i -> i * i ).collect(Collectors.toList());
System.out.println("Own digits power sums for N = 3 to 9 inclusive:");
for ( int n = 3; n <= 9; n++ ) {
for ( int d = 2; d <= 9; d++ ) {
powers.set(d, powers.get(d) * d);
}
long number = (long) Math.pow(10, n - 1);
long maximum = number * 10;
int lastDigit = 0;
int sum = 0;
while ( number < maximum ) {
if ( lastDigit == 0 ) {
sum = String.valueOf(number)
.chars().map(Character::getNumericValue).map( i -> powers.get(i) ).sum();
} else if ( lastDigit == 1 ) {
sum += 1;
} else {
sum += powers.get(lastDigit) - powers.get(lastDigit - 1);
}
if ( sum == number ) {
System.out.println(number);
if ( lastDigit == 0 ) {
System.out.println(number + 1);
}
number += 10 - lastDigit;
lastDigit = 0;
} else if ( sum > number ) {
number += 10 - lastDigit;
lastDigit = 0;
} else if ( lastDigit < 9 ) {
number += 1;
lastDigit += 1;
} else {
number += 1;
lastDigit = 0;
}
}
}
}
}
</syntaxhighlight>
{{ out }}
<pre>
Own digits power sums for N = 3 to 9 inclusive:
153
370
371
407
1634
8208
9474
54748
92727
93084
548834
1741725
4210818
9800817
9926315
24678050
24678051
88593477
146511208
472335975
534494836
912985153
</pre>
=={{header|jq}}==
{{trans|Wren|(recursive version)}}
{{works with|jq}}
'''Works with gojq, the Go implementation of jq''' (*)
(*) gojq requires significantly more time and memory to run this program.
<syntaxhighlight lang="jq">def maxBase: 10;
# The global object:
# { usedDigits, powerDgt, numbers }
def initPowerDgt:
reduce range(0; maxBase) as $i (null; .[$i] = [range(0;maxBase)|0])
| reduce range(1; maxBase) as $i (.; .[0][$i] = 1)
| reduce range(1; maxBase) as $j (.;
reduce range(0; maxBase) as $i (.;
.[$j][$i] = .[$j-1][$i] * $i )) ;
# Input: global object
# Output: .numbers
def calcNum($depth):
if $depth < 3 then .
else .usedDigits as $used
| .powerDgt as $powerDgt
| (reduce range(1; maxBase) as $i (0;
if $used[$i] > 0 then . + $used[$i] * $powerDgt[$depth][$i]
else . end )) as $result
| if $result == 0 then .
else {n: $result, $used, $depth, numbers, r: null}
| until (.r == 0;
.r = ((.n / maxBase) | floor)
| .used[.n - .r * maxBase] += -1
| .n = .r
| .depth += -1 )
| if .depth != 0 then .
else . + {i: 1}
| until( .i >= maxBase or .used[.i] != 0; .i += 1)
| if .i >= maxBase
then .numbers += [$result]
else .
end
end
end
end
| .numbers ;
# input: global object
# output: updated global object
def nextDigit($dgt; $depth):
if $depth < maxBase-1
then reduce range($dgt; maxBase) as $i (.;
.usedDigits[$dgt] += 1
| nextDigit($i; $depth+1)
| .usedDigits[$dgt] += -1 )
else .
end
| reduce range(if $dgt == 0 then 1 else $dgt end; maxBase) as $i (.;
.usedDigits[$i] += 1
| .numbers = calcNum($depth)
| .usedDigits[$i] += -1 ) ;
def main:
{ usedDigits: [range(0; maxBase)|0],
powerDgt: initPowerDgt,
numbers:[] }
| nextDigit(0; 0)
| .numbers
| unique[]
;
"Own digits power sums for N = 3 to 9 inclusive:",
main</syntaxhighlight>
{{out}}
As for [[#Wren|Wren]].
=={{header|Julia}}==
<
dig = digits(n, base=base)
exponent = length(dig)
Line 646 ⟶ 1,245:
isowndigitspowersum(i) && println(i)
end
</
<pre>
153
Line 672 ⟶ 1,271:
</pre>
=={{header|
{{trans|Wren}}
<syntaxhighlight lang="Nim">import std/[algorithm, sequtils, strutils]
const MaxBase = 10
type UsedDigits = array[MaxBase, int]
var
usedDigits: UsedDigits
numbers: seq[int]
proc initPowerDigits(): array[MaxBase, array[MaxBase, int]] {.compileTime.} =
for i in 1..<MaxBase:
for j in 1..<MaxBase:
for i in 0..<MaxBase:
result[j][i] = result[j - 1][i] * i
const PowerDigits = initPowerDigits()
proc calcNum(depth: int; used: UsedDigits) =
var used
var depth
if depth <
var result
for i
if used[i] > 0:
result += used[i] * PowerDigits[depth][i]
if result == 0: return
var n = result
while true:
dec used[n - r * MaxBase]
dec depth
if depth
var i = 1
while i < MaxBase and used[i] == 0:
if i ==
numbers.add result
proc nextDigit(digit, depth: int) =
var depth = depth
if depth < MaxBase
inc usedDigits[digit]
dec
let digit
for i in digit..<MaxBase:
inc
calcNum(depth, usedDigits)
dec usedDigits[i]
nextDigit(0, 0)
numbers.sort()
numbers = numbers.deduplicate(true)
echo "Own digits power sums for N = 3 to 9 inclusive:"
echo numbers.join("\n")
</syntaxhighlight>
{{out}}
<pre>Own digits power sums for N = 3 to 9 inclusive:
153
370
371
407
1634
8208
9474
54748
92727
93084
548834
1741725
4210818
9800817
9926315
24678050
24678051
88593477
146511208
472335975
534494836
912985153</pre>
=={{header|Pascal}}==
[[Narcissistic_decimal_number#alternative|Narcissistic decimal number]]
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">cf = Compile[{{n, _Integer}}, Module[{digs, len},
digs = IntegerDigits[n];
len =
Total[digs^len] == n
], CompilationTarget -> "C"];
Select[Range[100, 100000000 - 1], cf]</syntaxhighlight>
{{out}}
<pre>{153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725, 4210818, 9800817, 9926315, 24678050, 24678051, 88593477}</pre>
=={{header|PARI/GP}}==
You should have enough memory to avoid stack overflow and configure GP accordingly...
<syntaxhighlight lang="parigp">
select(is_Armstrong(n)={n==vecsum([d^#n|d<-n=digits(n)])}, [100..999999999])
</syntaxhighlight>
{{out}}
<pre>%1 = [153,370,371,407,1634,8208,9474,54748,92727,93084,548834,1741725,4210818,9800817,9926315, 24678050,24678051,88593477,146511208,472335975,534494836,912985153]</pre>
=={{header|Perl}}==
===Brute Force===
Use <code>Parallel::ForkManager</code> to obtain concurrency, trading some code complexity for less-than-infinite run time. Still <b>very</b> slow.
<
use warnings;
use feature 'say';
Line 937 ⟶ 1,434:
$pm->wait_all_children;
say $_ for sort { $a <=> $b } map { @$_ } values %own_dps;</
{{out}}
<pre>153
Line 965 ⟶ 1,462:
===Combinatorics===
Leverage the fact that all combinations of digits give same DPS. Much faster than brute force, as only non-redundant values tested.
<
use warnings;
use List::Util 'sum';
Line 980 ⟶ 1,477:
}
print join "\n", sort { $a <=> $b } @own_dps;</
{{out}}
<pre>153
Line 1,004 ⟶ 1,501:
534494836
912985153</pre>
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">minps</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">maxps</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">pdn</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">results</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">own_digit_power_sum</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">taken</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">at</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">cps</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">son</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">-- looking for n digit numbers, taken is the number of digits collected so far,
-- any further digits must be "at" or higher. cps is the current power sum and
-- son is the smallest ordered number from those digits (eg 47 not 407).
-- results collected in son order eg 370 407 153 371 (from 37 47 135 137).</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">taken</span><span style="color: #0000FF;">=</span><span style="color: #000000;">n</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">cps</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">minps</span>
<span style="color: #008080;">and</span> <span style="color: #000000;">cps</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">son</span> <span style="color: #008080;">then</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">scps</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cps</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">tcps</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">trim_head</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">scps</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"0"</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">sson</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">son</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">tcps</span><span style="color: #0000FF;">=</span><span style="color: #000000;">sson</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">results</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">results</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">scps</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">taken</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">=</span><span style="color: #000000;">at</span> <span style="color: #008080;">to</span> <span style="color: #000000;">9</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">ncps</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">cps</span><span style="color: #0000FF;">+</span><span style="color: #000000;">pdn</span><span style="color: #0000FF;">[</span><span style="color: #000000;">d</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">ncps</span><span style="color: #0000FF;">></span><span style="color: #000000;">maxps</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">own_digit_power_sum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">taken</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ncps</span><span style="color: #0000FF;">,</span><span style="color: #000000;">son</span><span style="color: #0000FF;">*</span><span style="color: #000000;">10</span><span style="color: #0000FF;">+</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">3</span> <span style="color: #008080;">to</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">machine_bits</span><span style="color: #0000FF;">()=</span><span style="color: #000000;">64</span><span style="color: #0000FF;">?</span><span style="color: #000000;">17</span><span style="color: #0000FF;">:</span><span style="color: #000000;">14</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">minps</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- eg 100</span>
<span style="color: #000000;">maxps</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span> <span style="color: #000080;font-style:italic;">-- eg 999</span>
<span style="color: #000000;">pdn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sq_power</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">),</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">results</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #000000;">own_digit_power_sum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">results</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d digits: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">results</span><span style="color: #0000FF;">)),</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
3 digits: 153 370 371 407
4 digits: 1634 8208 9474
5 digits: 54748 92727 93084
6 digits: 548834
7 digits: 1741725 4210818 9800817 9926315
8 digits: 24678050 24678051 88593477
9 digits: 146511208 472335975 534494836 912985153
10 digits: 4679307774
11 digits: 32164049650 32164049651 40028394225 42678290603 44708635679 49388550606 82693916578 94204591914
14 digits: 28116440335967
"8.8s"
</pre>
Takes about 3 times as long under p2js. You can push it a bit further on 64 bit, but unfortunately some discrepancies crept in at 19 digits, so I decided to call it a day at 17 digits (there are no 18 digit solutions).
<pre>
16 digits: 4338281769391370 4338281769391371
17 digits: 21897142587612075 35641594208964132 35875699062250035
"33.2s"
</pre>
=={{header|Python}}==
===Python :: Procedural===
==== slower ====
<
def isowndigitspowersum(integer):
Line 1,020 ⟶ 1,581:
if isowndigitspowersum(i):
print(i)
</
==== faster ====
{{trans|Wren}} Same output.
<
MAX_BASE = 10
Line 1,078 ⟶ 1,639:
print('Own digits power sums for N = 3 to 9 inclusive:')
for n in NUMBERS:
print(n)</
===Python :: Functional===
Using a function from the Combinations with Repetitions task:
<
from itertools import accumulate, chain, islice, repeat
Line 1,175 ⟶ 1,736:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>N ∈ [3 .. 8]
Line 1,202 ⟶ 1,763:
534494836
912985153</pre>
=={{header|Quackery}}==
<code>narcissistic</code> is defined at [[Narcissistic decimal number#Quackery]].
<syntaxhighlight lang="Quackery"> 100000000 100 - times
[ i^ 100 + narcissistic if
[ i^ 100 + echo cr ] ]
</syntaxhighlight>
{{out}}
<pre>153
370
371
407
1634
8208
9474
54748
92727
93084
548834
1741725
4210818
9800817
9926315
24678050
24678051
88593477</pre>
=={{header|Raku}}==
<syntaxhighlight lang="raku"
my %pow = (^10).map: { $_ => $_ ** $p };
my $start = 10 ** ($p - 1);
Line 1,219 ⟶ 1,810:
}
.say for unique sort @temp;
}</
{{out}}
<pre>153
Line 1,242 ⟶ 1,833:
=== Combinations with repetitions ===
Using code from [[Combinations with repetitions]] task, a version that runs relatively quickly, and scales well.
<syntaxhighlight lang="raku"
multi combs_with_rep (0, @ ) { () }
multi combs_with_rep ($, []) { () }
Line 1,257 ⟶ 1,848:
}
}
}</
{{out}}
<pre>153 370 371 407 1634 8208 9474 54748 92727 93084 548834 1741725 4210818 9800817 9926315 24678050 24678051 88593477 146511208 472335975 534494836 912985153</pre>
=={{header|Ring}}==
<
see "working..." + nl
see "Own digits power sum for N = 3 to 9 inclusive:" + nl
Line 1,282 ⟶ 1,873:
see "done..." + nl
</syntaxhighlight>
{{out}}
<pre>
Line 1,314 ⟶ 1,905:
=={{header|Ruby}}==
Repeated combinations allow for N=18 in less than a minute.
<syntaxhighlight lang
DIGITS = (0..9).to_a
range = (3..18)
Line 1,326 ⟶ 1,918:
end
puts "Own digits power sums for N = #{range}:", res
</syntaxhighlight>
{{out}}
<pre>
Own digits power sums for 3..18
153
370
Line 1,366 ⟶ 1,961:
35641594208964132
35875699062250035
</pre>
=={{header|Rust}}==
===Slow===
{{trans|D}}
<syntaxhighlight lang="rust">
fn is_own_digits_power_sum(n: u32) -> bool {
let n_str = n.to_string();
n_str.chars()
.map(|c| {
let digit = c.to_digit(10).unwrap();
digit.pow(n_str.len() as u32)
})
.sum::<u32>()
== n
}
fn main() {
let result: Vec<u32> = (10u32.pow(2)..10u32.pow(9))
.filter(|&n| is_own_digits_power_sum(n))
.collect();
println!("{:?}", result);
}
</syntaxhighlight>
{{out}}
<pre>
[153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725, 4210818, 9800817, 9926315, 24678050, 24678051, 88593477, 146511208, 472335975, 534494836, 912985153]
</pre>
===Faster===
{{trans|Wren}}
<syntaxhighlight lang="rust">
fn main() {
let mut powers = vec![0, 1, 4, 9, 16, 25, 36, 49, 64, 81];
println!("Own digits power sums for N = 3 to 9 inclusive:");
for n in 3..=9 {
for d in 2..=9 {
powers[d] *= d;
}
let mut i = 10u64.pow(n - 1);
let max = i * 10;
let mut last_digit = 0;
let mut sum = 0;
let mut digits: Vec<u32>;
while i < max {
if last_digit == 0 {
digits = i.to_string()
.chars()
.map(|c| c.to_digit(10).unwrap())
.collect();
sum = digits.iter().map(|&d| powers[d as usize]).sum();
} else if last_digit == 1 {
sum += 1;
} else {
sum += powers[last_digit as usize] - powers[(last_digit - 1) as usize];
}
if sum == i.try_into().unwrap() {
println!("{}", i);
if last_digit == 0 {
println!("{}", i + 1);
}
i += 10 - last_digit;
last_digit = 0;
} else if sum > i.try_into().unwrap() {
i += 10 - last_digit;
last_digit = 0;
} else if last_digit < 9 {
i += 1;
last_digit += 1;
} else {
i += 1;
last_digit = 0;
}
}
}
}
</syntaxhighlight>
{{out}}
<pre>
Own digits power sums for N = 3 to 9 inclusive:
153
370
371
407
1634
8208
9474
54748
92727
93084
548834
1741725
4210818
9800817
9926315
24678050
24678051
88593477
146511208
472335975
534494836
912985153
</pre>
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func armstrong_numbers(n, base=10) {
var D = @(^base)
var P = D.map {|d| d**n }
var list = []
D.combinations_with_repetition(n, {|*c|
var v = c.sum {|d| P[d] }
if (v.digits(base).sort == c) {
list.push(v)
}
})
list.sort
}
for n in (3..10) {
say ("For n = #{'%2d' % n}: ", armstrong_numbers(n))
}</syntaxhighlight>
{{out}}
<pre>
For n = 3: [153, 370, 371, 407]
For n = 4: [1634, 8208, 9474]
For n = 5: [54748, 92727, 93084]
For n = 6: [548834]
For n = 7: [1741725, 4210818, 9800817, 9926315]
For n = 8: [24678050, 24678051, 88593477]
For n = 9: [146511208, 472335975, 534494836, 912985153]
For n = 10: [4679307774]
</pre>
=={{header|Visual Basic .NET}}==
{{Trans|ALGOL 68}}
<
Option Explicit On
Line 1,561 ⟶ 2,302:
End Module</
{{out}}
<pre>
Line 1,594 ⟶ 2,335:
{{libheader|Wren-math}}
Includes some simple optimizations to try and quicken up the search. However, getting up to N = 9 still took a little over 4 minutes on my machine.
<
var powers = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Line 1,630 ⟶ 2,371:
}
}
}</
{{out}}
Line 1,663 ⟶ 2,404:
{{libheader|Wren-seq}}
Astonishing speed-up. Runtime now only 0.5 seconds!
<
var maxBase = 10
Line 1,722 ⟶ 2,463:
numbers.sort()
System.print("Own digits power sums for N = 3 to 9 inclusive:")
System.print(numbers.map { |n| n.toString }.join("\n"))</
{{out}}
<pre>
Same as iterative version.
</pre>
=={{header|XPL0}}==
Slow (14.4 second) recursive solution on a Pi4.
<syntaxhighlight lang="xpl0">int Num, NumLen, PowTbl(10, 10);
proc PowSum(Lev, Sum); \Display own digits power sum
int Lev, Sum, Dig;
[if Lev = 0 then
[for Dig:= 0 to 9 do
[if Sum + PowTbl(Dig, NumLen) = Num and Num >= 100 then
[IntOut(0, Num); CrLf(0)];
Num:= Num+1;
case Num of
10, 100, 1_000, 10_000, 100_000, 1_000_000, 10_000_000, 100_000_000:
NumLen:= NumLen+1
other [];
];
]
else for Dig:= 0 to 9 do \recurse
PowSum(Lev-1, Sum + PowTbl(Dig, NumLen));
];
int Dig, Pow;
[for Dig:= 0 to 9 do \make digit power table (for speed)
[PowTbl(Dig, 1):= Dig;
for Pow:= 2 to 9 do
PowTbl(Dig, Pow):= PowTbl(Dig, Pow-1)*Dig;
];
Num:= 0;
NumLen:= 1;
PowSum(9-1, 0);
]</syntaxhighlight>
{{out}}
<pre>
153
370
371
407
1634
8208
9474
54748
92727
93084
548834
1741725
4210818
9800817
9926315
24678050
24678051
88593477
146511208
472335975
534494836
912985153
</pre>
|