プロジェクト

全般

プロフィール

Wiki » 履歴 » バージョン 2

Noppi, 2023/12/27 01:27

1 2 Noppi
[ホーム](https://redmine.noppi.jp)
2
# [[Wiki|Project Euler]]
3
* [[Problem 1]]
4
* [[Problem 2]]
5
* [[Problem 3]]
6
* [[Problem 4]]
7
* [[Problem 5]]
8
* [[Problem 6]]
9
* [[Problem 7]]
10
* [[Problem 8]]
11
* [[Problem 9]]
12
* [[Problem 10]]
13 1 Redmine
14
```scheme
15
#!r6rs
16
#!chezscheme
17
18
(import (chezscheme))
19
20
;;; Problem 1
21
(define (answer-1)
22
  (let loop ((n 3) (sum 0))
23
    (cond
24
      [(<= 1000 n) sum]
25
      [(or (zero? (mod n 3)) (zero? (mod n 5)))
26
       (loop (add1 n) (+ sum n))]
27
      [else (loop (add1 n) sum)])))
28
29
;;; Problem 2
30
(define (answer-2)
31
  (let loop ((first 0) (second 1) (result 0))
32
    (let ((next (+ first second)))
33
      (cond
34
        [(< 4000000 next) result]
35
        [(even? next)
36
         (loop second next (+ result next))]
37
        [else (loop second next result)]))))
38
39
;;; Problem 3
40
(define (answer-3)
41
  (let* ((n 600851475143)
42
         (check-max (isqrt n)))
43
    (let loop ((current 3) (rest n) (max-prime 1))
44
      (cond
45
        [(< check-max current) max-prime]
46
        [(zero? (mod rest current))
47
         (loop current (div rest current) current)]
48
        [else (loop (+ current 2) rest max-prime)]))))
49
50
(define (RotationNumber? num)
51
  (let* ((num-string (number->string num))
52
         (str-length (string-length num-string))
53
         (count (div str-length 2)))
54
    (let loop ((index 0))
55
      (cond
56
        [(<= count index) #t]
57
        [(equal? (string-ref num-string index) (string-ref num-string (- str-length 1 index)))
58
         (loop (add1 index))]
59
        [else #f]))))
60
61
;;; Problem 4
62
(define (answer-4)
63
  (let loop1 ((num1 100)
64
              (result 0))
65
    (if (< 999 num1)
66
      result
67
      (let loop2 ((num2 100)
68
                  (result result))
69
        (if (< 999 num2)
70
          (loop1 (add1 num1) result)
71
          (let ((num (* num1 num2)))
72
            (if (RotationNumber? num)
73
              (loop2 (add1 num2) (max result num))
74
              (loop2 (add1 num2) result))))))))
75
76
;;; Problem 5
77
(define (answer-5)
78
  (let ((n (* 11 13 15 16 17 19)))
79
    (let loop ((i n))
80
      (if (and
81
            (zero? (mod i 12))
82
            (zero? (mod i 14))
83
            (zero? (mod i 18))
84
            (zero? (mod i 20)))
85
        i
86
        (loop (+ i n))))))
87
88
;;; Problem 6
89
(define (answer-6)
90
  (let loop ((n 1) (sum-all 0) (squared-all 0))
91
    (if (< 100 n)
92
      (- (expt sum-all 2) squared-all)
93
      (loop (add1 n) (+ sum-all n) (+ squared-all (expt n 2))))))
94
95
(define (prime? num)
96
  (if (even? num)
97
    #f
98
    (let ((count (isqrt num)))
99
      (let loop ((check-num 3))
100
        (cond
101
          [(< count check-num) #t]
102
          [(zero? (mod num check-num)) #f]
103
          [else (loop (+ check-num 2))])))))
104
105
;;; Problem 7
106
(define (answer-7)
107
  (let loop ((current 3) (count 1) (prime 2))
108
    (if
109
      (= 10001 count)
110
      prime
111
      (let ((next (+ current 2)))
112
        (if (prime? current)
113
          (loop next (add1 count) current)
114
          (loop next count prime))))))
115
116
(define (Problem8-number-string)
117
  (string-append "73167176531330624919225119674426574742355349194934"
118
                 "96983520312774506326239578318016984801869478851843"
119
                 "85861560789112949495459501737958331952853208805511"
120
                 "12540698747158523863050715693290963295227443043557"
121
                 "66896648950445244523161731856403098711121722383113"
122
                 "62229893423380308135336276614282806444486645238749"
123
                 "30358907296290491560440772390713810515859307960866"
124
                 "70172427121883998797908792274921901699720888093776"
125
                 "65727333001053367881220235421809751254540594752243"
126
                 "52584907711670556013604839586446706324415722155397"
127
                 "53697817977846174064955149290862569321978468622482"
128
                 "83972241375657056057490261407972968652414535100474"
129
                 "82166370484403199890008895243450658541227588666881"
130
                 "16427171479924442928230863465674813919123162824586"
131
                 "17866458359124566529476545682848912883142607690042"
132
                 "24219022671055626321111109370544217506941658960408"
133
                 "07198403850962455444362981230987879927244284909188"
134
                 "84580156166097919133875499200524063689912560717606"
135
                 "05886116467109405077541002256983155200055935729725"
136
                 "71636269561882670428252483600823257530420752963450"))
137
138
;;; Problem 8
139
(define (answer-8)
140
  (let* ((p8-num-string (Problem8-number-string))
141
         (count (- (string-length p8-num-string) 13)))
142
    (let loop ((index 0) (max-num 0))
143
      (if (< count index)
144
        max-num
145
        (let* ((sub-num (substring p8-num-string index (+ index 13)))
146
               (char-lis (string->list sub-num))
147
               (num-lis (map
148
                          (lambda (char)
149
                            (- (char->integer char)
150
                               (char->integer #\0)))
151
                          char-lis))
152
               (product-num (fold-left * 1 num-lis)))
153
          (loop (add1 index) (max max-num product-num)))))))
154
155
;;; Problem 9
156
(define (answer-9)
157
  (let loop1 ((a 1))
158
    (if (< 333 a)
159
      (error "answer-9" "answer-9 was not found!")
160
      (let loop2 ((b a))
161
        (if (< 499 b)
162
          (loop1 (add1 a))
163
          (let ((c (- 1000 a b)))
164
            (if (= (+ (expt a 2) (expt b 2)) (expt c 2))
165
              (* a b c)
166
              (loop2 (add1 b)))))))))
167
168
;;; Problem 10
169
(define (answer-10)
170
  (let ((table (make-vector 2000001 #t))
171
        (count (isqrt 2000000)))
172
    (vector-set! table 0 #f)
173
    (vector-set! table 1 #f)
174
175
    (let loop1 ((index 2))
176
      (cond
177
        [(< count index)
178
         (let loop2 ((index 0) (result 0))
179
           (cond
180
             [(< 2000000 index) result]
181
             [(vector-ref table index)
182
              (loop2 (add1 index) (+ result index))]
183
             [else (loop2 (add1 index) result)]))]
184
        [(vector-ref table index)
185
         (let loop3 ((current (* index 2)))
186
           (cond
187
             [(< 2000000 current)
188
              (loop1 (add1 index))]
189
             [else
190
               (vector-set! table current #f)
191
               (loop3 (+ current index))]))]
192
        [else (loop1 (add1 index))]))))
193
194
;(printf "1: ~D~%" (answer-1))
195
;(printf "2: ~D~%" (answer-2))
196
;(printf "3: ~D~%" (answer-3))
197
;(printf "4: ~D~%" (answer-4))
198
;(printf "5: ~D~%" (answer-5))
199
;(printf "6: ~D~%" (answer-6))
200
;(printf "7: ~D~%" (answer-7))
201
;(printf "8: ~D~%" (answer-8))
202
;(printf "9: ~D~%" (answer-9))
203
(printf "10: ~D~%" (answer-10))
204
```