Python求n以内最大十位素数的和
课程设计
1
Python求n以内最大十位素数的和,如图,应该如何编程??
-
def isprime(n): if n<=1: return False for i in range(2,n): if n%i==0: return False return True def f(n): count = 0 s = 0 while count<10 and n>1: if isprime(n): count += 1 s += n n -= 1 return s p = int(input()) print(f(p))
-
n = int(input()) num = [] i = 2 for i in range(2, n): j = 2 for j in range(2, i): if (i % j == 0): break else: num.append(i) print('所有素数',num) print('最大十位素数',num[-10::]) print('最大十位素数和',sum(num[-10::]))
发表回复