Own digits power sum: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Added link to related task.)
m (→‎{{header|Wren}}: Changed to Wren S/H)
(36 intermediate revisions by 12 users not shown)
Line 1:
{{draft task}}
 
; Description
Line 21:
{{trans|Python}}
 
<langsyntaxhighlight 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)
Line 73:
print(‘Own digits power sums for N = 3 to 9 inclusive:’)
L(n) NUMBERS
print(n)</langsyntaxhighlight>
 
{{out}}
Line 106:
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.
<langsyntaxhighlight lang="algol68">BEGIN
# 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 245:
OD;
print( ( "Considered ", whole( try count, 0 ), " digit combinations" ) )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 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 278 ⟶ 306:
Takes about 1.9 seconds to run (GCC 9.3.0 -O3).
{{trans|Wren}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <math.h>
 
Line 333 ⟶ 361:
}
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 343 ⟶ 371:
{{trans|Pascal}}
Down now to 14ms.
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <string.h>
 
Line 434 ⟶ 462:
for (i = 0; i <= j; ++i) printf("%lld\n", numbers[i]);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 440 ⟶ 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#}}==
<langsyntaxhighlight lang="fsharp">
// 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>
</lang>
{{out}}
<pre>
Line 457 ⟶ 805:
9 digit are: 146511208 472335975 534494836 912985153
</pre>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
dim as uinteger N, curr, temp, dig, sum
 
Line 473 ⟶ 822:
next curr
next N
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 484 ⟶ 833:
{{libheader|Go-rcu}}
Takes about 16.5 seconds to run including compilation time.
<langsyntaxhighlight lang="go">package main
 
import (
Line 535 ⟶ 884:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 545 ⟶ 894:
Down to about 128 ms now including compilation time. Actual run time only 8 ms!
{{trans|Pascal}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 644 ⟶ 993:
fmt.Println(n)
}
}</langsyntaxhighlight>
 
{{out}}
Line 653 ⟶ 1,002:
=={{header|Haskell}}==
Using a function from the Combinations with Repetitions task:
<langsyntaxhighlight lang="haskell">import Data.List (sort)
 
------------------- OWN DIGITS POWER SUM -----------------
Line 696 ⟶ 1,045:
 
digits :: Show a => a -> [Int]
digits n = (\x -> read [x] :: Int) <$> show n</langsyntaxhighlight>
{{Out}}
<pre>N ∈ [3 .. 8]
Line 723 ⟶ 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}}==
Line 730 ⟶ 1,166:
 
(*) gojq requires significantly more time and memory to run this program.
<langsyntaxhighlight lang="jq">def maxBase: 10;
 
# The global object:
Line 795 ⟶ 1,231:
 
