Digit fifth powers: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(46 intermediate revisions by 21 users not shown)
Line 7:
Even though 1<sup>5</sup> = 1, it is not expressed as a ''sum'' (a sum being the summation of a list of two or more numbers), and is therefore not included.
<br><br>
 
=={{header|11l}}==
<syntaxhighlight lang="11l">F fifth_power_digit_sum(n)
R sum(String(n).map(c -> Int(c) ^ 5))
 
print(sum((2..999999).filter(i -> i == fifth_power_digit_sum(i))))</syntaxhighlight>
 
{{out}}
<pre>
443839
</pre>
 
=={{header|8080 Assembly}}==
<syntaxhighlight lang="asm">putch: equ 2 ; CP/M syscall to print a character
puts: equ 9 ; CP/M syscall to print a string
org 100h
; Find the sum of the 5-powers of the digits
; of the current number
sum5: mvi b,6 ; There are 6 digits
lxi h,dps ; Set the accumulator to zero
call dgzero
lxi d,cur ; Load the start of the current number
addpow: ldax d ; Get current digit
mov c,a ; Multiply by 6 (width of table)
add a
add c
add a
mvi h,0 ; HL = index of table entry
mov l,a
push d ; Keep pointer to current digit
lxi d,pow5 ; Add start address of pow5 table
dad d
xchg ; Let [DE] = n^5
lxi h,dps ; Get accumulator
call dgadd ; Add the current power to it
pop d ; Restore pointer to current digit
inx d
dcr b ; If we're not done yet, do the next digit
jnz addpow
lxi d,cur ; Is the result the same as the current number?
call dgcmp
jnz next ; If not, try the next number
lxi h,total ; But if so, it needs to be added to the total
call dgadd
xchg ; As well as printed
call dgout
next: lxi h,cur ; Increment the current number
call dginc
lxi d,max ; Have we reached the end yet?
call dgcmp
jnz sum5 ; If not, keep going
lxi d,stot
mvi c,puts
call 5
lxi h,total
jmp dgout
;;;;;;; Program data ;;;;;;;
; Table of powers of 5, stored as digits in low-endian order
pow5: db 0,0,0,0,0,0 ; 0 ^ 5
db 1,0,0,0,0,0 ; 1 ^ 5
db 2,3,0,0,0,0 ; 2 ^ 5
db 3,4,2,0,0,0 ; 3 ^ 5
db 4,2,0,1,0,0 ; 4 ^ 5
db 5,2,1,3,0,0 ; 5 ^ 5
db 6,7,7,7,0,0 ; 6 ^ 5
db 7,0,8,6,1,0 ; 7 ^ 5
db 8,6,7,2,3,0 ; 8 ^ 5
db 9,4,0,9,5,0 ; 9 ^ 5
; End of the search space (9^5 * 6)
max: db 4,9,2,4,5,3
; Variables
total: db 0,0,0,0,0,0 ; Total of all matching numbers
dps: db 0,0,0,0,0,0 ; Current sum of 5-powers of digits
cur: db 2,0,0,0,0,0 ; Current number to test (start at 2)
; Strings
nl: db 13,10,'$' ; Newline
stot: db 'Total: $'
;;;;;;; Math routines ;;;;;;
; Zero out [HL]
dgzero: push b ; Keep BC and HL
push h
xra a
mvi b,6
dgzl: mov m,a
inx h
dcr b
jnz dgzl
pop h ; Restore HL and BC
pop b
ret
; Increment [HL]
dginc: push h ; Keep HL
dgincl: inr m ; Increment current digit
mov a,m ; Load it into the accumulator
sui 10 ; Subtract 10 from it
jc dginco ; If there is no carry, we're done
mov m,a ; Otherewise, write it back
inx h ; And go increment the next digit
jmp dgincl
dginco: pop h ; Restore HL
ret
; Print the number in [HL]
dgout: push b ; Keep all registers
push d
push h
lxi b,6 ; Move to the last digit
dad b
dzero: dcr c ; Skip leading zeroes
jm restor ; Don't bother handling 0 case
dcx h ; Go back
mov a,m ; Get digit
ana a
jz dzero ; Keep going until we find a nonzero digit
dgprn: adi '0' ; Write the digit
mov e,a
push b ; CP/M syscall destroys registers
push h
mvi c,putch
call 5
pop h
pop b
dcx h
mov a,m
dcr c
jp dgprn
mvi c,puts ; Finally, print a newline
lxi d,nl
call 5
restor: pop h ; And restore the registers
pop d
pop b
ret
; Compare [DE] to [HL]
dgcmp: push b ; Keep the registers
push d
push h
mvi b,6
dgcmpl: ldax d ; Get [DE]
cmp m ; Compare to [HL]
jnz restor ; If unequal, this is the result
inx d ; Otherwise, compare next pair
inx h
dcr b
jnz dgcmpl
jmp restor
; Add [DE] to [HL]
dgadd: push b
push d
push h
lxi b,600h ; B = counter, C = carry
dgaddl: ldax d ; Get digit from [DE]
add m ; Add digit from [HL]
add c ; Carry the one
cpi 10 ; Is the result 10 or higher?
mvi c,0 ; Assume there will be no carry
jc dgwr ; If not, handle next digit
sui 10 ; But if so, subtract 10,
inr c ; And set the carry flag for the next digit
dgwr: mov m,a ; Store the resulting digit in [HL]
inx d ; Move the pointers
inx h
dcr b ; Any more digits?
jnz dgaddl
jmp restor</syntaxhighlight>
{{out}}
<pre>4150
4151
54748
92727
93084
194979
Total: 443839</pre>
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_Io;
 
