Digit fifth powers: Difference between revisions
(Blanked the page) |
(Added XPL0 example.) |
||
Line 1: | Line 1: | ||
{{Draft task}} |
|||
;Task: |
|||
<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><br> |
|||
=={{header|Ring}}== |
|||
<lang ring> |
|||
see "working..." + nl |
|||
sumEnd = 0 |
|||
sumList = [] |
|||
limitStart = 10000 |
|||
limitEnd = 99999 |
|||
for n = limitStart to limitEnd |
|||
sum = 0 |
|||
nStr = string(n) |
|||
for m = 1 to len(nStr) |
|||
sum = sum + pow(number(nStr[m]),5) |
|||
next |
|||
if sum = n |
|||
add(sumList,n) |
|||
sumEnd += n |
|||
ok |
|||
next |
|||
see "The sum of all the numbers that can be written as the sum of fifth powers of their digits:" + nl |
|||
for n = 1 to len(sumList)-1 |
|||
see "" + sumList[n] + " + " |
|||
next |
|||
see "" + sumList[n] + " = " + sumEnd + nl |
|||
see "done..." + nl |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
working... |
|||
The sum of all the numbers that can be written as the sum of fifth powers of their digits: |
|||
54748 + 92727 + 93084 = 240559 |
|||
done... |
|||
</pre> |
|||
=={{header|XPL0}}== |
|||
<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> |
|||
{{out}} |
|||
<pre> |
|||
0 |
|||
4150 |
|||
1 |
|||
4151 |
|||
93084 |
|||
92727 |
|||
54748 |
|||
194979 |
|||
443840 |
|||
</pre> |
Revision as of 14:19, 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.
Ring
<lang ring> see "working..." + nl
sumEnd = 0 sumList = [] limitStart = 10000 limitEnd = 99999
for n = limitStart to limitEnd
sum = 0 nStr = string(n) for m = 1 to len(nStr) sum = sum + pow(number(nStr[m]),5) next if sum = n add(sumList,n) sumEnd += n ok
next
see "The sum of all the numbers that can be written as the sum of fifth powers of their digits:" + nl for n = 1 to len(sumList)-1
see "" + sumList[n] + " + "
next see "" + sumList[n] + " = " + sumEnd + nl see "done..." + nl </lang>
- Output:
working... The sum of all the numbers that can be written as the sum of fifth powers of their digits: 54748 + 92727 + 93084 = 240559 done...
XPL0
<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