Problem 20 » 履歴 » バージョン 2
Noppi, 2024/01/02 10:12
1 | 1 | Noppi | [ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]] |
---|---|---|---|
2 | # [[Problem 20]] |
||
3 | |||
4 | ## Factorial Digit Sum |
||
5 | $n!$ means $n \times (n - 1) \times \cdots \times 3 \times 2 \times 1$. |
||
6 | For example, $10! = 10 \times 9 \times \cdots \times 3 \times 2 \times 1 = 3628800$,<br>and the sum of the digits in the number $10!$ is $3 + 6 + 2 + 8 + 8 + 0 + 0 = 27$. |
||
7 | Find the sum of the digits in the number $100!$. |
||
8 | |||
9 | ## 各位の数字の和 2 |
||
10 | n × (n - 1) × ... × 3 × 2 × 1 を n! と表す. |
||
11 | 例えば, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800 となる. |
||
12 | この数の各桁の合計は 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27 である. |
||
13 | では, 100! の各位の数字の和を求めよ. |
||
14 | 注: [[Problem 16]] も各位の数字の和に関する問題です。解いていない方は解いてみてください。 |
||
15 | |||
16 | ```scheme |
||
17 | 2 | Noppi | #!r6rs |
18 | #!chezscheme |
||
19 | |||
20 | (import (chezscheme)) |
||
21 | |||
22 | (define (each-numbers num) |
||
23 | (let ([number-chars (string->list (number->string num))]) |
||
24 | (map |
||
25 | (lambda (char) |
||
26 | (char- char #\0)) |
||
27 | number-chars))) |
||
28 | |||
29 | (define iota-1-100 |
||
30 | (cdr (iota 101))) |
||
31 | |||
32 | (define answer-20 |
||
33 | (apply + (each-numbers (apply * iota-1-100)))) |
||
34 | |||
35 | (printf "20: ~D~%" answer-20) |
||
36 | 1 | Noppi | ``` |