Digit fifth powers: Difference between revisions
No edit summary |
(Clarification about non-inclusion of 1) |
||
Line 4: | Line 4: | ||
<br>Task desciption is taken from Project Eulet(https://projecteuler.net/problem=30) |
<br>Task desciption is taken from Project Eulet(https://projecteuler.net/problem=30) |
||
<br>Find the sum of all the numbers that can be written as the sum of fifth powers of their digits. |
<br>Find the sum of all the numbers that can be written as the sum of fifth powers of their digits. |
||
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> |
<br><br> |
||
Revision as of 17:46, 5 November 2021
- Task
Task desciption is taken from Project Eulet(https://projecteuler.net/problem=30)
Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.
Even though 15 = 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.
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. <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))
</lang>
- Output:
Numbers > 1 that can be written as the sum of fifth powers of their digits: 4150 + 4151 + 54748 + 92727 + 93084 + 194979 = 443839
Python
<lang>print(sum([n for n in range(2, 6*9**5) if sum(int(i)**5 for i in str(n)) == n]))</lang>
- Output:
443839
Raku
<lang perl6>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. EXPANATION
sub super($i) { $i.trans('0123456789' => '⁰¹²³⁴⁵⁶⁷⁸⁹') }
for 3..8 -> $power {
print "\nSum of powers of n{super $power}: "; my $threshold = 9**$power * $power; put .join(' + '), ' = ', .sum with cache (2..$threshold).race.map: { state %p = ^10 .map: { $_ => $_ ** $power }; $_ if %p{.comb}.sum == $_ }
}</lang>
- Output:
Sum of all integers (except 1 for some mysterious reason ¯\_(ツ)_/¯), for which the individual digits to the nth power sum to itself. Sum of powers of n³: 153 + 370 + 371 + 407 = 1301 Sum of powers of n⁴: 1634 + 8208 + 9474 = 19316 Sum of powers of n⁵: 4150 + 4151 + 54748 + 92727 + 93084 + 194979 = 443839 Sum of powers of n⁶: 548834 = 548834 Sum of powers of n⁷: 1741725 + 4210818 + 9800817 + 9926315 + 14459929 = 40139604 Sum of powers of n⁸: 24678050 + 24678051 + 88593477 = 137949578
Ring
<lang ring>? "working..."
sumEnd = 0 sumList = ""
pow5 = [] for i = 1 to 9
add(pow5, pow(i, 5))
next
limitStart = 2 limitEnd = 6 * pow5[9]
for n = limitStart to limitEnd
sum = 0 m = n while m > 0 d = m % 10 if d > 0 sum += pow5[d] ok m = unsigned(m, 10, "/") end if sum = n sumList += "" + n + " + " sumEnd += n ok
next
? "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..."</lang>
- Output:
working... The sum of all the numbers that can be written as the sum of fifth powers of their digits: 4150 + 4151 + 54748 + 92727 + 93084 + 194979 = 443839 done...
XPL0
Since 1 is not actually a sum, it should not be included. Thus the answer should be 443839. <lang XPL0>\upper bound: 6*9^5 = 354294 \7*9^5 is still only a 6-digit number, so 6 digits are sufficient
int A, B, C, D, E, F, \digits, A=LSD
A5, B5, C5, D5, E5, F5, \digits to 5th power A0, B0, C0, D0, E0, F0, \digits multiplied by their decimal place N, \number that can be written as the sum of its 5th pwrs S; \sum of all numbers
[S:= 0;
for A:= 0, 9 do \for all digits
[A5:= A*A*A*A*A; A0:= A; for B:= 0, 9 do [B5:= B*B*B*B*B; B0:= B*10; for C:= 0, 9 do [C5:= C*C*C*C*C; C0:= C*100; for D:= 0, 9 do [D5:= D*D*D*D*D; D0:= D*1000; for E:= 0, 9 do [E5:= E*E*E*E*E; E0:= E*10000; for F:= 0, 3 do [F5:= F*F*F*F*F; F0:= F*100000; [N:= F0 + E0 + D0 + C0 + B0 + A0; if N = A5 + B5 + C5 + D5 + E5 + F5 then [S:= S + N; IntOut(0, N); CrLf(0); ]; ]; ]; ]; ]; ]; ]; ];
CrLf(0); IntOut(0, S); CrLf(0); ]</lang>
- Output:
0 4150 1 4151 93084 92727 54748 194979 443840