プロジェクト

全般

プロフィール

Wiki » 履歴 » バージョン 1

Redmine, 2023/12/26 09:02

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