Today to see Baidu know someone asked how to verify that the number within 1000 matches Goldbach conjecture, wrote a
Feel more than 10000 when a bit slow ah, and Java compared to the efficiency of the point, I hope the master can give optimization under
#!/usr/bin/env python__author__ = ' Huainan drizzling ' "" "Script authoring Environment Python 3.4.2 Goldbach conjecture simple Validation" "" Import MathClass Goldbach: "" Goldbach conjecture: any large An even number of 2 can be written as the sum of two prime numbers, "" "Def __init__ (Self, n=100):" "" Goldbach conjecture constructor:p Aram N/A even satisfies Goldbach conjecture : Return: "" "SELF.N = n def check (self):" "verifies that an even number within N is consistent with Goldbach conjecture: return:" "If SELF.N = = 1 or SELF.N = = 2:print (str (SELF.N), ' an even number within ' satisfies Goldbach conjecture ') return True it = (I for I in range (3,self.n+1) if I% 2 = = 0) # Remove an even-numbered iterator to validate for I in It:if not self._conform (i) : Print (' even: ', str (i), ' not satisfying Goldbach conjecture ') Break print (str (SELF.N), ' an even number within ' satisfies Goldbach conjecture ') def _conform (self,n): "" "to determine n conforms to Goldbach conjecture:p Aram N: Even n:return:" "num = N//2 + 1 For I in Range (2,num): If Self._is_prime (i) and Self._is_prime (n-i): Return True return False def _is_Prime (Self,num): "" "Determines whether a number is prime:p Aram Num:: Return:" "" NUM1 = Int (Math.sqrt (nu m)) for I in Range (2,num1+1): if num% i = = 0:return False return trueif __name__ = = ' __main__ ': a = Goldbach (100000) A.check ()
Python Validation Goldbach conjecture