"Own digits power sums for N = 3 to 9 inclusive:",
main</langsyntaxhighlight>
{{out}}
As for [[#Wren|Wren]].
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">function isowndigitspowersum(n::Integer, base=10)
dig = digits(n, base=base)
exponent = length(dig)
Line 809 ⟶ 1,245:
isowndigitspowersum(i) && println(i)
end
</langsyntaxhighlight>{{out}}
<pre>
153
Line 834 ⟶ 1,270:
912985153
</pre>
 
=={{header|Nim}}==
{{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:
result[0][i] = 1
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 = used
var depth = depth
if depth < 3: return
var result = 0
for i in 1..<MaxBase:
if used[i] > 0:
result += used[i] * PowerDigits[depth][i]
 
if result == 0: return
var n = result
while true:
let r = n div MaxBase
dec used[n - r * MaxBase]
n = r
dec depth
if r == 0: break
if depth != 0: return
 
var i = 1
while i < MaxBase and used[i] == 0:
inc i
if i == MaxBase:
numbers.add result
 
 
proc nextDigit(digit, depth: int) =
var depth = depth
if depth < MaxBase - 1:
for i in digit..<MaxBase:
inc usedDigits[digit]
nextDigit(i, depth + 1)
dec usedDigits[digit]
let digit = if digit == 0: 1 else: digit
for i in digit..<MaxBase:
inc usedDigits[i]
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 = Length[digs];
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.
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 876 ⟶ 1,434:
$pm->wait_all_children;
 
say $_ for sort { $a <=> $b } map { @$_ } values %own_dps;</langsyntaxhighlight>
{{out}}
<pre>153
Line 904 ⟶ 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.
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use List::Util 'sum';
Line 919 ⟶ 1,477:
}
 
print join "\n", sort { $a <=> $b } @own_dps;</langsyntaxhighlight>
{{out}}
<pre>153
Line 945 ⟶ 1,503:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="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>
Line 986 ⟶ 1,544:
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,011 ⟶ 1,569:
===Python :: Procedural===
==== slower ====
<langsyntaxhighlight lang="python">""" Rosetta code task: Own_digits_power_sum """
 
def isowndigitspowersum(integer):
Line 1,023 ⟶ 1,581:
if isowndigitspowersum(i):
print(i)
</langsyntaxhighlight>{{out}}Same as Wren example. Takes over a half hour to run.
==== faster ====
{{trans|Wren}} Same output.
<langsyntaxhighlight lang="python">""" Rosetta code task: Own_digits_power_sum (recursive method)"""
 
MAX_BASE = 10
Line 1,081 ⟶ 1,639:
print('Own digits power sums for N = 3 to 9 inclusive:')
for n in NUMBERS:
print(n)</langsyntaxhighlight>
 
===Python :: Functional===
Using a function from the Combinations with Repetitions task:
<langsyntaxhighlight lang="python">'''Own digit power sums'''
 
from itertools import accumulate, chain, islice, repeat
Line 1,178 ⟶ 1,736:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>N ∈ [3 .. 8]
Line 1,205 ⟶ 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" perl6line>(3..8).map: -> $p {
my %pow = (^10).map: { $_ => $_ ** $p };
my $start = 10 ** ($p - 1);
Line 1,222 ⟶ 1,810:
}
.say for unique sort @temp;
}</langsyntaxhighlight>
{{out}}
<pre>153
Line 1,245 ⟶ 1,833:
=== Combinations with repetitions ===
Using code from [[Combinations with repetitions]] task, a version that runs relatively quickly, and scales well.
<syntaxhighlight lang="raku" perl6line>proto combs_with_rep (UInt, @ ) { * }
multi combs_with_rep (0, @ ) { () }
multi combs_with_rep ($, []) { () }
Line 1,260 ⟶ 1,848:
}
}
}</langsyntaxhighlight>
{{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}}==
<langsyntaxhighlight lang="ring">
see "working..." + nl
see "Own digits power sum for N = 3 to 9 inclusive:" + nl
Line 1,285 ⟶ 1,873:
 
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,317 ⟶ 1,905:
=={{header|Ruby}}==
Repeated combinations allow for N=18 in less than a minute.
<syntaxhighlight lang ="ruby">DIGITS = (0..9).to_a
DIGITS = (0..9).to_a
range = (3..18)
 
Line 1,329 ⟶ 1,918:
end
 
puts "Own digits power sums for N = #{range}:", res</lang>
</syntaxhighlight>
 
{{out}}
<pre>
<pre>Own digits power sums for 3..18
Own digits power sums for 3..18
153
370
Line 1,369 ⟶ 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}}==
<langsyntaxhighlight lang="ruby">func armstrong_numbers(n, base=10) {
 
var D = @(^base)
Line 1,391 ⟶ 2,096:
for n in (3..10) {
say ("For n = #{'%2d' % n}: ", armstrong_numbers(n))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,406 ⟶ 2,111:
=={{header|Visual Basic .NET}}==
{{Trans|ALGOL 68}}
<langsyntaxhighlight lang="vbnet">Option Strict On
Option Explicit On
 
Line 1,597 ⟶ 2,302:
 
 
End Module</langsyntaxhighlight>
{{out}}
<pre>
Line 1,630 ⟶ 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.
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
 
var powers = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Line 1,666 ⟶ 2,371:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,699 ⟶ 2,404:
{{libheader|Wren-seq}}
Astonishing speed-up. Runtime now only 0.5 seconds!
<langsyntaxhighlight ecmascriptlang="wren">import "./seq" for Lst
 
var maxBase = 10
Line 1,758 ⟶ 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"))</langsyntaxhighlight>
 
{{out}}
Line 1,767 ⟶ 2,472:
=={{header|XPL0}}==
Slow (14.4 second) recursive solution on a Pi4.
<langsyntaxhighlight XPL0lang="xpl0">int Num, NumLen, PowTbl(10, 10);
proc PowSum(Lev, Sum); \Display own digits power sum
int Lev, Sum, Dig;
Line 1,794 ⟶ 2,499:
NumLen:= 1;
PowSum(9-1, 0);
]</langsyntaxhighlight>
 
{{out}}
9,476

edits