Digit fifth powers: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Add PL/M)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(25 intermediate revisions by 16 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}}==
<langsyntaxhighlight lang="asm">putch: equ 2 ; CP/M syscall to print a character
puts: equ 9 ; CP/M syscall to print a string
org 100h
Line 159 ⟶ 170:
dcr b ; Any more digits?
jnz dgaddl
jmp restor</langsyntaxhighlight>
{{out}}
<pre>4150
Line 170 ⟶ 181:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_Io;
 
procedure Digit_Fifth_Powers is
Line 198 ⟶ 209:
Put ("Sum: ");
Put_Line (Natural'Image (Sum));
end Digit_Fifth_Powers;</langsyntaxhighlight>
{{out}}
<pre> 4150
Line 211 ⟶ 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 274 ⟶ 285:
print( ( newline ) );
print( ( "Total: ", whole( total, 0 ), newline ) )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 283 ⟶ 294:
=={{header|APL}}==
{{works with|Dyalog APL}}
<langsyntaxhighlight lang="apl">+/(⊢(/⍨)(⊢=(+/5*⍨⍎¨∘⍕))¨)1↓⍳6×9*5</langsyntaxhighlight>
{{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
Line 307 ⟶ 535:
next i
 
print "Their sum is ", sum</langsyntaxhighlight>
{{out}}<pre>
4150
Line 318 ⟶ 546:
 
==={{header|GW-BASIC}}===
<langsyntaxhighlight lang="gwbasic">10 SUM! = 0
20 FOR I! = 2 TO 999999!
30 GOSUB 80
Line 332 ⟶ 560:
130 NEXT J
140 RETURN
</syntaxhighlight>
</lang>
{{out}}<pre>
4150
Line 342 ⟶ 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 366 ⟶ 594:
 
PRINT "The sum is"; sum
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 379 ⟶ 607:
 
=={{header|BQN}}==
<langsyntaxhighlight lang="bqn">Sum5 ← { 0:0; (𝕊⌊𝕩÷10) + (10|𝕩)⋆5 }
 
+´(⊢=Sum5)¨⊸/ 2↓↕6×9⋆5</langsyntaxhighlight>
{{out}}
<pre>443839</pre>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include<stdio.h>
#include<stdlib.h>
#include<math.h>
Line 405 ⟶ 633:
printf( "Total is %d\n", sum );
return 0;
}</langsyntaxhighlight>
{{out}}<pre>4150
4151
Line 416 ⟶ 644:
=={{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.
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <cmath>
#include <chrono>
Line 440 ⟶ 668:
std::cout << t << " " <<
duration_cast<nanoseconds>(et - st).count() / 1000.0 << " μs";
}</langsyntaxhighlight>
{{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}}==
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. DIGIT-FIFTH-POWER.
 
Line 484 ⟶ 746:
ADD-DIGIT-POWER.
COMPUTE POWER-SUM = POWER-SUM + DIGITS(DIGIT) ** 5.</langsyntaxhighlight>
{{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
Line 496 ⟶ 786:
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
sub pow5(n: uint32): (p: uint32) is
Line 525 ⟶ 815:
print("Total: ");
print_i32(total);
print_nl();</langsyntaxhighlight>
{{out}}
<pre>4150
Line 537 ⟶ 827:
=={{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 547 ⟶ 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 559 ⟶ 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}}==
<langsyntaxhighlight lang="focal">01.10 S M=9^5*6
01.20 S T=0
01.30 F C=2,M;D 3
Line 578 ⟶ 917:
03.30 T %6,C,!
03.40 S T=T+C
03.50 R</langsyntaxhighlight>
{{out}}
<pre>= 4150
Line 591 ⟶ 930:
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 624 ⟶ 963:
}
fmt.Printf(" = %d\n", sum)
}</langsyntaxhighlight>
 
{{out}}
Line 633 ⟶ 972:
 
=={{header|J}}==
<langsyntaxhighlight lang="j">(([=[:+/10&#.^:_1^5:)"0+/@#])2}.i.6*9^5</langsyntaxhighlight>
{{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 661 ⟶ 1,085:
194979
Total: 443839</pre>
 
=={{header|Pascal}}==
slightly modified [[Own_digits_power_sum]] checks decimals up to power 19.
<langsyntaxhighlight lang="pascal">program PowerOwnDigits2;
{$IFDEF FPC}
{$R+,O+}
Line 877 ⟶ 1,302:
{$ENDIF}
setlength(CombIdx,0);
end.</langsyntaxhighlight>
{{out|Output @ Tio.run}}
<pre style="height:260px">
Line 935 ⟶ 1,360:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 946 ⟶ 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 954 ⟶ 1,379:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="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>
Line 969 ⟶ 1,394:
<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>
<!--</langsyntaxhighlight>-->
{{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}}==
<langsyntaxhighlight lang="pli">100H:
/* BDOS ROUTINES */
BDOS: PROCEDURE (F,A); DECLARE F BYTE, A ADDRESS; GO TO 5; END BDOS;
Line 1,086 ⟶ 1,564:
CALL NEWLINE;
CALL EXIT;
EOF</langsyntaxhighlight>
{{out}}
<pre>4150
Line 1,098 ⟶ 1,576:
=={{header|Python}}==
Comparing conventional vs. faster.
<langsyntaxhighlight lang="python">from time import time
 
# conventional
Line 1,131 ⟶ 1,609:
if np == nm:
if nm > 1: numbers.append(nm)
print(sum(numbers), " ", (time() - st) * 1000, "ms", end = "")</langsyntaxhighlight>
{{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 1,149 ⟶ 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 1,169 ⟶ 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===
<langsyntaxhighlight lang="ring">? "working..."
sumEnd = 0
Line 1,201 ⟶ 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 1,212 ⟶ 1,740:
===Faster===
Around six times faster than the conventional version.
<langsyntaxhighlight lang="ring">st = clock()
lst9 = 1:10 lst3 = 1:4
p5 = [] m5 = [] m4 = [] m3 = [] m2 = [] m1 = []
Line 1,233 ⟶ 1,761:
next next next next next next
et = clock()
put t + " = " + s + " " + (et - st) / clockspersecond() + " sec"</langsyntaxhighlight>
{{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>
 
=={{header|Wren}}==
{{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 1,256 ⟶ 1,908:
}
}
System.print(" = %(sum)")</langsyntaxhighlight>
 
{{out}}
Line 1,266 ⟶ 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 1,311 ⟶ 1,963:
IntOut(0, S);
CrLf(0);
]</langsyntaxhighlight>
 
{{out}}
Line 1,328 ⟶ 1,980:
 
=={{header|Zig}}==
<langsyntaxhighlight lang="zig">const std = @import("std");
 
fn sum5(n: u32) u32 {
Line 1,352 ⟶ 2,004:
 
try stdout.print("Total: {d:6}\n", .{total});
}</langsyntaxhighlight>
{{out}}
<pre> 4150
9,486

edits