The 10th Blue Bridge Cup java-c group-prime numbers

1. Problem description:

Total score for this question: 10 points.
We know that the first prime number is 2, the second prime number is 3, and the third prime number is 5... Please calculate the 2019th prime number?
[Answer Submission]
This is a question that fills in the blanks with the result. You only need to calculate the result and submit it. The result of this question is an integer. Only fill in this integer when submitting the answer, and fill in the extra content will not be scored.

2. Thinking analysis:

To analyze the problem, you can know the entire process of simulation. Set a variable to record the current number of prime numbers. One variable is the number currently traversed (starting from 2). When the number is less than 2019, continue to loop until the 2019 prime number is found. Stop the loop, when judging whether the current traversed number is a prime number, you can use j * j <= i as the loop condition for judgment, mainly to solve it violently

3. The code is as follows:

if __name__=="__main__":
    i, count = 2, 0
    while True:
        # 设置一个标记用来判断循环结束之后是否是素数
        j, f = 2, 1
        while j * j <= i:
            if i % j == 0:
               f = 0
            j += 1
        if f: count += 1
        if count == 2019:
            print(i)
            break
        i += 1

 

Guess you like

Origin blog.csdn.net/qq_39445165/article/details/115006553