Problem 27 » 履歴 » バージョン 2
Noppi, 2024/01/11 12:51
| 1 | 1 | Noppi | [ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]] |
|---|---|---|---|
| 2 | # [[Problem 27]] |
||
| 3 | |||
| 4 | ## Quadratic Primes |
||
| 5 | Euler discovered the remarkable quadratic formula: |
||
| 6 | <p style="text-align:center;">$n^2 + n + 41$</p> |
||
| 7 | It turns out that the formula will produce $40$ primes for the consecutive integer values $0 \le n \le 39$. However, when $n = 40, 40^2 + 40 + 41 = 40(40 + 1) + 41$ is divisible by $41$, and certainly when $n = 41, 41^2 + 41 + 41$ is clearly divisible by $41$. |
||
| 8 | |||
| 9 | The incredible formula $n^2 - 79n + 1601$ was discovered, which produces $80$ primes for the consecutive values $0 \le n \le 79$. The product of the coefficients, $-79$ and $1601$, is $-126479$. |
||
| 10 | Considering quadratics of the form: |
||
| 11 | |||
| 12 | > $n^2 + an + b$, where $|a| < 1000$ and $|b| \le 1000$ |
||
| 13 | > |
||
| 14 | > where $|n|$ is the modulus/absolute value of $n$ |
||
| 15 | > e.g. $|11| = 11$ and $|-4| = 4$ |
||
| 16 | |||
| 17 | Find the product of the coefficients, $a$ and $b$, for the quadratic expression that produces the maximum number of primes for consecutive values of $n$, starting with $n = 0$. |
||
| 18 | |||
| 19 | ## 二次式素数 |
||
| 20 | オイラーは以下の二次式を考案している: |
||
| 21 | <p style="text-align:center;">$n^2 + n + 41$</p> |
||
| 22 | この式は, n を0から39までの連続する整数としたときに40個の素数を生成する. しかし, $n = 40$ のとき $40^2 + 40 + 41 = 40(40 + 1) + 41$ となり41で割り切れる. また, $n = 41$ のときは $41^2 + 41 + 41$であり明らかに41で割り切れる. |
||
| 23 | |||
| 24 | 計算機を用いて, 二次式 $n^2 - 79n + 1601$ という式が発見できた. これは n = 0 から 79 の連続する整数で80個の素数を生成する. 係数の積は, -79 × 1601 で -126479である. |
||
| 25 | |||
| 26 | さて, |a| < 1000, |b| ≤ 1000 として以下の二次式を考える (ここで |a| は絶対値): 例えば |11| = 11 |-4| = 4である. |
||
| 27 | <p style="text-align:center;">$n^2 + an + b$</p> |
||
| 28 | n = 0 から始めて連続する整数で素数を生成したときに最長の長さとなる上の二次式の, 係数 a, b の積を答えよ. |
||
| 29 | |||
| 30 | ```scheme |
||
| 31 | 2 | Noppi | (import (scheme base) |
| 32 | (gauche base) |
||
| 33 | (math prime) |
||
| 34 | (scheme sort)) |
||
| 35 | |||
| 36 | (define temp-primes (primes)) |
||
| 37 | |||
| 38 | (define (prime? n) |
||
| 39 | (= n |
||
| 40 | (find (^p (<= n p)) |
||
| 41 | temp-primes))) |
||
| 42 | |||
| 43 | (define (consecutive-prime a b) |
||
| 44 | (let loop ([n 0]) |
||
| 45 | (if (prime? (+ (expt n 2) |
||
| 46 | (* n a) |
||
| 47 | b)) |
||
| 48 | (loop (+ n 1)) |
||
| 49 | n))) |
||
| 50 | |||
| 51 | (define ab-combinations |
||
| 52 | (fold-right |
||
| 53 | (^[m result] |
||
| 54 | (append |
||
| 55 | (fold-right |
||
| 56 | (^[n result] |
||
| 57 | (cons `(,m . ,n) result)) |
||
| 58 | '() |
||
| 59 | (iota (+ 1 (* 1000 2)) -1000)) |
||
| 60 | result)) |
||
| 61 | '() |
||
| 62 | (iota (+ 1 (* 999 2)) -999))) |
||
| 63 | |||
| 64 | ;; これでいけると思ったけどうまくいかなかった… |
||
| 65 | ;; 何か見落としてるっぽい… |
||
| 66 | ;(define ab-combinations |
||
| 67 | ; (fold-right |
||
| 68 | ; (^[m result] |
||
| 69 | ; (append |
||
| 70 | ; (fold-right |
||
| 71 | ; (^[n result] |
||
| 72 | ; (cons `(,m . ,n) result)) |
||
| 73 | ; '() |
||
| 74 | ; (iota (+ (div (- -1 m) |
||
| 75 | ; 2) |
||
| 76 | ; 1) |
||
| 77 | ; 3 |
||
| 78 | ; 2)) |
||
| 79 | ; result)) |
||
| 80 | ; '() |
||
| 81 | ; (iota 499 -997 2))) |
||
| 82 | |||
| 83 | (define consecutive-list |
||
| 84 | (map |
||
| 85 | (^c |
||
| 86 | (cons* (car c) |
||
| 87 | (cdr c) |
||
| 88 | (consecutive-prime (car c) (cdr c)))) |
||
| 89 | ab-combinations)) |
||
| 90 | |||
| 91 | (define answer-27 |
||
| 92 | (let ([answer-abc (car (list-sort |
||
| 93 | (^[a b] |
||
| 94 | (> (cddr a) (cddr b))) |
||
| 95 | consecutive-list))]) |
||
| 96 | (* (car answer-abc) (cadr answer-abc)))) |
||
| 97 | |||
| 98 | (format #t "27: ~d~%" answer-27) |
||
| 99 | 1 | Noppi | ``` |