プロジェクト

全般

プロフィール

Problem 34 » 履歴 » バージョン 2

Noppi, 2024/01/13 14:57

1 1 Noppi
[ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]]
2
# [[Problem 34]]
3
4
## Digit Factorials
5
$145$ is a curious number, as $1! + 4! + 5! = 1 + 24 + 120 = 145$.
6
7
Find the sum of all numbers which are equal to the sum of the factorial of their digits.
8
9
Note: As $1! = 1$ and $2! = 2$ are not sums they are not included.
10
11
## 桁の階乗
12
145は面白い数である. 1! + 4! + 5! = 1 + 24 + 120 = 145となる.
13
14
各桁の数の階乗の和が自分自身と一致するような数の和を求めよ.
15
16
**注:** 1! = 1 と 2! = 2 は総和に含めてはならない.
17
18
```scheme
19 2 Noppi
(import (scheme base)
20
        (gauche base))
21
22
(define (digit-factorial-num n)
23
  (fold (^[n result]
24
          (+ result
25
             (apply * (iota n 1))))
26
        0
27
        (map digit->integer
28
             (string->list (number->string n)))))
29
30
(define digit-factorials
31
  (fold (^[n lis]
32
          (if (= n (digit-factorial-num n))
33
            (cons n lis)
34
            lis))
35
        '()
36
        ; 9! * 7 = 2540160
37
        ; 9! * 8 = 2903040
38
        ; のため、3 ~ 2540160 まで求めれば良い
39
        (iota 2540158 3)))
40
41
(define answer-34
42
  (apply + digit-factorials))
43
44
(format #t "34: ~d~%" answer-34)
45 1 Noppi
```