2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Project Euler 27

Last updated at Posted at 2015-03-02

問題

オイラーは以下の二次式を考案している:

n**2 + n + 41.
この式は, n を0から39までの連続する整数としたときに40個の素数を生成する. しかし, n = 40 のとき 40**2 + 40 + 41 = 40(40 + 1) + 41 となり41で割り切れる. また, n = 41 のときは 41**2 + 41 + 41 であり明らかに41で割り切れる.

計算機を用いて, 二次式 n**2 - 79*n + 1601 という式が発見できた. これは n = 0 から 79 の連続する整数で80個の素数を生成する. 係数の積は, -79 × 1601 で -126479である.

さて, |a| < 1000, |b| < 1000 として以下の二次式を考える (ここで |a| は絶対値): 例えば |11| = 11 |-4| = 4である.

n**2 + a*n + b
n = 0 から始めて連続する整数で素数を生成したときに最長の長さとなる上の二次式の, 係数 a, b の積を答えよ.
http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2027

回答

言われるままに全文検索。おそらくよくないんだろうな。
なお、mymathはこちら
http://qiita.com/cof/items/45d3823c3d71e7e22920

import mymath

def f(n,a,b):
  return n**2 + a * n + b 

def cof():
  P_MAX = 10 ** 6
  pri = mymath.get_primes(P_MAX)
  max_n = 0
  ans = 0
  seq = range(-1000,1001)
  for a in seq:
    for b in seq:
      n = 0
      while pri['bool'][f(n,a,b)]:
        n += 1
      if n > max_n:
        max_n = n
        ans = a*b
  print ans

遅いこと山の如し。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?