我是python编程的初学者。 我试图找到一个范围内的素数(输入将由用户决定)和素数的总数。
我能够找到素数但不能完成这一部分 - 计算素数。 谁能帮我? 这是程序
#!/usr/bin/env python
import math
lower=input("lower value:")
upper=input("upper value:")
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)
def count_prime(num,lower,upper):
count_prime= 0
for x in num:
if lower <= x <= upper:
count_prime += 1
print ( "count_prime:",count_prime )
结果输出如下(范围在10到100之间)
(',10,'和',100之间的'素数'是:') 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 ('count_prime:',)
答案 0 :(得分:1)
最简单的方法是让每次找到素数时都会增加一个变量。因此,当您打印数字时,也将该计数增加1。您可以通过其他各种方式执行此操作,即将每个素数保存在数组中,然后计算元素数量等。
与变量有关:
/usr/local/lib/python3.6/site-packages/line_profiler.py:321: UserWarning: Could not extract a code object for the object cnt time
6 15 17 30 5 (2003-03-09 00:00:00, 2003-10-05 00:00:00, 200...
9 19 20 21 2 (2003-02-27 00:00:00, 2011-08-21 00:00:00)
4 20 21 26 2 (2003-02-27 00:00:00, 2011-05-19 00:00:00)
6 7 10 25 1 (2003-03-06 00:00:00,)
4 6 17 30 3 (2003-03-09 00:00:00, 2011-06-09 00:00:00, 201...
9 20 21 1 (2003-02-27 00:00:00,)
10 11 26 28 1 (2003-02-23 00:00:00,)
7 23 28 32 1 (2003-03-02 00:00:00,)
4 6 15 30 3 (2003-03-09 00:00:00, 2003-10-02 00:00:00, 200...
13 25 1 (2003-03-06 00:00:00,)
10 25 3 (2003-03-06 00:00:00, 2005-05-22 00:00:00, 200...
11 12 13 28 2 (2003-02-23 00:00:00, 2018-01-28 00:00:00)
7 10 13 25 5 (2003-03-06 00:00:00, 2007-01-09 00:00:00, 200...
4 19 20 21 1 (2003-02-27 00:00:00,)
1 7 10 28 1 (2003-03-02 00:00:00,)
32 3 (2003-03-02 00:00:00, 2004-02-01 00:00:00, 200...
4 15 30 31 3 (2003-03-09 00:00:00, 2006-10-31 00:00:00, 201...
6 30 31 2 (2003-03-09 00:00:00, 2013-02-07 00:00:00)
11 13 26 28 2 (2003-02-23 00:00:00, 2018-01-28 00:00:00)
10 11 12 28 2 (2003-02-23 00:00:00, 2013-07-11 00:00:00)
7 10 23 28 3 (2003-03-02 00:00:00, 2017-08-06 00:00:00, 201...
4 9 20 26 2 (2003-02-27 00:00:00, 2007-01-02 00:00:00)
7 10 28 32 1 (2003-03-02 00:00:00,)
4 7 13 25 2 (2003-03-06 00:00:00, 2010-03-07 00:00:00)
1 7 28 32 2 (2003-03-02 00:00:00, 2015-12-08 00:00:00)
10 11 12 26 4 (2003-02-23 00:00:00, 2008-10-23 00:00:00, 201...
6 17 30 31 2 (2003-03-09 00:00:00, 2004-11-30 00:00:00)
4 6 7 13 1 (2003-03-06 00:00:00,)
1 7 23 28 3 (2003-03-02 00:00:00, 2005-01-02 00:00:00, 201...
19 20 21 26 4 (2003-02-27 00:00:00, 2005-03-31 00:00:00, 200...
10 12 13 26 4 (2003-02-23 00:00:00, 2011-08-25 00:00:00, 201...
4 7 10 25 3 (2003-03-06 00:00:00, 2010-03-07 00:00:00, 201...
11 12 26 28 2 (2003-02-23 00:00:00, 2018-01-28 00:00:00)
1 7 23 32 1 (2003-03-02 00:00:00,)
4 6 15 31 1 (2003-03-09 00:00:00,)
17 31 2 (2003-03-09 00:00:00, 2007-12-02 00:00:00)
15 17 31 2 (2003-03-09 00:00:00, 2006-12-14 00:00:00)
9 21 26 4 (2003-02-27 00:00:00, 2009-02-05 00:00:00, 201...
6 15 30 31 2 (2003-03-09 00:00:00, 2005-05-19 00:00:00)
10 12 13 28 1 (2003-02-23 00:00:00,)
4 9 19 21 1 (2003-02-27 00:00:00,)
10 11 13 26 1 (2003-02-23 00:00:00,)
4 6 7 25 4 (2003-03-06 00:00:00, 2007-07-08 00:00:00, 201...
7 10 13 2 (2003-03-06 00:00:00, 2010-03-07 00:00:00)
1 10 23 28 2 (2003-03-02 00:00:00, 2016-01-07 00:00:00)
28 32 1 (2003-03-02 00:00:00,)
9 20 21 26 2 (2003-02-27 00:00:00, 2011-08-21 00:00:00)
6 7 13 25 2 (2003-03-06 00:00:00, 2003-08-10 00:00:00)
4 10 13 25 4 (2003-03-06 00:00:00, 2007-11-15 00:00:00, 201...
17 30 31 1 (2003-03-09 00:00:00,)
9 19 26 1 (2003-02-27 00:00:00,)
10 13 26 28 2 (2003-02-23 00:00:00, 2006-11-14 00:00:00)
6 7 10 13 1 (2003-03-06 00:00:00,)
4 19 20 26 2 (2003-02-27 00:00:00, 2016-03-29 00:00:00)
15 17 30 31 1 (2003-03-09 00:00:00,)
4 6 7 10 2 (2003-03-06 00:00:00, 2014-03-02 00:00:00)
6 15 17 31 2 (2003-03-09 00:00:00, 2015-02-05 00:00:00)
10 11 12 13 1 (2003-02-23 00:00:00,)
7 10 23 32 1 (2003-03-02 00:00:00,)
4 6 10 13 2 (2003-03-06 00:00:00, 2011-01-09 00:00:00)
15 17 30 1 (2003-03-09 00:00:00,)
12 13 26 28 2 (2003-02-23 00:00:00, 2018-01-28 00:00:00)
4 6 15 17 2 (2003-03-09 00:00:00, 2009-02-10 00:00:00)
9 19 21 26 4 (2003-02-27 00:00:00, 2003-03-16 00:00:00, 201...
11 12 13 26 2 (2003-02-23 00:00:00, 2018-01-28 00:00:00)
4 9 19 20 2 (2003-02-27 00:00:00, 2004-04-29 00:00:00)
19 21 26 1 (2003-02-27 00:00:00,)
10 23 28 32 2 (2003-03-02 00:00:00, 2013-07-11 00:00:00)
9 19 20 26 4 (2003-02-27 00:00:00, 2008-02-24 00:00:00, 201...
1 10 23 32 1 (2003-03-02 00:00:00,)
10 11 13 28 1 (2003-02-23 00:00:00,)
6 10 13 25 2 (2003-03-06 00:00:00, 2014-07-24 00:00:00)
1 7 10 23 2 (2003-03-02 00:00:00, 2017-04-13 00:00:00)
23 28 32 2 (2003-03-02 00:00:00, 2007-10-21 00:00:00)
10 12 26 28 2 (2003-02-23 00:00:00, 2008-08-10 00:00:00)
profile = LineProfiler(*funcs)
与数组有关:
lower=input("lower value:")
upper=input("upper value:")
count = 0
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)
count += 1
print ( "count_prime:", count )
答案 1 :(得分:0)
要计算素数的数量,您可以先使用列表存储素数,然后使用len
来查找素数的数量。这很容易理解:
import math
store = []
lower=int(input("lower value:"))
upper=int(input("upper value:"))
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)
store.append(num)
print("The number of prime numbers are:",len(store))
答案 2 :(得分:0)
“ sympy”库在这种情况下会很有帮助
import sympy
lower=int(input("lower value:")) #let it be 30
upper=int(input("upper value:")) #let it be 60
l=list(sympy.primerange(lower,upper+1)) #[31,37,41,43,47,53,59]
z=len(l)
print(z)
答案 3 :(得分:0)
我发现了其他一些方法可以计算在https://stackoverflow.com/a/53451147/4683899.In处发现的素数,您可以自定义列表。
def is_prime(n):
if n <= 1: # negative numbers, 0 or 1
return False
if n <= 3: # 2 and 3
return True
if n % 2 == 0 or n % 3 == 0:
return False
for i in range(5, int(math.sqrt(n)) + 1, 2):
if n % i == 0:
return False
return True
ans = [i for i in input().split() if is_prime(int(i))]
print(len(ans))
或者如果您想在范围内完成相同的任务,则只需编辑 input()。split()到range(lower,upper + 1)
ans = [i for i in range(lower,upper+1) if is_prime(i)]
print(len(ans))