I recently read a book “solitude of prime numbers”, which talks about twin prime numbers, want to check the distribution of twin prime numbers. It mainly uses the method of calculating prime numbers (prime numbers). After searching, the top ones are all done by using the for loop, which feels slightly troublesome. In comparison, I still feel that recursive screening method is used to solve this problem.

Create a new List and, starting with the 0th bit, remove the element from the array if the next bit is divisible by that number, and so on, leaving behind a prime number.

The Python version is different from the Java version. Java can delete the element while iterating through the list. You can perform the I — operation on the loop variable I to prevent future get(I) errors

The code is as follows:

#! /usr/bin/python3
 
class Test():
    def __init__(self):
        print ("fan")
    def get(self,list,st):
        n = list[st]
        a = []
        for i in range(st+1,len(list)):
            if list[i] % n == 0:
                a.append(list[i])
        for x in a:
            list.remove(x)
        if len(list) > st+1:
            self.get(list,st+1)
if __name__ == "__main__":
    test = Test()
    list = [i for i inThe test range (2500)]. The get (list, 0)for i in range(len(list)-1):
        a = list[i]
        b = list[i+1]
        if b-a= = 2:print ("Twin prime numbers:"+str(a)+"--"+str(b))
Copy the code

Python limits the depth of recursion in order to prevent memory overflow.

RecursionError: maximum recursion depth exceeded in comparison

Technical articles selected

  1. Java one line of code to print a heart
  2. Linux performance monitoring software Netdata Chinese version
  3. Interface Test Code Coverage (JACOCO) solution sharing
  4. Performance testing framework
  5. How to perform performance tests on a Linux command line interface
  6. Graphic HTTP brain map
  7. How do I test a probabilistic business interface
  8. Httpclient handles multiple online users simultaneously
  9. Automatically turn the Swagger document into test code
  10. Five lines of code to build a static blog
  11. How does HttpClient handle 302 redirects
  12. Probe into the Linear interface testing framework based on Java

Selected non-technical articles

  1. Why software testing as a career path?
  2. 10 steps to becoming a Great Java developer
  3. Programming thinking for everyone
  4. Barriers to automated testing
  5. The problem with automated testing
  6. Test the Code Immortal brain map

Click on the map of the official account