procedure Digit_Fifth_Powers is
Line 37 ⟶ 209:
Put ("Sum: ");
Put_Line (Natural'Image (Sum));
end Digit_Fifth_Powers;</langsyntaxhighlight>
{{out}}
<pre> 4150
Line 50 ⟶ 222:
As noted by the Julia sample, we need only consider up to 6 digit numbers.<br>
Also note, the digit fifth power sum is independent of the order of the digits.
<langsyntaxhighlight lang="algol68">BEGIN
[]INT fifth = []INT( 0, 1, 2^5, 3^5, 4^5, 5^5, 6^5, 7^5, 8^5, 9^5 )[ AT 0 ];
# as observed by the Julia sample, 9^5 * 7 has only 6 digits whereas 9^5 * 6 has 6 digits #
Line 113 ⟶ 285:
print( ( newline ) );
print( ( "Total: ", whole( total, 0 ), newline ) )
END</langsyntaxhighlight>
{{out}}
<pre>
Numbers that are the sums of the fifth powers of their digits: 4150 4151 93084 92727 54748 194979
Total: 443839
</pre>
 
=={{header|APL}}==
{{works with|Dyalog APL}}
<syntaxhighlight lang="apl">+/(⊢(/⍨)(⊢=(+/5*⍨⍎¨∘⍕))¨)1↓⍳6×9*5</syntaxhighlight>
{{out}}
<pre>443839</pre>
 
=={{header|AppleScript}}==
Simple solution:
<syntaxhighlight lang="applescript">on join(lst, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set txt to lst as text
set AppleScript's text item delimiters to astid
return txt
end join
 
on digit5thPowers()
set sums to {}
set total to 0
repeat with n from (2 ^ 5) to ((9 ^ 5) * 6)
set temp to n
set sum to (temp mod 10) ^ 5
repeat while (temp > 9)
set temp to temp div 10
set sum to sum + (temp mod 10) ^ 5
end repeat
if (sum = n) then
set end of sums to n
set total to total + n
end if
end repeat
return join(sums, " + ") & " = " & total
end digit5thPowers
 
digit5thPowers()</syntaxhighlight>
 
{{output}}
<syntaxhighlight lang="applescript">"4150 + 4151 + 54748 + 92727 + 93084 + 194979 = 443839"</syntaxhighlight>
 
Faster alternative (about 55 times as fast) using the "start with the digits" approach suggested by other contributors. Its iterative structure requires prior knowledge that six digits will be needed.
 
<syntaxhighlight lang="applescript">on join(lst, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set txt to lst as text
set AppleScript's text item delimiters to astid
return txt
end join
 
on digit5thPowers()
set hits to {}
set total to 0
repeat with d1 from 1 to 9
set s1 to (d1 ^ 5)
repeat with d2 from 1 to d1
set s2 to s1 + (d2 ^ 5)
repeat with d3 from 0 to d2
set s3 to s2 + (d3 ^ 5)
repeat with d4 from 0 to d3
set s4 to s3 + (d4 ^ 5)
repeat with d5 from 0 to d4
set s5 to s4 + (d5 ^ 5)
repeat with d6 from 0 to d5
set sum to s5 + (d6 ^ 5) as integer
set temp to sum
set d to temp mod 10
set digits to {d1, d2, d3, d4, d5, d6}
repeat while (digits contains {d})
repeat with i from 1 to 6
if (digits's item i = d) then
set digits's item i to missing value
exit repeat
end if
end repeat
set temp to temp div 10
set d to temp mod 10
end repeat
if (((count digits each integer) = 0) and (sum > (2 ^ 5))) then
set end of hits to sum
set total to total + sum
end if
end repeat
end repeat
end repeat
end repeat
end repeat
end repeat
return join(hits, " + ") & " = " & total
end digit5thPowers
 
digit5thPowers()</syntaxhighlight>
 
{{output}}
<syntaxhighlight lang="applescript">"4150 + 4151 + 54748 + 92727 + 93084 + 194979 = 443839"</syntaxhighlight>
 
Recursive version of the above. This takes the power as a parameter and is believed to be good for powers between 3 and 13. (No matches found when the power is 12.)
 
<syntaxhighlight lang="applescript">on join(lst, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set txt to lst as text
set AppleScript's text item delimiters to astid
return txt
end join
 
on digitNthPowers(pwr)
if ((pwr < 2) or (pwr > 13)) then return missing value -- Clear non-starter or too high for AppleScript.
-- Trusting the theory in the Julia solution, work out how many digits are needed.
set digits to {missing value}
set digitCount to 1
repeat until ((9 ^ pwr) * digitCount < (10 ^ digitCount))
set digitCount to digitCount + 1
set end of digits to missing value
end repeat
set hits to {}
set total to 0
script o
on dnp(slot, dmin, dmax, sum)
-- Recursive handler. Inherits the variables set before this script object.
-- slot: current slot in digits.
-- dmin, dmax: range of digit values to try in it.
-- sum: sum of 5th powers at the calling level.
repeat with d from dmin to dmax
set digits's item slot to d
if (slot < digitCount) then
dnp(slot + 1, 0, d, sum + d ^ pwr)
else
copy digits to checklist
set sum to (sum + (d ^ pwr)) div 1
set temp to sum
set d to temp mod 10
repeat while (checklist contains {d})
repeat with i from 1 to digitCount
if (checklist's item i = d) then
set checklist's item i to missing value
exit repeat
end if
end repeat
set temp to temp div 10
set d to temp mod 10
end repeat
if (((count checklist each integer) = 0) and (sum > (2 ^ pwr))) then
set end of hits to sum
set total to total + sum
end if
end if
end repeat
end dnp
end script
o's dnp(1, 1, 9, 0.0)
if (hits = {}) then return missing value
return join(hits, " + ") & " = " & total
end digitNthPowers
 
join({digitNthPowers(4), digitNthPowers(5), digitNthPowers(13)}, linefeed)</syntaxhighlight>
 
{{output}}
<syntaxhighlight lang="applescript">"1634 + 8208 + 9474 = 19316
4150 + 4151 + 54748 + 92727 + 93084 + 194979 = 443839
5.64240140138E+11 = 5.64240140138E+11"</syntaxhighlight>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">fifthDigitSum?: function [n]->
n = sum map digits n 'd -> d^5
 
print dec sum select 1..1000000 => fifthDigitSum?</syntaxhighlight>
 
{{out}}
 
<pre>443839</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f DIGIT_FIFTH_POWERS.AWK
BEGIN {
for (p=3; p<=6; p++) {
limit = 9^p*p
sum = 0
for (i=2; i<=limit; i++) {
if (i == main(i)) {
printf("%6d\n",i)
sum += i
}
}
printf("%6d power %d sum\n\n",sum,p)
}
exit(0)
}
function main(n, i,total) {
for (i=1; i<=length(n); i++) {
total += substr(n,i,1) ^ p
}
return(total)
}
</syntaxhighlight>
{{out}}
<pre>
153
370
371
407
1301 power 3 sum
 
1634
8208
9474
19316 power 4 sum
 
4150
4151
54748
92727
93084
194979
443839 power 5 sum
 
548834
548834 power 6 sum
</pre>
 
=={{header|BASIC}}==
==={{header|FreeBASIC}}===
<langsyntaxhighlight lang="freebasic">function dig5( n as uinteger ) as uinteger
dim as string ns = str(n)
dim as uinteger ret = 0
for i as ubyte = 21 to len(ns)
ret += val(mid(ns,i,1))^5
next i
Line 133 ⟶ 528:
dim as uinteger i, sum = 0
 
for i = 02 to 999999
if i = dig5(i) then
print i
Line 140 ⟶ 535:
next i
 
print "Their sum is ", sum</langsyntaxhighlight>
{{out}}<pre>
4150
Line 149 ⟶ 544:
194979
Their sum is 443839</pre>
 
==={{header|GW-BASIC}}===
<langsyntaxhighlight lang="gwbasic">10 SUM! = 0
20 FOR I! = 2 TO 999999!
30 GOSUB 80
Line 164 ⟶ 560:
130 NEXT J
140 RETURN
</syntaxhighlight>
</lang>
{{out}}<pre>
4150
Line 174 ⟶ 570:
Total = 443839</pre>
==={{header|QB64}}===
<langsyntaxhighlight lang="qbasic">CONST LIMIT& = 9 ^ 5 * 6 ' we don't need to search higher than this in base 10
DIM AS LONG num, sum, digitSum
DIM digit AS _BYTE
Line 198 ⟶ 594:
 
PRINT "The sum is"; sum
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 209 ⟶ 605:
The sum is 443839
</pre>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">Sum5 ← { 0:0; (𝕊⌊𝕩÷10) + (10|𝕩)⋆5 }
 
+´(⊢=Sum5)¨⊸/ 2↓↕6×9⋆5</syntaxhighlight>
{{out}}
<pre>443839</pre>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include<stdio.h>
#include<stdlib.h>
#include<math.h>
Line 230 ⟶ 633:
printf( "Total is %d\n", sum );
return 0;
}</langsyntaxhighlight>
{{out}}<pre>4150
4151
Line 238 ⟶ 641:
194979
Total is 443839</pre>
 
=={{header|C++}}==
Fast version. Checks numbers up to 399,999, which is above the requirement of 6 * 9<sup>5</sup> and well below the overkill value of 999,999.
<syntaxhighlight lang="cpp">#include <iostream>
#include <cmath>
#include <chrono>
 
using namespace std;
using namespace chrono;
 
int main() {
auto st = high_resolution_clock::now();
const uint i5 = 100000, i4 = 10000, i3 = 1000, i2 = 100, i1 = 10;
uint p4[] = { 0, 1, 32, 243 }, nums[10], p5[10], t = 0,
m5, m4, m3, m2, m1, m0; m5 = m4 = m3 = m2 = m1 = m0 = 0;
for (uint i = 0; i < 10; i++) p5[i] = pow(nums[i] = i, 5);
for (auto i : p4) { auto im = m5, ip = i; m4 = 0;
for (auto j : p5) { auto jm = im + m4, jp = ip + j; m3 = 0;
for (auto k : p5) { auto km = jm + m3, kp = jp + k; m2 = 0;
for (auto l : p5) { auto lm = km + m2, lp = kp + l; m1 = 0;
for (auto m : p5) { auto mm = lm + m1, mp = lp + m; m0 = 0;
for (auto n : p5) { auto nm = mm + m0++;
if (nm == mp + n && nm > 1) t += nm;
} m1 += i1; } m2 += i2; } m3 += i3; } m4 += i4; } m5 += i5; }
auto et = high_resolution_clock::now();
std::cout << t << " " <<
duration_cast<nanoseconds>(et - st).count() / 1000.0 << " μs";
}</syntaxhighlight>
{{out|Output @ Tio.run}}
<pre>443839 250.514 μs</pre>
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">sum5 = proc (n: int) returns (int)
sum: int := 0
while n > 0 do
sum := sum + (n//10) ** 5
n := n/10
end
return(sum)
end sum5
 
start_up = proc ()
po: stream := stream$primary_output()
total: int := 0
for i: int in int$from_to(2, 6*9**5) do
if sum5(i)=i then
total := total + i
stream$putright(po, int$unparse(i), 6)
stream$putc(po, '\n')
end
end
stream$putl(po, "------ +")
stream$putright(po, int$unparse(total), 6)
stream$putc(po, '\n')
end start_up</syntaxhighlight>
{{out}}
<pre> 4150
4151
54748
92727
93084
194979
------ +
443839</pre>
 
=={{header|COBOL}}==
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. DIGIT-FIFTH-POWER.
 
DATA DIVISION.
WORKING-STORAGE SECTION.
01 VARIABLES.
03 CANDIDATE PIC 9(6).
03 MAXIMUM PIC 9(6).
03 DIGITS PIC 9 OCCURS 6 TIMES,
REDEFINES CANDIDATE.
03 DIGIT PIC 9.
03 POWER-SUM PIC 9(6).
03 TOTAL PIC 9(6).
 
01 OUT-FORMAT.
03 OUT-NUM PIC Z(5)9.
 
PROCEDURE DIVISION.
BEGIN.
MOVE ZERO TO TOTAL.
COMPUTE MAXIMUM = 9 ** 5 * 6.
PERFORM TEST-NUMBER
VARYING CANDIDATE FROM 2 BY 1
UNTIL CANDIDATE IS GREATER THAN MAXIMUM.
DISPLAY '------ +'.
DISPLAY TOTAL.
STOP RUN.
 
TEST-NUMBER.
MOVE ZERO TO POWER-SUM.
PERFORM ADD-DIGIT-POWER
VARYING DIGIT FROM 1 BY 1
UNTIL DIGIT IS GREATER THAN 6.
IF POWER-SUM IS EQUAL TO CANDIDATE,
MOVE CANDIDATE TO OUT-NUM,
DISPLAY OUT-NUM,
ADD CANDIDATE TO TOTAL.
ADD-DIGIT-POWER.
COMPUTE POWER-SUM = POWER-SUM + DIGITS(DIGIT) ** 5.</syntaxhighlight>
{{out}}
<pre> 4150
4151
54748
92727
93084
194979
------ +
443839</pre>
 
=={{header|Comal}}==
<syntaxhighlight lang="comal">0010 FUNC sum5(n) CLOSED
0020 sum:=0
0030 WHILE n>0 DO sum:+(n MOD 10)^5;n:=n DIV 10
0040 RETURN sum
0050 ENDFUNC sum5
0060 //
0070 max:=9^5*6
0080 total:=0
0090 FOR i:=2 TO max DO
0100 IF i=sum5(i) THEN
0110 PRINT USING "######":i
0120 total:+i
0130 ENDIF
0140 ENDFOR i
0150 PRINT "------ +"
0160 PRINT USING "######":total
0170 END</syntaxhighlight>
{{out}}
<pre> 4150
4151
54748
92727
93084
194979
------ +
443839</pre>
 
=={{header|Cowgol}}==
<syntaxhighlight lang="cowgol">include "cowgol.coh";
 
sub pow5(n: uint32): (p: uint32) is
p := n*n * n*n * n;
end sub;
 
sub sum5(n: uint32): (r: uint32) is
r := 0;
while n != 0 loop
r := r + pow5(n % 10);
n := n / 10;
end loop;
end sub;
 
var total: uint32 := 0;
var n: uint32 := 2;
var max: uint32 := pow5(9) * 6;
 
while n <= max loop
if n == sum5(n) then
total := total + n;
print_i32(n);
print_nl();
end if;
n := n + 1;
end loop;
 
print("Total: ");
print_i32(total);
print_nl();</syntaxhighlight>
{{out}}
<pre>4150
4151
54748
92727
93084
194979
Total: 443839</pre>
 
=={{header|Factor}}==
Thanks to to the [http://rosettacode.org/wiki/Digit_fifth_powers#Julia Julia entry] for the tip about the upper bound of the search.
<langsyntaxhighlight lang="factor">USING: kernel math math.functions math.ranges math.text.utils
math.vectors prettyprint sequences ;
 
2 9 5 ^ 6 * [a,b] [ dup 1 digit-groups 5 v^n sum = ] filter sum .</langsyntaxhighlight>
{{out}}
<pre>
Line 251 ⟶ 837:
 
=={{header|Fermat}}==
<langsyntaxhighlight lang="fermat">Func Sumfp(n) = if n<10 then Return(n^5) else Return((n|10)^5 + Sumfp(n\10)) fi.;
sum:=0;
for i=2 to 999999 do if i=Sumfp(i) then sum:=sum+i; !!i fi od;
!!('The sum was ', sum );</langsyntaxhighlight>
{{out}}<pre>
4150
Line 263 ⟶ 849:
194979
The sum was 443839</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
Optimized for speed - runs in 60 ms on a Ryzen 7.
 
<syntaxhighlight lang="Delphi">
 
const Power5: array [0..9] of integer = (0,1,32,243,1024,3125,7776,16807,32768,59049);
 
function SumFifthPower(N: integer): integer;
var S: string;
var I: integer;
begin
S:=IntToStr(N);
Result:=0;
for I:=1 to Length(S) do
Result:=Result+Power5[byte(S[I])-$30];
end;
 
procedure ShowFiftPowerDigits(Memo: TMemo);
var I,Sum: integer;
begin
Sum:=0;
for I:=2 to 354424 do
begin
if I = SumFifthPower(I) then
begin
Memo.Lines.Add(Format('%8.0n',[I*1.0]));
Sum:=Sum+I;
end;
end;
Memo.Lines.Add('========');
Memo.Lines.Add(Format('%8.0n',[Sum*1.0]));
end;
 
</syntaxhighlight>
{{out}}
<pre>
4,150
4,151
54,748
92,727
93,084
194,979
========
443,839
 
</pre>
 
=={{header|FOCAL}}==
<syntaxhighlight lang="focal">01.10 S M=9^5*6
01.20 S T=0
01.30 F C=2,M;D 3
01.40 T "TOTAL",T,!
01.50 Q
 
02.10 S X=C
02.20 S S=0
02.30 S Y=FITR(X/10)
02.40 S S=S+(X-Y*10)^5
02.50 S X=Y
02.60 I (-X)2.3
 
03.10 D 2
03.20 I (C-S)3.5,3.3,3.5
03.30 T %6,C,!
03.40 S T=T+C
03.50 R</syntaxhighlight>
{{out}}
<pre>= 4150
= 4151
= 54748
= 92727
= 93084
= 194979
TOTAL= 443839</pre>
 
=={{header|Go}}==
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 300 ⟶ 963:
}
fmt.Printf(" = %d\n", sum)
}</langsyntaxhighlight>
 
{{out}}
Line 307 ⟶ 970:
4150 + 4151 + 54748 + 92727 + 93084 + 194979 = 443839
</pre>
 
=={{header|J}}==
<syntaxhighlight lang="j">(([=[:+/10&#.^:_1^5:)"0+/@#])2}.i.6*9^5</syntaxhighlight>
{{out}}
<pre>443839</pre>
 
=={{header|jq}}==
'''Adapted from [[#Julia|Julia]]
 
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
'''Preliminaries'''
<syntaxhighlight lang="jq"># To take advantage of gojq's arbitrary-precision integer arithmetic:
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in);
 
def sum(s): reduce s as $x (0; .+$x);
 
# Output: a stream of integers
def digits: tostring | explode[] | [.] | implode | tonumber;</syntaxhighlight>
'''The Task'''
<syntaxhighlight lang="jq"># Output: an array of i^5 for i in 0 .. 9 inclusive
def dp5: [range(0;10) | power(5)];
 
def task:
dp5 as $dp5
| ($dp5[9] * 6) as $limit
| sum( range(2; $limit + 1)
| sum( digits | $dp5[.] ) as $s
| select(. == $s) ) ;
 
"The sum of all numbers that can be written as the sum of the 5th powers of their digits is:", task</syntaxhighlight>
{{out}}
<pre>
The sum of all numbers that can be written as the sum of the 5th powers of their digits is:
443839
</pre>
 
 
=={{header|Julia}}==
In base 10, the largest digit is 9. If n is the number of digits, as n increases,
9^5 * n < 10^n. So we do not have to look beyond 9^5 * 6 since 9^5 * 6 < 1,000,000.
<langsyntaxhighlight lang="julia">println("Numbers > 1 that can be written as the sum of fifth powers of their digits:")
arr = [i for i in 2 : 9^5 * 6 if mapreduce(x -> x^5, +, digits(i)) == i]
println(join(arr, " + "), " = ", sum(arr))
</langsyntaxhighlight>{{out}}
<pre>
Numbers > 1 that can be written as the sum of fifth powers of their digits:
4150 + 4151 + 54748 + 92727 + 93084 + 194979 = 443839
</pre>
 
=={{header|MAD}}==
<syntaxhighlight lang="mad"> NORMAL MODE IS INTEGER
INTERNAL FUNCTION(X)
ENTRY TO POW5.
FUNCTION RETURN X * X * X * X * X
END OF FUNCTION
INTERNAL FUNCTION(N)
ENTRY TO SUM5.
CUR = N
SUM = 0
LOOP WHENEVER CUR.G.0
NEXT = CUR / 10
SUM = SUM + POW5.(CUR - NEXT*10)
CUR = NEXT
TRANSFER TO LOOP
END OF CONDITIONAL
FUNCTION RETURN SUM
END OF FUNCTION
LIMIT = POW5.(9) * 6
TOTAL = 0
THROUGH TEST, FOR I = 2, 1, I.GE.LIMIT
WHENEVER SUM5.(I).E.I
TOTAL = TOTAL + I
PRINT FORMAT NUM, I
END OF CONDITIONAL
TEST CONTINUE
 
PRINT FORMAT TOT, TOTAL
VECTOR VALUES NUM = $S7,I6*$
VECTOR VALUES TOT = $7HTOTAL: ,I6*$
END OF PROGRAM </syntaxhighlight>
{{out}}
<pre> 4150
4151
54748
92727
93084
194979
TOTAL: 443839</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">ClearAll[FifthPowerSumQ]
FifthPowerSumQ[n_Integer] := Total[IntegerDigits[n]^5] == n
sol = Select[Range[2, 10000000], FifthPowerSumQ]
Total[sol]</syntaxhighlight>
{{out}}
<pre>{4150, 4151, 54748, 92727, 93084, 194979}
443839</pre>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">sumfp(n)=if(n<10,n^5,(n%10)^5+sumfp(n\10));
s=0;
for(i=2,999999,if(i==sumfp(i),s=s+i;print(i)));
print("Total: ",s);</langsyntaxhighlight>
{{out}}<pre>4150
4151
Line 332 ⟶ 1,085:
194979
Total: 443839</pre>
 
=={{header|Pascal}}==
slightly modified [[Own_digits_power_sum]] checks decimals up to power 19.
<syntaxhighlight lang="pascal">program PowerOwnDigits2;
{$IFDEF FPC}
{$R+,O+}
{$MODE DELPHI}{$OPTIMIZATION ON,ALL}{$COPERATORS ON}
{$ELSE}
{$APPTYPE CONSOLE}
{$ENDIF}
uses
SysUtils,StrUtils;
const
CPU_hz = 1000*1000*1000;
const
MAXBASE = 10;
MaxDgtVal = MAXBASE - 1;
MaxDgtCount = 19;
type
tDgtCnt = 0..MaxDgtCount;
tValues = 0..MaxDgtVal;
tUsedDigits = array[tValues] of Int8;
tpUsedDigits = ^tUsedDigits;
 
tPower = array[tValues] of Uint64;
var
PowerDgt: tPower;
gblUD : tUsedDigits;
CombIdx : array of Int8;
Numbers : array of Uint64;
rec_cnt : NativeInt;
 
function GetCPU_Time: Uint64;
type
TCpu = record
HiCpu,
LoCpu : Dword;
end;
var
Cput : TCpu;
begin
{$ASMMODE INTEL}
asm
RDTSC;
MOV Dword Ptr [CpuT.LoCpu],EAX; MOV Dword Ptr [CpuT.HiCpu],EDX
end;
with Cput do result := Uint64(HiCPU) shl 32 + LoCpu;
end;
 
function InitCombIdx(ElemCount: Byte): pbyte;
begin
setlength(CombIdx, ElemCount + 1);
Fillchar(CombIdx[0], sizeOf(CombIdx[0]) * (ElemCount + 1), #0);
Result := @CombIdx[0];
Fillchar(gblUD[0], sizeOf(tUsedDigits), #0);
gblUD[0]:= 1;
end;
 
function Init(ElemCount:byte;Expo:byte):pByte;
var
pP1 : pUint64;
p: Uint64;
i,j: Int32;
begin
pP1 := @PowerDgt[0];
for i in tValues do
Begin
p := 1;
for j := 1 to Expo do
p *= i;
pP1[i] := p;
end;
result := InitCombIdx(ElemCount);
gblUD[0]:= 1;
end;
 
function GetPowerSum(minpot:nativeInt;digits:pbyte;var UD :tUsedDigits):NativeInt;
var
res,r : Uint64;
dgt :Int32;
begin
dgt := minpot;
res := 0;
repeat
dgt -=1;
res += PowerDgt[digits[dgt]];
until dgt=0;
result := 0;
//convert res into digits
repeat
r := res DIV MAXBASE;
result+=1;
dgt := res-r*MAXBASE;
//substract from used digits
UD[dgt] -= 1;
res := r;
until r = 0;
end;
 
procedure calcNum(minPot:Int32;digits:pbyte);
var
UD :tUsedDigits;
res: Uint64;
i: nativeInt;
begin
UD := gblUD;
i:= GetPowerSum(minpot,digits,UD);
if i = minPot then
Begin
//don't check 0
i := 1;
repeat
If UD[i] <> 0 then
Break;
i +=1;
until i > MaxDgtVal;
 
if i > MaxDgtVal then
begin
res := 0;
for i := minpot-1 downto 0 do
res += PowerDgt[digits[i]];
setlength(Numbers, Length(Numbers) + 1);
Numbers[high(Numbers)] := res;
end;
end;
end;
 
function NextCombWithRep(pComb: pByte;pUD :tpUsedDigits;MaxVal, ElemCount: UInt32): boolean;
var
i,dgt: NativeInt;
begin
i := -1;
repeat
i += 1;
dgt := pComb[i];
if dgt < MaxVal then
break;
dec(pUD^[dgt]);
until i >= ElemCount;
Result := i >= ElemCount;
 
if i = 0 then
begin
dec(pUD^[dgt]);
dgt +=1;
pComb[i] := dgt;
inc(pUD^[dgt]);
end
else
begin
dec(pUD^[dgt]);
dgt +=1;
pUD^[dgt]:=i+1;
repeat
pComb[i] := dgt;
i -= 1;
until i < 0;
end;
end;
 
var
digits : pByte;
T0,T1 : UInt64;
tmp : Uint64;
Pot,dgtCnt,i, j : Int32;
 
begin
T0 := GetCPU_Time;
For pot := 2 to MaxDgtCount do
begin
Write('Exponent : ',Pot,' used ');
T1 := GetCPU_Time;
digits := Init(MaxDgtCount,pot);
rec_cnt := 0;
// i > 0
For dgtCnt := 2 to pot+1 do
Begin
digits := InitCombIdx(Pot);
repeat
calcnum(dgtCnt,digits);
inc(rec_cnt);
until NextCombWithRep(digits,@gblUD,MaxDgtVal,dgtCnt);
end;
writeln(rec_cnt,' recursions in ',(GetCPU_Time-T1)/CPU_hz:0:6,' GigaCyles');
If length(numbers) > 0 then
Begin
//sort
for i := 0 to High(Numbers) - 1 do
for j := i + 1 to High(Numbers) do
if Numbers[j] < Numbers[i] then
begin
tmp := Numbers[i];
Numbers[i] := Numbers[j];
Numbers[j] := tmp;
end;
 
tmp := 0;
for i := 0 to High(Numbers)-1 do
begin
write(Numb2USA(IntToStr(Numbers[i])),' + ');
tmp +=Numbers[i];
end;
write(Numb2USA(IntToStr(Numbers[High(Numbers)])),' = ');
tmp +=Numbers[High(Numbers)];
writeln('sum to ',Numb2USA(IntToStr(tmp)));
end;
writeln;
setlength(Numbers,0);
end;
T0 := GetCPU_Time-T0;
Writeln('Max Uint64 ',Numb2USA(IntToStr(High(Uint64))));
writeln('Total runtime : ',T0/CPU_hz:0:6,' GigaCyles');
{$IFDEF WINDOWS}
readln;
{$ENDIF}
setlength(CombIdx,0);
end.</syntaxhighlight>
{{out|Output @ Tio.run}}
<pre style="height:260px">
TIO.RUN User time: 12.905 s //Total runtime : 29.470650 GigaCyles estimated ~2,28 Ghz
Exponent : 2 used 275 recursions in 0.000030 GigaCyles
 
Exponent : 3 used 990 recursions in 0.000161 GigaCyles
153 + 370 + 371 + 407 = sum to 1,301
 
Exponent : 4 used 2992 recursions in 0.000367 GigaCyles
1,634 + 8,208 + 9,474 = sum to 19,316
 
Exponent : 5 used 7997 recursions in 0.001193 GigaCyles // /2.28 -> 523 µs
4,150 + 4,151 + 54,748 + 92,727 + 93,084 + 194,979 = sum to 443,839
 
Exponent : 6 used 19437 recursions in 0.003013 GigaCyles
548,834 = sum to 548,834
 
Exponent : 7 used 43747 recursions in 0.007827 GigaCyles
1,741,725 + 4,210,818 + 9,800,817 + 9,926,315 + 14,459,929 = sum to 40,139,604
 
Exponent : 8 used 92367 recursions in 0.017619 GigaCyles
24,678,050 + 24,678,051 + 88,593,477 = sum to 137,949,578
 
Exponent : 9 used 184745 recursions in 0.037308 GigaCyles
146,511,208 + 472,335,975 + 534,494,836 + 912,985,153 = sum to 2,066,327,172
 
Exponent : 10 used 352705 recursions in 0.090797 GigaCyles
4,679,307,774 = sum to 4,679,307,774
 
Exponent : 11 used 646635 recursions in 0.207819 GigaCyles
32,164,049,650 + 32,164,049,651 + 40,028,394,225 + 42,678,290,603 + 44,708,635,679 + 49,388,550,606 + 82,693,916,578 + 94,204,591,914 = sum to 418,030,478,906
 
Exponent : 12 used 1144055 recursions in 0.295691 GigaCyles
 
Exponent : 13 used 1961245 recursions in 0.532789 GigaCyles
564,240,140,138 = sum to 564,240,140,138
 
Exponent : 14 used 3268749 recursions in 0.937579 GigaCyles
28,116,440,335,967 = sum to 28,116,440,335,967
 
Exponent : 15 used 5311724 recursions in 1.623457 GigaCyles
 
Exponent : 16 used 8436274 recursions in 2.680338 GigaCyles
4,338,281,769,391,370 + 4,338,281,769,391,371 = sum to 8,676,563,538,782,741
 
Exponent : 17 used 13123099 recursions in 4.432118 GigaCyles
233,411,150,132,317 + 21,897,142,587,612,075 + 35,641,594,208,964,132 + 35,875,699,062,250,035 = sum to 93,647,847,008,958,559
 
Exponent : 18 used 20029999 recursions in 7.169892 GigaCyles
 
Exponent : 19 used 30045004 recursions in 11.431518 GigaCyles
1,517,841,543,307,505,039 + 3,289,582,984,443,187,032 + 4,498,128,791,164,624,869 + 4,929,273,885,928,088,826 = sum to 14,234,827,204,843,405,766
 
Max Uint64 18,446,744,073,709,551,615
Total runtime : 29.470650 GigaCyles</pre>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 345 ⟶ 1,371:
}
say "\nSum of powers of n**$power: " . join(' + ', @matches) . ' = ' . sum @matches;
}</langsyntaxhighlight>
{{out}}
<pre>Sum of powers of n**3: 153 + 370 + 371 + 407 = 1301
Line 351 ⟶ 1,377:
Sum of powers of n**5: 4150 + 4151 + 54748 + 92727 + 93084 + 194979 = 443839
Sum of powers of n**6: 548834 = 548834</pre>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">sum5</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: #008080;">return</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;"><</span><span style="color: #000000;">10</span><span style="color: #0000FF;">?</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">):</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">),</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">sum5</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">/</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">total</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">6</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">sum5</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">total</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">i</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: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s = %d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" + "</span><span style="color: #0000FF;">),</span><span style="color: #000000;">total</span><span style="color: #0000FF;">})</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
4150 + 4151 + 54748 + 92727 + 93084 + 194979 = 443839
</pre>
 
=={{header|PicoLisp}}==
<syntaxhighlight lang="picolisp">
(de sum5th (N)
(sum
'((D) (** (format D) 5))
(chop N)))
 
(setq solutions
(cdr # exclude 1
(make
(for N `(* 6 (** 9 5))
(when (= N (sum5th N))
(link N))))))
 
(prinl "The numbers that can be written as the sum of the 5th power of their digits are:" )
(prin " ") (println solutions)
(prinl "Their sum is " (apply + solutions))
(bye)
</syntaxhighlight>
{{Out}}
<pre>
The numbers that can be written as the sum of the 5th power of their digits are:
(4150 4151 54748 92727 93084 194979)
Their sum is 443839
</pre>
=={{header|PILOT}}==
<syntaxhighlight lang="pilot">C :max=9*(9*(9*(9*(9*6))))
:sum=0
:n=2
*number
C :dps=0
:cur=n
*digit
C :next=cur/10
:d=cur-(next*10)
:dps=dps+d*(d*(d*(d*#d)))
:cur=next
J (cur>0):*digit
T (dps=n):#n
C (dps=n):sum=sum+n
:n=n+1
J (n<max):*number
T :Total: #sum
E :</syntaxhighlight>
{{out}}
<pre>4150
4151
54748
92727
93084
194979
Total: 443839</pre>
 
=={{header|PL/M}}==
<syntaxhighlight lang="pli">100H:
/* BDOS ROUTINES */
BDOS: PROCEDURE (F,A); DECLARE F BYTE, A ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
PUT$CHAR: PROCEDURE (C); DECLARE C BYTE; CALL BDOS(2,C); END PUT$CHAR;
PRINT: PROCEDURE (S); DECLARE S ADDRESS; CALL BDOS(9,S); END PRINT;
NEW$LINE: PROCEDURE; CALL PRINT(.(13,10,'$')); END NEW$LINE;
 
/* THE NATIVE INTEGER TYPES ARE NOT BIG ENOUGH, SO WE NEED TO
MAKE OUR OWN */
DECLARE DGT$SIZE LITERALLY '6';
MAKE$DEC: PROCEDURE (N, BUF) ADDRESS;
DECLARE (N, BUF) ADDRESS, (I, D BASED BUF) BYTE;
DO I=0 TO DGT$SIZE-1;
D(I) = N MOD 10;
N = N/10;
END;
RETURN BUF;
END MAKE$DEC;
 
ADD: PROCEDURE (ACC, ADDEND) ADDRESS;
DECLARE (ACC, ADDEND) ADDRESS;
DECLARE (I, C, A BASED ACC, D BASED ADDEND) BYTE;
C = 0;
DO I=0 TO DGT$SIZE-1;
A(I) = A(I) + D(I) + C;
IF A(I) < 10 THEN
C = 0;
ELSE DO;
A(I) = A(I) - 10;
C = 1;
END;
END;
RETURN ACC;
END ADD;
 
INCR: PROCEDURE (N);
DECLARE N ADDRESS, (I, D BASED N) BYTE;
DO I=0 TO DGT$SIZE-1;
IF (D(I) := D(I) + 1) < 10 THEN
RETURN;
ELSE
D(I) = 0;
END;
END INCR;
 
EQUAL: PROCEDURE (A, B) BYTE;
DECLARE (A, B) ADDRESS, (DA BASED A, DB BASED B, I) BYTE;
DO I=0 TO DGT$SIZE-1;
IF DA(I) <> DB(I) THEN RETURN 0;
END;
RETURN 0FFH;
END EQUAL;
 
PRINT$NUM: PROCEDURE (N);
DECLARE N ADDRESS, (I, D BASED N) BYTE;
I = DGT$SIZE-1;
DO WHILE I <> -1 AND D(I) = 0;
I = I-1;
END;
DO WHILE I <> -1;
CALL PUT$CHAR('0' + D(I));
I = I-1;
END;
END PRINT$NUM;
 
/* GENERATE A TABLE OF DIGIT POWERS BEFOREHAND */
DECLARE NATIVE$POWER$5 (10) ADDRESS INITIAL
(0, 1, 32, 243, 1024, 3125, 7776, 16807, 32768, 59049);
DECLARE POWER$5 (10) ADDRESS;
DECLARE POWER$BUF (60) BYTE;
DECLARE P BYTE;
DO P=0 TO 9;
POWER$5(P) = MAKE$DEC(NATIVE$POWER$5(P), .POWER$BUF(DGT$SIZE * P));
END;
 
/* DIGITS OF SEARCH LIMIT (9**5 * 6) IN LOW ENDIAN ORDER */
DECLARE MAX (DGT$SIZE) BYTE INITIAL (4,9,2,4,5,3);
 
/* SUM THE 5-POWERS OF THE DIGITS OF N */
SUM$5: PROCEDURE (N, BUF) ADDRESS;
DECLARE (N, BUF) ADDRESS, (I, D BASED N) BYTE;
BUF = MAKE$DEC(0, BUF);
DO I=0 TO DGT$SIZE-1;
BUF = ADD(BUF, POWER$5(D(I)));
END;
RETURN BUF;
END SUM$5;
 
DECLARE CUR (DGT$SIZE) BYTE INITIAL (2,0,0,0,0,0);
DECLARE TOTAL$BUF (DGT$SIZE) BYTE;
DECLARE TOTAL ADDRESS;
TOTAL = MAKE$DEC(0, .TOTAL$BUF);
 
/* TEST EACH NUMBER */
DO WHILE NOT EQUAL(.CUR, .MAX);
IF EQUAL(SUM5(.CUR, .MEMORY), .CUR) THEN DO;
TOTAL = ADD(TOTAL, .CUR);
CALL PRINT$NUM(.CUR);
CALL NEWLINE;
END;
CALL INCR(.CUR);
END;
 
CALL PRINT(.'TOTAL: $');
CALL PRINT$NUM(TOTAL);
CALL NEWLINE;
CALL EXIT;
EOF</syntaxhighlight>
{{out}}
<pre>4150
4151
54748
92727
93084
194979
TOTAL: 443839</pre>
 
=={{header|Python}}==
Comparing conventional vs. faster.
<lang>print(sum([n for n in range(2, 6*9**5) if sum(int(i)**5 for i in str(n)) == n]))</lang>
<syntaxhighlight lang="python">from time import time
 
# conventional
st = time()
print(sum([n for n in range(2, 6*9**5) if sum(int(i)**5 for i in str(n)) == n]), " ", (time() - st) * 1000, "ms")
 
# faster
st = time()
nums = list(range(10))
nu = list(range(((6 * 9**5) // 100000) + 1))
numbers = []
p5 = []
for i in nums: p5.append(i**5)
for i in nu:
im = i * 100000
ip = p5[i]
for j in nums:
jm = im + 10000 * j
jp = ip + p5[j]
for k in nums:
km = jm + 1000 * k
kp = jp + p5[k]
for l in nums:
lm = km + 100 * l
lp = kp + p5[l]
for m in nums:
mm = lm + 10 * m
mp = lp + p5[m]
for n in nums:
nm = mm + n
np = mp + p5[n]
if np == nm:
if nm > 1: numbers.append(nm)
print(sum(numbers), " ", (time() - st) * 1000, "ms", end = "")</syntaxhighlight>
{{out|Output @ Tio.run}}
<pre>443839 195.04594802856445 ms
443839 22.282838821411133 ms</pre>Around eight times faster.
 
 
=={{header|Quackery}}==
 
Credit to the Julia example for deducing that 9^5*6 is an upper bound.
 
The <code>1 -</code> at the end is to deduct the precluded solution, <code>1</code>.
 
<syntaxhighlight lang="Quackery"> [ [] swap
[ 10 /mod
rot join swap
dup 0 = until ]
drop ] is digits ( n --> [ )
 
0
9 5 ** 6 * times
[ i^ 0 over digits
witheach [ 5 ** + ]
= if [ i^ + ] ]
1 - echo</syntaxhighlight>
 
{{out}}
 
<pre>443839</pre>
 
=={{header|Raku}}==
 
<syntaxhighlight lang="raku" perl6line>print q:to/EXPANATION/;
Sum of all integers (except 1 for some mysterious reason ¯\_(ツ)_/¯),
for which the individual digits to the nth power sum to itself.
Line 370 ⟶ 1,651:
my $threshold = 9**$power * $power;
put .join(' + '), ' = ', .sum with cache
(2..$threshold).racehyper.map: {
state %p = ^10 .map: { $_ => $_ ** $power };
$_ if %p{.comb}.sum == $_
}
}</langsyntaxhighlight>
{{out}}
<pre>Sum of all integers (except 1 for some mysterious reason ¯\_(ツ)_/¯),
Line 390 ⟶ 1,671:
 
Sum of powers of n⁸: 24678050 + 24678051 + 88593477 = 137949578</pre>
 
=={{header|REXX}}==
<syntaxhighlight lang="rexx">/* numbers that are equal to the sum of their digits raised to the power 5 */
 
maximum = 9**5 * 6
total = 0
out = ''
do i = 2 to maximum
if sum5(i) = i then do
if out \= '' then out = out || ' + '
out = out || i
total = total + i
end
end
say out || ' = ' || total
exit
 
sum5: procedure
arg num
result = 0
do i = 1 to length(num)
result = result + substr(num, i, 1) ** 5
end
return result</syntaxhighlight>
{{out}}
<pre>4150 + 4151 + 54748 + 92727 + 93084 + 194979 = 443839</pre>
 
=={{header|Ring}}==
===Conventional===
<lang ring>? "working..."
<syntaxhighlight lang="ring">? "working..."
sumEnd = 0
Line 421 ⟶ 1,729:
? "The sum of all the numbers that can be written as the sum of fifth powers of their digits:"
? substr(sumList, 1, len(sumList) - 2) + "= " + sumEnd
? "done..."</langsyntaxhighlight>
{{out}}
<pre>
Line 428 ⟶ 1,736:
4150 + 4151 + 54748 + 92727 + 93084 + 194979 = 443839
done...
</pre>
 
===Faster===
Around six times faster than the conventional version.
<syntaxhighlight lang="ring">st = clock()
lst9 = 1:10 lst3 = 1:4
p5 = [] m5 = [] m4 = [] m3 = [] m2 = [] m1 = []
for i in lst9
add(p5, pow(i - 1, 5)) add(m1, (i - 1) * 10) add(m2, m1[i] * 10)
add(m3, m2[i] * 10) add(m4, m3[i] * 10) add(m5, m4[i] * 10)
next
 
s = 0 t = ""
for i in lst3 ip = p5[i] im = m5[i]
for j in lst9 jp = ip + p5[j] jm = im + m4[j]
for k in lst9 kp = jp + p5[k] km = jm + m3[k]
for l in lst9 lp = kp + p5[l] lm = km + m2[l]
for m in lst9 mp = lp + p5[m] mm = lm + m1[m]
for n in lst9 np = mp + p5[n] nm = mm + n - 1
if nm = np and nm > 1
if t != "" t += " + " ok
s += nm t += nm
ok
next next next next next next
et = clock()
put t + " = " + s + " " + (et - st) / clockspersecond() + " sec"</syntaxhighlight>
{{out|Output @ Tio.run}}
<pre>4150 + 4151 + 54748 + 92727 + 93084 + 194979 = 443839 4.90 sec</pre>
 
=={{header|RPL}}==
===Brute force approach===
The code below could have worked... if the time dog of the RPL emulator did not interrupt the execution after a few minutes.
≪ 2 999999 '''FOR''' n
n →STR DUP SIZE 0 1 ROT '''FOR''' j
OVER j DUP SUB STR→ 5 ^ +
'''NEXT'''
'''IF''' n ≠ '''THEN''' DROP '''END'''
'''NEXT'''
 
===Smarter approach===
{{works with|Halcyon Calc|4.2.7}}
So as not to wake the time dog, the execution has been broken into several parts and the algorithm has been improved:
# the program does not generate all the compliant numbers, but only provides the next value of the sequence, given the first ones
# since 9^5 = 59094, we do not need to check if the sum of the powered digits matches for numbers with a 9 and less than 59094
# on the other side, 6 * 7^5 = 100842, which means that 6-digit numbers above this value must have at least an 8 or a 9 in their digits to comply
# as 6 * 9^5 = 354424, there can't be any compliant 6-digit number above this value
≪ DUP SIZE 0 1 ROT '''FOR''' j
OVER j DUP SUB STR→ 5 ^ +
'''NEXT'''
SWAP DROP
'''IF''' DUP2 == '''THEN''' 1 SF ROT + SWAP '''ELSE''' DROP '''END'''
'Chk5p' STO
≪ DROP
'''IF''' DUP SIZE '''THEN''' DUP LIST→ →ARRY RNRM 1 + '''ELSE''' 2 '''END'''
1 CF '''DO'''
DUP →STR
'''IF''' OVER 59094 ≤
'''THEN IF''' DUP "9" POS NOT '''THEN''' Chk5p '''ELSE''' DROP '''END'''
'''ELSE IF''' OVER 100842 ≥
'''THEN IF''' DUP "9" POS OVER "8" POS OR '''THEN''' Chk5p '''ELSE''' DROP '''END'''
'''ELSE''' Chk5p
'''END'''
'''END'''
1 +
'''UNTIL''' 1 FS? DUP 354424 == OR '''END'''
DROP DUP LIST→ →ARRY CNRM
'NXT5P' STO
 
{} 0 NXT5P
NXT5P
NXT5P
NXT5P
NXT5P
NXT5P
{{out}}
<pre>
2: { 194979 93084 92727 54748 4151 4150 }
1: 443839
</pre>
 
=={{header|Ruby}}==
Translation of Julia.
<syntaxhighlight lang="ruby">arr = (2..9**5*6).select{|n| n.digits.sum{|d| d**5} == n }
puts "#{arr.join(" + ")} = #{arr.sum}"
</syntaxhighlight>
{{out}}
<pre>4150 + 4151 + 54748 + 92727 + 93084 + 194979 = 443839
</pre>
 
=={{header|Seed7}}==
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const proc: main is func
local
var integer: i is 0;
var integer: n is 0;
var integer: sum is 0;
var integer: digitsum is 0;
begin
for i range 2 to 9 ** 5 * 6 do
n := i;
while n > 0 do
digitsum +:= (n mod 10) ** 5;
n := n div 10;
end while;
if digitsum = i then
sum +:= i;
end if;
digitsum := 0;
end for;
writeln(sum);
end func;</syntaxhighlight>
{{out}}
<pre>
443839
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func digit_nth_powers(n, base=10) {
 
var D = @(^base)
var P = D.map {|d| d**n }
var A = []
var m = (base-1)**n
 
for(var (k, t) = (1, 1); k*m >= t; (++k, t*=base)) {
D.combinations_with_repetition(k, {|*c|
var v = c.sum {|d| P[d] }
A.push(v) if (v.digits(base).sort == c)
})
}
 
A.sort.grep { _ > 1 }
}
 
for n in (3..8) {
var a = digit_nth_powers(n)
say "Sum of #{n}-th powers of their digits: #{a.join(' + ')} = #{a.sum}"
}</syntaxhighlight>
{{out}}
<pre>
Sum of 3-th powers of their digits: 153 + 370 + 371 + 407 = 1301
Sum of 4-th powers of their digits: 1634 + 8208 + 9474 = 19316
Sum of 5-th powers of their digits: 4150 + 4151 + 54748 + 92727 + 93084 + 194979 = 443839
Sum of 6-th powers of their digits: 548834 = 548834
Sum of 7-th powers of their digits: 1741725 + 4210818 + 9800817 + 9926315 + 14459929 = 40139604
Sum of 8-th powers of their digits: 24678050 + 24678051 + 88593477 = 137949578
</pre>
 
Line 433 ⟶ 1,892:
{{libheader|Wren-math}}
Using the Julia entry's logic to arrive at an upper bound:
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
 
// cache 5th powers of digits
Line 449 ⟶ 1,908:
}
}
System.print(" = %(sum)")</langsyntaxhighlight>
 
{{out}}
Line 459 ⟶ 1,918:
=={{header|XPL0}}==
Since 1 is not actually a sum, it should not be included. Thus the answer should be 443839.
<langsyntaxhighlight XPL0lang="xpl0">\upper bound: 6*9^5 = 354294
\7*9^5 is still only a 6-digit number, so 6 digits are sufficient
 
Line 504 ⟶ 1,963:
IntOut(0, S);
CrLf(0);
]</langsyntaxhighlight>
 
{{out}}
Line 519 ⟶ 1,978:
443840
</pre>
 
=={{header|Zig}}==
<syntaxhighlight lang="zig">const std = @import("std");
 
fn sum5(n: u32) u32 {
var i = n;
var r: u32 = 0;
while (i != 0) : (i /= 10)
r += std.math.pow(u32, i%10, 5);
return r;
}
 
pub fn main() !void {
const stdout = std.io.getStdOut().writer();
const max = std.math.pow(u32,9,5) * 6;
var n: u32 = 2;
var total: u32 = 0;
while (n <= max) : (n += 1) {
if (sum5(n) == n) {
try stdout.print("{d:6}\n", .{n});
total += n;
}
}
 
try stdout.print("Total: {d:6}\n", .{total});
}</syntaxhighlight>
{{out}}
<pre> 4150
4151
54748
92727
93084
194979
Total: 443839</pre>
9,476

edits