Solution to problem 46
This commit is contained in:
parent
c8403e627e
commit
c034f97431
48
src/Python/Problem046.py
Normal file
48
src/Python/Problem046.py
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
Created on 12 Sep 2021
|
||||||
|
|
||||||
|
@author: David Doblas Jiménez
|
||||||
|
@email: daviddoji@pm.me
|
||||||
|
|
||||||
|
Solution for problem 46 of Project Euler
|
||||||
|
https://projecteuler.net/problem=46
|
||||||
|
"""
|
||||||
|
|
||||||
|
from utils import timeit, is_prime
|
||||||
|
|
||||||
|
def is_goldbach(number):
|
||||||
|
for i in range(number - 1, 1, -1):
|
||||||
|
if is_prime(i) and ((number - i) / 2)**0.5 % 1 == 0:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
@timeit("Problem 46")
|
||||||
|
def compute():
|
||||||
|
"""
|
||||||
|
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×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
|
||||||
|
|
||||||
|
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?
|
||||||
|
"""
|
||||||
|
ans = 9
|
||||||
|
while True:
|
||||||
|
ans += 2
|
||||||
|
if not is_prime(ans) and not is_goldbach(ans):
|
||||||
|
return ans
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
|
||||||
|
print(f"Result for Problem 46: {compute()}")
|
Loading…
x
Reference in New Issue
Block a user