プロジェクト

全般

プロフィール

操作

ホーム - Project Euler

Problem 30

Digit Fifth Powers

Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits: \begin{align} 1634 &= 1^4 + 6^4 + 3^4 + 4^4\\ 8208 &= 8^4 + 2^4 + 0^4 + 8^4\\ 9474 &= 9^4 + 4^4 + 7^4 + 4^4 \end{align}

As $1 = 1^4$ is not a sum it is not included.

The sum of these numbers is $1634 + 8208 + 9474 = 19316$.

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

各桁の5乗

驚くべきことに, 各桁を4乗した数の和が元の数と一致する数は3つしかない. \begin{align} 1634 &= 1^4 + 6^4 + 3^4 + 4^4\\ 8208 &= 8^4 + 2^4 + 0^4 + 8^4\\ 9474 &= 9^4 + 4^4 + 7^4 + 4^4 \end{align}

ただし, $1 = 1^4$ は含まないものとする. この数たちの和は 1634 + 8208 + 9474 = 19316 である.

各桁を5乗した数の和が元の数と一致するような数の総和を求めよ.

(import (scheme base)
        (gauche base))

(define (fifth-power n)
  (fold (^[n acc]
          (+ (expt n 5) acc))
        0
        (map (cut digit->integer <>)
             (string->list (number->string n)))))

(define fifth-power-list
  ; (* (expt 9 5) 6) -> 354294
  ; (* (expt 9 5) 7) -> 413343
  ; なので、2 ~ 354294 まで求めればよい
  (map (^n (cons n (fifth-power n)))
       (iota 354293 2)))

(define digit-fifth-powers
  (map (cut car <>)
       (filter (^c (= (car c) (cdr c)))
               fifth-power-list)))

(define answer-30
  (apply + digit-fifth-powers))

(format #t "30: ~d~%" answer-30)

Noppi2024/01/12に更新 · 2件の履歴