python第49讲作业
我的代码import math
def isprime(i):
if i > 1:
if i==2:
return 1
if i%2==0:
return 0
for current in range(3, int(math.sqrt(i) + 1), 2):
if i % current == 0:
return False
return 1
return 0
def getprime(i):
while 1:
if isprime(i):
yield i
i=i+1
def solve():
num=2
for j in getprime(3):
if j<2000000:
num+=j
else:
print(num)
break
if __name__ == '__main__':
solve()
小甲鱼的代码
import math
def 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):
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()
为什么我的代码一直在执行就是不出结果呢 缩进的问题,检查一下getprime里面 i = i+1的缩紧看看 import math
def isprime(i):
if i > 1:
if i==2:
return 1
if i%2==0:
return 0
for current in range(3, int(math.sqrt(i) + 1), 2):
if i % current == 0:
return False
return 1
return 0
def getprime(i):
while 1:
if isprime(i):
yield i
i=i+1 #缩进缩一下子
def solve():
num=2
for j in getprime(3):
if j<2000000:
num+=j
else:
print(num)
break
if __name__ == '__main__':
solve()
以后代码框写代码啊
页:
[1]