Digit fifth powers: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(→‎{{header|AppleScript}}: Minor improvement to existing script. Added faster alternative.)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(3 intermediate revisions by 3 users not shown)
Line 331:
<syntaxhighlight lang="applescript">"4150 + 4151 + 54748 + 92727 + 93084 + 194979 = 443839"</syntaxhighlight>
 
A fasterFaster 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, but I can't get my head around a recursive version at the moment!.
 
<syntaxhighlight lang="applescript">on join(lst, delim)
Line 386:
{{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}}==
Line 770 ⟶ 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}}==
Line 1,764 ⟶ 1,892:
{{libheader|Wren-math}}
Using the Julia entry's logic to arrive at an upper bound:
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
 
// cache 5th powers of digits
9,488

edits