Python 素数的算法有疑问?求详解
import mathdef is_prime(number):
if number > 1:
if number == 2:
return True
if number % 2 == 0:
return False
for current in range(3, int(math.sqrt(number) + 1), 2):#range语句我明白,我对计算素数的算法有疑问,为什么用到平方根呢?感觉并不能对素数进行筛选
if number % current == 0:
return False
return True
return False
def get_primes(number):
while True:
if is_prime(number):
yield number
number += 1
def solve():
total = 2
for next_prime in get_primes(3):
if next_prime < 2000000:
total += next_prime
else:
print(total)
return
if __name__ == '__main__':
solve() 这个算 数学知识8
https://zhidao.baidu.com/question/398985406213125765.html
页:
[1]