LoginSignup
1
2

More than 5 years have passed since last update.

Project Euler 46 「もうひとつのゴールドバッハの予想」

Posted at

Problem 46 「もうひとつのゴールドバッハの予想」

Christian Goldbachは全ての奇合成数は平方数の2倍と素数の和で表せると予想した.

9 = 7 + 2×1^2
15 = 7 + 2×2^2
21 = 3 + 2×3^2
25 = 7 + 2×3^2
27 = 19 + 2×2^2
33 = 31 + 2×1^2

後に, この予想は誤りであることが分かった.
平方数の2倍と素数の和で表せない最小の奇合成数はいくつか?

def hoge():
    N = 33
    while True:
        N += 2 # 奇数なので+2ずつカウントアップ
        # 合成数 = 素数ではない
        if not is_prime(N) and \
           not any(is_prime(N - n**2 * 2)
                   # 最小の平方数の2倍(2)をNから引いておく
                   for n in range(1, int(((N-2) / 2) ** 0.5) + 1)):
            return N

def is_prime(n):
    if n < 2: return False
    if n == 2: return True
    if n % 2 == 0: return False
    return all(n % i != 0 for i in range(3, int(n ** 0.5) + 1, 2))

print(hoge())
1
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
1
2