created by Aha00aAha00a at 2016-12-15
last modified by Aha00aAha00a at 2026-11-15
revision: 3

Project Euler Problem27

http://projecteuler.net/index.php?section=problems&id=27

Quadratic primes

오일러는 주목할만한 이차방정식을 발견했다.

$___$n^2+n+41$___$

이 방정식은 연속적인 정수

$___$0 \le n \le 39$___$
에 대해 40개의 소수를 생성해 냄이 드러났다. 하지만
$___$n = 40$___$
일 때는
$___$40^2 + 40 + 41 = 40(40 + 1) + 41$___$
과 같이 41로 나누어 떨어지게 되고,
$___$n = 41$___$
일 때는
$___$41^2 + 41 + 41$___$
과 같이 41로 나누어 떨어진다.

놀라운 방정식

$___$n^2-79n+1601$___$
이 발견되었는데, 이는 연속적인 정수
$___$0 \le n \le 79$___$
에 대해 80개의 소수를 생성해 낸다. 계수(-79, 1601)의 곱은 -126479가 된다.

아래 2차방정식에서

$___$n^2+an+b$___$
,
$___$|a|<1000$___$
이고
$___$|b|≤1000$___$

가장 많은 수의 소수를 생성해 내는 식의 계수(a, b)의 곱을 찾아라.

1. Solution

-59231

걍 Brute force..

new {
  def isPrime(n: Int): Boolean = (n > 0) && !(2 +: (3 to Math.sqrt(n).toInt by 2)).exists(n % _ == 0)
  val solve = (for (a <- -999 to 999; b <- -999 to 999) yield (a, b, Stream.from(0).takeWhile(n => isPrime(n * n + a * n + b)).length)).maxBy(_._3)
  val ans = (solve, solve._1 * solve._2)
}.ans

5805ms 걸렸다.

연필로 풀 수 있는 문제라고 하네.

http://mathworld.wolfram.com/Prime-GeneratingPolynomial.html

-- 2016-12-15

2. See Also

2.2. Adjacent Pages

≤ 32
all
1.0x
1.0x
80
-120
ON
0/0
0/0
0.00
0
0 / 0
1.00x