PTA 1007- prime of conjecture - python timeout solve

1. Topic

Input formats:

It gives a positive integer in the input line N.

Output formats:

In the output line does not exceed the number N satisfy the conjecture of the primes.

Sample input:

20

Sample output:

4

2. The problem encountered

The key to this program is to determine the number of confrontation, have problems running timeout after write good programs, we have through the following changes:

1. Initial release: First of all the data cycle, then the data for a cycle is taken to determine whether it is more than the upper limit of a prime number. Run out.

2. modulo cycle is a half of the upper limit of the data. Run out.

3. modulo cycle as an upper limit to a square root data. Run out.

4. Try a number divisible prime number can be less than its square root
(if a number of C can be (a * b) is divisible, then C% a or C% b is how much? Obviously, the remainder is zero, that is it certainly can be a number divisible by the number divisor of about therefore, all composite number as the dividend is duplication of effort on comprehensive, just need to try a number can be less than its square root of the number divisible by prime)..
ideas source: HTTPS: //blog.csdn.net/stranger61/article/details/77341613
however, still run out.

5. prompted by the students, can not remember even number is a prime number, we only odd-numbered rounds. Blending fourth, and does not run out.

3.python Code

import math
n = input()
l = [2,3]
for i in range(5,int(n)+1,2):
    for j in l:
        if i%j == 0:
            break
        if j > math.sqrt(i):
            l.append(i)
            break
num = 0
#print(l)
for i in range(len(l)-1):
    if l[i+1]-l[i]==2:
       num += 1
print(num)

Guess you like

Origin blog.csdn.net/qq_32188669/article/details/93631355