Finished 46 with quite a quick algorithm
This commit is contained in:
60
46/main.py
Normal file
60
46/main.py
Normal file
@@ -0,0 +1,60 @@
|
||||
'''
|
||||
It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square.
|
||||
|
||||
9 = 7 + 2×12
|
||||
15 = 7 + 2×22
|
||||
21 = 3 + 2×32
|
||||
25 = 7 + 2×32
|
||||
27 = 19 + 2×22
|
||||
33 = 31 + 2×12
|
||||
|
||||
It turns out that the conjecture was false.
|
||||
|
||||
What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?
|
||||
'''
|
||||
|
||||
# So I guess I just go past all odd non-prime numbers and keep subtracting all double squares that fit and see if the rest is a prime
|
||||
|
||||
import math
|
||||
import numpy as np
|
||||
|
||||
def sieve(n):
|
||||
assert n > 1
|
||||
|
||||
ns = [True] * n
|
||||
|
||||
for i in range(2, math.ceil(np.sqrt(n))):
|
||||
if ns[i]:
|
||||
j = pow(i, 2)
|
||||
|
||||
while j < n:
|
||||
ns[j] = False
|
||||
j = j + i
|
||||
|
||||
return [i for i,val in enumerate(ns) if val][2:]
|
||||
|
||||
def main():
|
||||
print("Hello this is Patrick")
|
||||
|
||||
end = 10000
|
||||
|
||||
primes = set(sieve(end))
|
||||
|
||||
for n in range(9, end, 2):
|
||||
termination = True
|
||||
|
||||
if n not in primes:
|
||||
x = 1
|
||||
while n - 2 * x*x > 0:
|
||||
if n - 2 * x*x in primes:
|
||||
termination = False
|
||||
break
|
||||
|
||||
x += 1
|
||||
|
||||
if termination:
|
||||
print(n)
|
||||
break
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user