Problem 56 » 履歴 » バージョン 2
Noppi, 2024/01/22 08:51
1 | 1 | Noppi | [ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]] |
---|---|---|---|
2 | # [[Problem 56]] |
||
3 | |||
4 | ## Powerful Digit Sum |
||
5 | A googol ($10^{100}$) is a massive number: one followed by one-hundred zeros; $100^{100}$ is almost unimaginably large: one followed by two-hundred zeros. Despite their size, the sum of the digits in each number is only $1$. |
||
6 | |||
7 | Considering natural numbers of the form, $a^b$, where $a, b \lt 100$, what is the maximum digital sum? |
||
8 | |||
9 | ## もっとべき乗の数字和 |
||
10 | Googol ($10^{100}$)は非常に大きな数である: 1の後に0が100個続く. $100^{100}$は想像を絶する. 1の後に0が200回続く. その大きさにも関わらず, 両者とも数字和 ( 桁の和 ) は$1$である. |
||
11 | |||
12 | $a, b \lt 100$ について自然数 $a^b$ を考える. 数字和の最大値を答えよ. |
||
13 | |||
14 | ```scheme |
||
15 | 2 | Noppi | (import (scheme base) |
16 | (gauche base)) |
||
17 | |||
18 | (define (digit-sum num) |
||
19 | (assume (exact-integer? num)) |
||
20 | (assume (not (negative? num))) |
||
21 | (if (zero? num) |
||
22 | 0 |
||
23 | (let loop ([rest num] [result 0]) |
||
24 | (if (zero? rest) |
||
25 | result |
||
26 | (loop (div rest 10) |
||
27 | (+ result |
||
28 | (mod rest 10))))))) |
||
29 | |||
30 | (define all-num |
||
31 | (fold-right (^[n lis] |
||
32 | (append |
||
33 | (map (^m `(,n ,m)) |
||
34 | (iota 99 1)) |
||
35 | lis)) |
||
36 | '() |
||
37 | (iota 99 1))) |
||
38 | |||
39 | (define all-num&digit-sum |
||
40 | (map (^[lis] |
||
41 | `(,lis . ,(digit-sum |
||
42 | (expt (car lis) |
||
43 | (cadr lis))))) |
||
44 | all-num)) |
||
45 | |||
46 | (define max-digit-sum |
||
47 | (fold (^[result current] |
||
48 | (if (< (cdr result) (cdr current)) |
||
49 | current |
||
50 | result)) |
||
51 | '(#f . 0) |
||
52 | all-num&digit-sum)) |
||
53 | |||
54 | (define answer-56 |
||
55 | (cdr max-digit-sum)) |
||
56 | |||
57 | (format #t "56: ~d~%" answer-56) |
||
58 | 1 | Noppi | ``` |