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
- Java one line of code to print a heart
- Linux performance monitoring software Netdata Chinese version
- Interface Test Code Coverage (JACOCO) solution sharing
- Performance testing framework
- How to perform performance tests on a Linux command line interface
- Graphic HTTP brain map
- How do I test a probabilistic business interface
- Httpclient handles multiple online users simultaneously
- Automatically turn the Swagger document into test code
- Five lines of code to build a static blog
- How does HttpClient handle 302 redirects
- Probe into the Linear interface testing framework based on Java
Selected non-technical articles
- Why software testing as a career path?
- 10 steps to becoming a Great Java developer
- Programming thinking for everyone
- Barriers to automated testing
- The problem with automated testing
- Test the Code Immortal brain map