Problem 28 » 履歴 » バージョン 2
Noppi, 2024/01/11 15:16
1 | 1 | Noppi | [ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]] |
---|---|---|---|
2 | # [[Problem 28]] |
||
3 | |||
4 | ## Number Spiral Diagonals |
||
5 | Starting with the number $1$ and moving to the right in a clockwise direction a $5$ by $5$ spiral is formed as follows: |
||
6 | |||
7 | | | | | | | |
||
8 | |--|--|--|--|--| |
||
9 | | <span style="color:red"> **21** </span> | 22 | 23 | 24 | <span style="color:red"> **25** </span> | |
||
10 | | 20 | <span style="color:red"> **7** </span> | 8 | <span style="color:red"> **9** </span> | 10 | |
||
11 | | 19 | 6 | <span style="color:red"> **1** </span> | 2 | 11 | |
||
12 | | 18 | <span style="color:red"> **5** </span> | 4 | <span style="color:red"> **3** </span> | 12 | |
||
13 | | <span style="color:red"> **17** </span> | 16 | 15 | 14 | <span style="color:red"> **13** </span> | |
||
14 | |||
15 | It can be verified that the sum of the numbers on the diagonals is $101$. |
||
16 | |||
17 | What is the sum of the numbers on the diagonals in a $1001$ by $1001$ spiral formed in the same way? |
||
18 | |||
19 | ## 螺旋状に並んだ数の対角線 |
||
20 | 1から初めて右方向に進み時計回りに数字を増やしていき, 5×5の螺旋が以下のように生成される: |
||
21 | |||
22 | | | | | | | |
||
23 | |--|--|--|--|--| |
||
24 | | <span style="color:red"> **21** </span> | 22 | 23 | 24 | <span style="color:red"> **25** </span> | |
||
25 | | 20 | <span style="color:red"> **7** </span> | 8 | <span style="color:red"> **9** </span> | 10 | |
||
26 | | 19 | 6 | <span style="color:red"> **1** </span> | 2 | 11 | |
||
27 | | 18 | <span style="color:red"> **5** </span> | 4 | <span style="color:red"> **3** </span> | 12 | |
||
28 | | <span style="color:red"> **17** </span> | 16 | 15 | 14 | <span style="color:red"> **13** </span> | |
||
29 | |||
30 | 両対角線上の数字の合計は101であることが確かめられる. |
||
31 | |||
32 | 1001×1001の螺旋を同じ方法で生成したとき, 対角線上の数字の和はいくつか? |
||
33 | |||
34 | ```scheme |
||
35 | 2 | Noppi | ;;; |
36 | ;;; 未完成!!! |
||
37 | ;;; |
||
38 | |||
39 | (import (scheme base) |
||
40 | (gauche base) |
||
41 | (gauche array)) |
||
42 | |||
43 | (define (make-table xynums) |
||
44 | (let ([table (make-array (shape 0 xynums 0 xynums) #f)]) |
||
45 | (letrec ([walk-right (^[n y x] |
||
46 | (cond |
||
47 | [(and (< (+ x 1) xynums) |
||
48 | (not (array-ref table y (+ x 1)))) |
||
49 | (array-set! table y (+ x 1) n) |
||
50 | (walk-down (+ n 1) y (+ x 1))] |
||
51 | [(and (<= 0 (- y 1)) |
||
52 | (not (array-ref table (- y 1) x))) |
||
53 | (array-set! table (- y 1) x n) |
||
54 | (walk-right (+ n 1) (- y 1) x)] |
||
55 | [else table]))] |
||
56 | [walk-down (^[n y x] |
||
57 | (cond |
||
58 | [(and (< (+ y 1) xynums) |
||
59 | (not (array-ref table (+ y 1) x))) |
||
60 | (array-set! table (+ y 1) x n) |
||
61 | (walk-left (+ n 1) (+ y 1) x)] |
||
62 | [(and (< (+ x 1) xynums) |
||
63 | (not (array-ref table y (+ x 1)))) |
||
64 | (array-set! table y (+ x 1) n) |
||
65 | (walk-down (+ n 1) y (+ x 1))] |
||
66 | [else table]))] |
||
67 | [walk-left (^[n y x] |
||
68 | (cond |
||
69 | [(and (<= 0 (- x 1)) |
||
70 | (not (array-ref table y (- x 1)))) |
||
71 | (array-set! table y (- x 1) n) |
||
72 | (walk-up (+ n 1) y (- x 1))] |
||
73 | [(and (< (+ y 1) xynums) |
||
74 | (not (array-ref table (+ y 1) x))) |
||
75 | (array-set! table (+ y 1) x n) |
||
76 | (walk-left (+ n 1) (+ y 1) x)] |
||
77 | [else table]))] |
||
78 | [walk-up (^[n y x] |
||
79 | (cond |
||
80 | [(and (<= 0 (- y 1)) |
||
81 | (not (array-ref table (- y 1) x))) |
||
82 | (array-set! table (- y 1) x n) |
||
83 | (walk-right (+ n 1) (- y 1) x)] |
||
84 | [(and (<= 0 (- x 1)) |
||
85 | (not (array-ref table y (- x 1)))) |
||
86 | (array-set! table y (- x 1) n) |
||
87 | (walk-up (+ n 1) y (- x 1))] |
||
88 | [else table]))]) |
||
89 | (array-set! table (div xynums 2) (div xynums 2) 1) |
||
90 | (walk-right 2 (div xynums 2) (div xynums 2))))) |
||
91 | |||
92 | ;(define answer-28 |
||
93 | |||
94 | ;(format #t "28: ~d~%" answer-28) |
||
95 | 1 | Noppi | ``` |