Problem 8 » 履歴 » バージョン 4
Noppi, 2023/12/27 13:26
1 | 1 | Noppi | [ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]] |
---|---|---|---|
2 | # [[Problem 8]] |
||
3 | |||
4 | 4 | Noppi | ## Largest Product in a Series |
5 | The four adjacent digits in the $1000$-digit number that have the greatest product are $9 \times 9 \times 8 \times 9 = 5832$. |
||
6 | ``` |
||
7 | 73167176531330624919225119674426574742355349194934 |
||
8 | 96983520312774506326239578318016984801869478851843 |
||
9 | 85861560789112949495459501737958331952853208805511 |
||
10 | 12540698747158523863050715693290963295227443043557 |
||
11 | 66896648950445244523161731856403098711121722383113 |
||
12 | 62229893423380308135336276614282806444486645238749 |
||
13 | 30358907296290491560440772390713810515859307960866 |
||
14 | 70172427121883998797908792274921901699720888093776 |
||
15 | 65727333001053367881220235421809751254540594752243 |
||
16 | 52584907711670556013604839586446706324415722155397 |
||
17 | 53697817977846174064955149290862569321978468622482 |
||
18 | 83972241375657056057490261407972968652414535100474 |
||
19 | 82166370484403199890008895243450658541227588666881 |
||
20 | 16427171479924442928230863465674813919123162824586 |
||
21 | 17866458359124566529476545682848912883142607690042 |
||
22 | 24219022671055626321111109370544217506941658960408 |
||
23 | 07198403850962455444362981230987879927244284909188 |
||
24 | 84580156166097919133875499200524063689912560717606 |
||
25 | 05886116467109405077541002256983155200055935729725 |
||
26 | 71636269561882670428252483600823257530420752963450 |
||
27 | ``` |
||
28 | Find the thirteen adjacent digits in the $1000$-digit number that have the greatest product. What is the value of this product? |
||
29 | |||
30 | ## 数字列中の最大の積 |
||
31 | 次の1000桁の数字のうち, 隣接する4つの数字の総乗の中で, 最大となる値は, 9 × 9 × 8 × 9 = 5832である. |
||
32 | ``` |
||
33 | 73167176531330624919225119674426574742355349194934 |
||
34 | 96983520312774506326239578318016984801869478851843 |
||
35 | 85861560789112949495459501737958331952853208805511 |
||
36 | 12540698747158523863050715693290963295227443043557 |
||
37 | 66896648950445244523161731856403098711121722383113 |
||
38 | 62229893423380308135336276614282806444486645238749 |
||
39 | 30358907296290491560440772390713810515859307960866 |
||
40 | 70172427121883998797908792274921901699720888093776 |
||
41 | 65727333001053367881220235421809751254540594752243 |
||
42 | 52584907711670556013604839586446706324415722155397 |
||
43 | 53697817977846174064955149290862569321978468622482 |
||
44 | 83972241375657056057490261407972968652414535100474 |
||
45 | 82166370484403199890008895243450658541227588666881 |
||
46 | 16427171479924442928230863465674813919123162824586 |
||
47 | 17866458359124566529476545682848912883142607690042 |
||
48 | 24219022671055626321111109370544217506941658960408 |
||
49 | 07198403850962455444362981230987879927244284909188 |
||
50 | 84580156166097919133875499200524063689912560717606 |
||
51 | 05886116467109405077541002256983155200055935729725 |
||
52 | 71636269561882670428252483600823257530420752963450 |
||
53 | ``` |
||
54 | この1000桁の数字から13個の連続する数字を取り出して, それらの総乗を計算する. では、それら総乗のうち、最大となる値はいくらか. |
||
55 | EX 6桁の数123789から5個の連続する数字を取り出す場合, 1*2*3*7*8と2*3*7*8*9の二通りとなり, 後者の2*3*7*8*9=3024が最大の総乗となる. |
||
56 | |||
57 | 1 | Noppi | ```scheme |
58 | #!r6rs |
||
59 | #!chezscheme |
||
60 | |||
61 | (import (chezscheme)) |
||
62 | |||
63 | (define Problem8-number-string |
||
64 | (string-append "73167176531330624919225119674426574742355349194934" |
||
65 | "96983520312774506326239578318016984801869478851843" |
||
66 | "85861560789112949495459501737958331952853208805511" |
||
67 | "12540698747158523863050715693290963295227443043557" |
||
68 | "66896648950445244523161731856403098711121722383113" |
||
69 | "62229893423380308135336276614282806444486645238749" |
||
70 | "30358907296290491560440772390713810515859307960866" |
||
71 | "70172427121883998797908792274921901699720888093776" |
||
72 | "65727333001053367881220235421809751254540594752243" |
||
73 | "52584907711670556013604839586446706324415722155397" |
||
74 | "53697817977846174064955149290862569321978468622482" |
||
75 | "83972241375657056057490261407972968652414535100474" |
||
76 | "82166370484403199890008895243450658541227588666881" |
||
77 | "16427171479924442928230863465674813919123162824586" |
||
78 | "17866458359124566529476545682848912883142607690042" |
||
79 | "24219022671055626321111109370544217506941658960408" |
||
80 | "07198403850962455444362981230987879927244284909188" |
||
81 | "84580156166097919133875499200524063689912560717606" |
||
82 | "05886116467109405077541002256983155200055935729725" |
||
83 | "71636269561882670428252483600823257530420752963450")) |
||
84 | |||
85 | (define answer-8 |
||
86 | 3 | Noppi | (let* ([count (- (string-length Problem8-number-string) |
87 | 13)]) |
||
88 | (let loop ([index 0] [max-num 0]) |
||
89 | 1 | Noppi | (if (< count index) |
90 | max-num |
||
91 | 3 | Noppi | (let* ([sub-num (substring Problem8-number-string index (+ index 13))] |
92 | [char-lis (string->list sub-num)] |
||
93 | [num-lis (map |
||
94 | 1 | Noppi | (lambda (char) |
95 | (- (char->integer char) |
||
96 | (char->integer #\0))) |
||
97 | 3 | Noppi | char-lis)] |
98 | [product-num (fold-left * 1 num-lis)]) |
||
99 | 1 | Noppi | (loop (add1 index) (max max-num product-num))))))) |
100 | |||
101 | (printf "8: ~D~%" answer-8) |
||
102 | ``` |