我在网上搜索答案,然后设法获得以下代码来查找下一个素数。我了解代码第1部分中的数学部分,其中定义了测试函数是否为质数的函数。
导入数学def isPrime(num):
if num == 1:
return False
square_root = int(math.sqrt(num))
for i in range(2, square_root + 1):
if num % i == 0:
return False
return True
def nextPrime(currentPrime):
flag = True #flag variable as a start, as false. Becomes a boonlean variable. #
while flag == 0: #what the point of doing that?
if currentPrime == 2: ##i guess the reason for doing that is because this is the first prime number
currentPrime += 1 ##so next prime number would essentially be 3, the only prime number sequence that is in the sequence, thus it is hard coded
else:
currentPrime += 2 #whats the point of this?
flag = isPrime(currentPrime) ##this as well
return currentPrime ##i dont get it.
nextPrime(11)