How to find the numbers that are the sum of fifth powers of their digits in Swift

1 Answer

0 votes
import Foundation

func sumOfFifthPowers(_ n: Int) -> Int {
    var sum = 0
    var temp = n
    
    while temp > 0 {
        let digit = temp % 10
        sum += digit * digit * digit * digit * digit;
        temp /= 10
    }
    
    return sum
}

for i in 1000..<1_000_000 {
    if i == sumOfFifthPowers(i) {
        print(i)
    }
}



/*
run:

4150
4151
54748
92727
93084
194979

*/

 



answered Nov 9, 2025 by avibootz
...