Moved solutions till 35 to Python.
This commit is contained in:
@@ -1,56 +1,27 @@
|
||||
def get_primes_till(n):
|
||||
square = lambda n: n * n
|
||||
candiates = range(2, n + 1)
|
||||
primes = []
|
||||
while candiates:
|
||||
prime = candiates[0]
|
||||
primes.append(prime)
|
||||
candiates = [c for c in candiates if c % prime != 0]
|
||||
return primes
|
||||
from lib_prime import primes, is_prime
|
||||
|
||||
|
||||
def get_coprime(n):
|
||||
primes = get_primes_till(n)
|
||||
for p in primes:
|
||||
if n % p != 0:
|
||||
return p
|
||||
raise Exception("No coprime found for {}.".format(n))
|
||||
|
||||
def is_prime_fermat(n):
|
||||
if n == 2:
|
||||
return True
|
||||
a = get_coprime(n)
|
||||
if (a ** (n - 1) % n) != 1:
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
|
||||
def is_prime_deterministic(n):
|
||||
pass
|
||||
|
||||
def is_prime(n):
|
||||
if n == 2:
|
||||
return True
|
||||
if n < 2:
|
||||
return False
|
||||
if not is_prime_fermat(n):
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
return is_prime_deterministic(n)
|
||||
|
||||
def get_length(a, b):
|
||||
def number_consecutive_primes(a, b):
|
||||
def formula(n):
|
||||
return n*n + a*n + b
|
||||
for n in range(99999):
|
||||
return n * n + a * n + b
|
||||
n = 0
|
||||
while True:
|
||||
if not is_prime(formula(n)):
|
||||
return n
|
||||
n += 1
|
||||
|
||||
def bruteforce():
|
||||
solution = None
|
||||
options = [(get_length(a, b), a, b)
|
||||
for a in get_primes_till(1000)
|
||||
for b in get_primes_till(1000)]
|
||||
print(max(options))
|
||||
|
||||
bruteforce()
|
||||
def euler_027():
|
||||
n_max, a_max, b_max = 0, 0, 0
|
||||
for b in primes(1000):
|
||||
for a in range(-999, 1000):
|
||||
a = -1 * a
|
||||
n = number_consecutive_primes(a, b)
|
||||
if n > n_max:
|
||||
n_max, a_max, b_max = n, a, b
|
||||
return a_max * b_max
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("e027.py: {}".format(euler_027()))
|
||||
assert(euler_027() == -59231)
|
||||
|
||||
Reference in New Issue
Block a user