Tổng số nguyên tố trong đầu vào
Đưa ra một danh sách các số nguyên, hãy viết một chương trình để in tổng của tất cả các số nguyên tố trong danh sách các số nguyên.
Lưu ý: một không phải là số nguyên tố hay số tổng hợp
Đầu vào sẽ là một dòng duy nhất chứa các số nguyên được phân tách không gian..Output
Đầu ra phải là một dòng duy nhất chứa tổng của tất cả các số nguyên tố từ 1 đến n.explanation
Ví dụ: nếu danh sách các số nguyên đã cho là
2 4 5 6 7 3 8
Như 2, 3, 5 và 7 là số nguyên tố, mã của bạn nên in tổng của các số này. Vì vậy, đầu ra phải là 17.
Đầu vào mẫu 1
2 4 5 6 7 3 8
Đầu ra mẫu 1
17
Đầu vào mẫu 2
65 87 96 31 32 86 57 69 20 42
Đầu ra mẫu 2
31
Sum of Prime Numbers In the Input
Given a list of integers, write a program to print the sum of all prime numbers in the list of integers.
Note: One is neither prime nor composite number.
Input
The input will be a single line containing space-separated integers..
Output
The output should be a single line containing the sum of all prime numbers from 1 to N.
Explanation
For example, if the given list of integers are
2 4 5 6 7 3 8
As 2, 3, 5 and 7 are prime numbers, your code should print the sum of these numbers. So the output should be 17.
Sample Input 1
2 4 5 6 7 3 8
Sample Output 1
17
Sample Input 2
65 87 96 31 32 86 57 69 20 42
Sample Output 2
31
#get numbers
numbers=input[""].split[' ']
sumPrimeNumbers=0
for n in numbers:
n=int[n]
isPrime=True
if n > 1:
#Check if number if prime
for i in range[2, n]:
if [n % i] == 0:
isPrime=False
break
if isPrime:
sumPrimeNumbers+=n
#Display the sum
print[str[sumPrimeNumbers]]
Tìm hiểu thêm về sự giúp đỡ của chúng tôi với các bài tập: Python
1
Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.
Tôi bị tấn công và cần giúp đỡ với điều này .. Tôi muốn tìm tổng số số nguyên tố từ một danh sách số nguyên nhất định. Dưới đây là một vài trường hợp thử nghiệm cho cùng.
n[[3,3,1,13]]
19
n[[2,4,6,9,11]]
13
n[[-3,0,1,6]]
0
Mã mà tôi đã viết như sau nhưng nó không thành công với các trường hợp kiểm tra ở trên ..
def sumprimes[n]:
sum1 = 0
for i in range[0,len[n]]:
num = n[i]
if num > 1:
for j in range[2, int[num**0.5]+1]:
if num%j != 0:
sum1 = sum1 + num
else:
sum1 = 0
return[sum1]
OHAD EYTAN
7,6391 Huy hiệu vàng21 Huy hiệu bạc31 Huy hiệu đồng1 gold badge21 silver badges31 bronze badges
Khi được hỏi ngày 2 tháng 8 năm 2016 lúc 22:07Aug 2, 2016 at 22:07
Phần này là sai:
for j in range[2, int[num**0.5]+1]:
if num%j != 0:
sum1 = sum1 + num
Bạn đang tổng hợp
Sum of Prime Numbers In the Input
Given a list of integers, write a program to print the sum of all prime numbers in the list of integers.
Note: One is neither prime nor composite number.
Input
The input will be a single line containing space-separated integers..
Output
The output should be a single line containing the sum of all prime numbers from 1 to N.
Explanation
For example, if the given list of integers are
2 4 5 6 7 3 8
As 2, 3, 5 and 7 are prime numbers, your code should print the sum of these numbers. So the output should be 17.
Sample Input 1
2 4 5 6 7 3 8
Sample Output 1
17
Sample Input 2
65 87 96 31 32 86 57 69 20 42
Sample Output 2
31
8 cho mỗi số trong phạm vi không phân chia. Bạn nên tổng hợp nếu tất cả chúng không chia.all of them didn't divide.Cách đơn giản để làm điều này là:
prime = True
for j in range[2, int[num**0.5]+1]:
if num%j == 0:
prime = False
break
if prime:
sum1 = sum1 + num
Hoặc theo một cách pythonic hơn bằng cách sử dụng
Sum of Prime Numbers In the Input
Given a list of integers, write a program to print the sum of all prime numbers in the list of integers.
Note: One is neither prime nor composite number.
Input
The input will be a single line containing space-separated integers..
Output
The output should be a single line containing the sum of all prime numbers from 1 to N.
Explanation
For example, if the given list of integers are
2 4 5 6 7 3 8
As 2, 3, 5 and 7 are prime numbers, your code should print the sum of these numbers. So the output should be 17.
Sample Input 1
2 4 5 6 7 3 8
Sample Output 1
17
Sample Input 2
65 87 96 31 32 86 57 69 20 42
Sample Output 2
31
9: if all[num%j != 0 for j in range[2, int[num**0.5]+1]]:
sum1 = sum1 + num
Graham
7.22317 Huy hiệu vàng58 Huy hiệu bạc83 Huy hiệu đồng17 gold badges58 silver badges83 bronze badges
Đã trả lời ngày 2 tháng 8 năm 2016 lúc 22:20Aug 2, 2016 at 22:20
Ohad Eytanohad EytanOhad Eytan
7,6391 Huy hiệu vàng21 Huy hiệu bạc31 Huy hiệu đồng1 gold badge21 silver badges31 bronze badges
2
Đừng cố gắng làm mọi thứ trong một chức năng. Tôi đã tách ra phần còn lại của logic, nhưng tôi sẽ để lại cho bạn:
def isprime[x]:
# Replace this with your code
# separate function so it can have its own tests
return x in [3, 13, 11, 2]
def sum_prime_numbers_in_list[l]:
return sum[[x for x in l if isprime[x]]]
if 19 != sum_prime_numbers_in_list[[3, 3, 1, 13]]:
raise ValueError
else:
print 'pass'
if 13 != sum_prime_numbers_in_list[[2, 4, 6, 9, 11]]:
raise ValueError
else:
print 'pass'
if 0 != sum_prime_numbers_in_list[[-3, 0, 1, 6]]:
raise ValueError
else:
print 'pass'
Đã trả lời ngày 2 tháng 8 năm 2016 lúc 22:42Aug 2, 2016 at 22:42
Kenny Ostromkenny OstromKenny Ostrom
5.2542 Huy hiệu vàng18 Huy hiệu bạc29 Huy hiệu đồng2 gold badges18 silver badges29 bronze badges
lower = int[input["Enter lower range: "]]
upper = int[input["Enter upper range: "]]
lst=[]
print["Prime numbers between",lower,"and",upper,"are:"]
for num in range[lower,upper + 1]:
if num > 1:
for i in range[2,num]:
if [num % i] == 0:
break
else:
print[num]
lst.append[num]
print[lst]
sum=0
for i in lst:
sum=sum+i
print['sum of all prime numbers=%d'%sum]
Đã trả lời ngày 8 tháng 3 năm 2019 lúc 6:32Mar 8, 2019 at 6:32
Sum of Prime Numbers In the Input
Given a list of integers, write a program to print the sum of all prime numbers in the list of integers.
Note: One is neither prime nor composite number.
Input
The input will be a single line containing space-separated integers..
Output
The output should be a single line containing the sum of all prime numbers from 1 to N.
Explanation
For example, if the given list of integers are
2 4 5 6 7 3 8
As 2, 3, 5 and 7 are prime numbers, your code should print the sum of these numbers. So the output should be 17.
Sample Input 1
2 4 5 6 7 3 8
Sample Output 1
17
Sample Input 2
65 87 96 31 32 86 57 69 20 42
Sample Output 2
31
0Đã trả lời ngày 6 tháng 5 năm 2021 lúc 5:10May 6, 2021 at 5:10
1
Câu trả lời nên là điều này. [Bạn nên thử mã này, trong liên kết này.
Sum of Prime Numbers In the Input
Given a list of integers, write a program to print the sum of all prime numbers in the list of integers.
Note: One is neither prime nor composite number.
Input
The input will be a single line containing space-separated integers..
Output
The output should be a single line containing the sum of all prime numbers from 1 to N.
Explanation
For example, if the given list of integers are
2 4 5 6 7 3 8
As 2, 3, 5 and 7 are prime numbers, your code should print the sum of these numbers. So the output should be 17.
Sample Input 1
2 4 5 6 7 3 8
Sample Output 1
17
Sample Input 2
65 87 96 31 32 86 57 69 20 42
Sample Output 2
31
1Đã trả lời ngày 4 tháng 8 năm 2016 lúc 13:57Aug 4, 2016 at 13:57
2
Sum of Prime Numbers In the Input
Given a list of integers, write a program to print the sum of all prime numbers in the list of integers.
Note: One is neither prime nor composite number.
Input
The input will be a single line containing space-separated integers..
Output
The output should be a single line containing the sum of all prime numbers from 1 to N.
Explanation
For example, if the given list of integers are
2 4 5 6 7 3 8
As 2, 3, 5 and 7 are prime numbers, your code should print the sum of these numbers. So the output should be 17.
Sample Input 1
2 4 5 6 7 3 8
Sample Output 1
17
Sample Input 2
65 87 96 31 32 86 57 69 20 42
Sample Output 2
31
2Đã trả lời ngày 9 tháng 2 năm 2017 lúc 14:08Feb 9, 2017 at 14:08
1
Sum of Prime Numbers In the Input
Given a list of integers, write a program to print the sum of all prime numbers in the list of integers.
Note: One is neither prime nor composite number.
Input
The input will be a single line containing space-separated integers..
Output
The output should be a single line containing the sum of all prime numbers from 1 to N.
Explanation
For example, if the given list of integers are
2 4 5 6 7 3 8
As 2, 3, 5 and 7 are prime numbers, your code should print the sum of these numbers. So the output should be 17.
Sample Input 1
2 4 5 6 7 3 8
Sample Output 1
17
Sample Input 2
65 87 96 31 32 86 57 69 20 42
Sample Output 2
31
3Đã trả lời ngày 13 tháng 2 năm 2019 lúc 17:43Feb 13, 2019 at 17:43
1
Sum of Prime Numbers In the Input
Given a list of integers, write a program to print the sum of all prime numbers in the list of integers.
Note: One is neither prime nor composite number.
Input
The input will be a single line containing space-separated integers..
Output
The output should be a single line containing the sum of all prime numbers from 1 to N.
Explanation
For example, if the given list of integers are
2 4 5 6 7 3 8
As 2, 3, 5 and 7 are prime numbers, your code should print the sum of these numbers. So the output should be 17.
Sample Input 1
2 4 5 6 7 3 8
Sample Output 1
17
Sample Input 2
65 87 96 31 32 86 57 69 20 42
Sample Output 2
31
4
Eric Aya
69.2K34 Huy hiệu vàng176 Huy hiệu bạc247 Huy hiệu đồng34 gold badges176 silver badges247 bronze badges
Đã trả lời ngày 4 tháng 8 lúc 17:52Aug 4 at 17:52
3
Sum of Prime Numbers In the Input
Given a list of integers, write a program to print the sum of all prime numbers in the list of integers.
Note: One is neither prime nor composite number.
Input
The input will be a single line containing space-separated integers..
Output
The output should be a single line containing the sum of all prime numbers from 1 to N.
Explanation
For example, if the given list of integers are
2 4 5 6 7 3 8
As 2, 3, 5 and 7 are prime numbers, your code should print the sum of these numbers. So the output should be 17.
Sample Input 1
2 4 5 6 7 3 8
Sample Output 1
17
Sample Input 2
65 87 96 31 32 86 57 69 20 42
Sample Output 2
31
5Đã trả lời ngày 26 tháng 8 năm 2018 lúc 17:07Aug 26, 2018 at 17:07
Sum of Prime Numbers In the Input
Given a list of integers, write a program to print the sum of all prime numbers in the list of integers.
Note: One is neither prime nor composite number.
Input
The input will be a single line containing space-separated integers..
Output
The output should be a single line containing the sum of all prime numbers from 1 to N.
Explanation
For example, if the given list of integers are
2 4 5 6 7 3 8
As 2, 3, 5 and 7 are prime numbers, your code should print the sum of these numbers. So the output should be 17.
Sample Input 1
2 4 5 6 7 3 8
Sample Output 1
17
Sample Input 2
65 87 96 31 32 86 57 69 20 42
Sample Output 2
31
6Đã trả lời ngày 21 tháng 8 năm 2018 lúc 17:09Aug 21, 2018 at 17:09
2
Sum of Prime Numbers In the Input
Given a list of integers, write a program to print the sum of all prime numbers in the list of integers.
Note: One is neither prime nor composite number.
Input
The input will be a single line containing space-separated integers..
Output
The output should be a single line containing the sum of all prime numbers from 1 to N.
Explanation
For example, if the given list of integers are
2 4 5 6 7 3 8
As 2, 3, 5 and 7 are prime numbers, your code should print the sum of these numbers. So the output should be 17.
Sample Input 1
2 4 5 6 7 3 8
Sample Output 1
17
Sample Input 2
65 87 96 31 32 86 57 69 20 42
Sample Output 2
31
7
Thepyguy
16.8K5 Huy hiệu vàng16 Huy hiệu bạc42 Huy hiệu đồng5 gold badges16 silver badges42 bronze badges
Đã trả lời ngày 15 tháng 6 năm 2021 lúc 18:25Jun 15, 2021 at 18:25
1