Chapter 6-2. Use function to find the sum of prime numbers (20 points)

Use a function to find the sum of prime numbers

prime (p), where the function prime returns True when the user passes in the parameter p as a prime number, otherwise returns False. PrimeSum (m, n), the PrimeSum function returns the sum of all prime numbers in the interval [m, n]. The title guarantees that the parameter 1 <= m <n passed by the user.

Function interface definition:

在这里描述函数接口:
prime(p),返回True表示p是素数,返回False表示p不是素数
PrimeSum(m,n),函数返回素数和
 

Sample referee test procedure:


/* 请在这里填写答案 */

m,n=input().split()
m=int(m)
n=int(n)
print(PrimeSum(m,n))
 

Sample input:

Here is a set of inputs. E.g:

1 10
 

Sample output:

The corresponding output is given here. E.g:

17
1  # using the prime number and function evaluation 
2  # the Author: cnRick 
. 3  # Time: 2020-4-10 
. 4  Import Math
 . 5  DEF Prime (P):
 . 6      IF P == 1 :
 . 7          return False
 . 8      the isPrime = True
 . 9      for I in Range (2, int (math.sqrt (p)) + 1 ):
 10          if p% i == 0:
 11              isPrime = False
 12              break 
13      return isPrime
 14  def PrimeSum(m,n):
15     result = 0
16     for num in range(m,n+1):
17         if prime(num) == True:
18             result += num
19     return result
20 m,n = input().split()
21 m = int(m)
22 n = int(n)
23 print(PrimeSum(m,n))

 

 

Guess you like

Origin www.cnblogs.com/dreamcoding/p/12676768.html