プロジェクト

全般

プロフィール

操作

ホーム - Project Euler

Problem 34

Digit Factorials

$145$ is a curious number, as $1! + 4! + 5! = 1 + 24 + 120 = 145$.

Find the sum of all numbers which are equal to the sum of the factorial of their digits.

Note: As $1! = 1$ and $2! = 2$ are not sums they are not included.

桁の階乗

145は面白い数である. 1! + 4! + 5! = 1 + 24 + 120 = 145となる.

各桁の数の階乗の和が自分自身と一致するような数の和を求めよ.

注: 1! = 1 と 2! = 2 は総和に含めてはならない.

(import (scheme base)
        (gauche base))

(define (digit-factorial-num n)
  (fold (^[n result]
          (+ result
             (apply * (iota n 1))))
        0
        (map digit->integer
             (string->list (number->string n)))))

(define digit-factorials
  (fold (^[n lis]
          (if (= n (digit-factorial-num n))
            (cons n lis)
            lis))
        '()
        ; 9! * 7 = 2540160
        ; 9! * 8 = 2903040
        ; のため、3 ~ 2540160 まで求めれば良い
        (iota 2540158 3)))

(define answer-34
  (apply + digit-factorials))

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

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