Python Program to Find the 100th Prime Number
This is a Python program that counts the first 100 prime numbers and outputs the 100th prime number.
count = 0
num = 2
while True:
for i in range(2, num):
if num % i == 0:
break
else:
count += 1
if count < 100:
num += 1
continue
print(num, '是从小到大0')
break
Explanation:
- The program initializes two variables:
countis set to 0, andnumis set to 2. - The
whileloop runs indefinitely until it's broken by thebreakstatement within theifstatement. - Inside the
whileloop, aforloop iterates from 2 to the value ofnum. If any number between 2 andnum(exclusive) dividesnumwith no remainder, theifstatement executes, and the loop breaks usingbreak. Ifnumis prime, theelsestatement executes, andcountis incremented. - After checking if
numis prime, the program checks ifcountis less than 100. If it is,numis incremented by 1, and thecontinuestatement sends the program back to the beginning of thewhileloop. Ifcountis equal to or greater than 100, the program prints the value ofnumand a message indicating it's the 100th prime number, then breaks out of thewhileloop.
Note: The output message '是从小到大0' is likely an error or typo in the original code, as it doesn't make sense in either Chinese or English. It should be replaced with a proper message indicating the 100th prime number has been found.
原文地址: https://www.cveoy.top/t/topic/ouJg 著作权归作者所有。请勿转载和采集!