Python Validation Goldbach conjecture

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.