Rebased projecteuler folder, now includes all contest programming stuff

This commit is contained in:
2021-10-26 10:54:24 +02:00
parent 1aa6120838
commit e0c627a384
77 changed files with 203 additions and 67 deletions

60
projecteuler/046/main.py Normal file
View 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()