Rebol3 Code Examplex


Digit fifth powers

Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.

Rebol [
    title: "Rosetta code: Digit fifth powers"
    file:  %Digit_fifth_powers.r3
    url:   https://rosettacode.org/wiki/Digit_fifth_powers
]

fifth-digit-sum?: function [
    "Return true if n equals the sum of its digits each raised to the 5th power."
    n [integer!]
][
    sum: 0
    foreach c form n [sum: sum + ((c - #"0") ** 5)]
    n = sum
]

result: 0
for n 2 1000000 1 [
    if fifth-digit-sum? n [
        if result > 0 [prin " + "]
        prin n
        result: result + n
    ]
]
print [" =" result]

Output:

4150 + 4151 + 54748 + 92727 + 93084 + 194979 = 443839