把从某个数字开始起的十个素数存放在数组中然后输出这十个素数的总和
时间: 2023-05-11 13:06:04 浏览: 51
Sure, here's the code to store the first 10 prime numbers starting from a given number in an array and then output their sum:
```python
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n**0.5)+1):
if n % i == 0:
return False
return True
def sum_of_primes(start):
primes = []
num = start
while len(primes) < 10:
if is_prime(num):
primes.append(num)
num += 1
return sum(primes)
start = 23 # change this to any number you want
print(sum_of_primes(start))
```
This code defines a function `is_prime(n)` that checks if a number `n` is prime or not. Then, it defines another function `sum_of_primes(start)` that takes a starting number `start`, finds the first 10 prime numbers starting from `start`, stores them in a list `primes`, and returns their sum using the built-in `sum()` function. Finally, it sets `start` to 23 (you can change this to any number you want) and calls `sum_of_primes(start)` to print the sum of the first 10 prime numbers starting from 23.
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)