[英]How to find prime numbers in python
I'm new to Python.我是 Python 的新手。 I am trying to count the prime numbers in a given range.
我正在尝试计算给定范围内的素数。 Some of the answers that developers have shared are like this:
开发者分享的一些答案是这样的:
import math
def count_primes(num):
out = []
for i in range(3,num,2):
if all(i%j!=0 for j in range(3,int(math.sqrt(i))+1,2)):
out.append(i)
print(out)
I wrote a one like this:我写了一个这样的:
import math
def count_primes(num):
out = []
for i in range(3,num,2):
for j in range(3, int(math.sqrt(i))+1,2):
if i%j != 0:
out.append(i)
print(out)
but it doesn't work.但它不起作用。 Could somebody please help me.
有人可以帮助我吗? Appreciated!
赞赏!
Neither of your example count_primes()
functions actually counts primes -- they simply print odd primes.您的示例
count_primes()
函数实际上都没有计算素数——它们只是打印奇数素数。 Let's implement a working version of your trial division code, not using confusing booleans and a bad algorithm, but rather taking advantage of Python's else
clause on for
loops:让我们实现一个试用版代码的工作版本,不要使用令人困惑的布尔值和糟糕的算法,而是利用 Python 在
for
循环中的else
子句:
def collect_odd_primes(number):
primes = []
for candidate in range(3, number, 2):
for divisor in range(3, int(candidate ** 0.5) + 1, 2):
if candidate % divisor == 0:
break
else: # no break
primes.append(candidate)
return primes
print(collect_odd_primes(40))
OUTPUT OUTPUT
> python3 test.py
[3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
>
As @MarkRansom comments, the Sieve of Eratosthenes is the better way to go.正如@MarkRansom 评论的那样, Eratosthenes 的筛子是 go 的更好方法。 (+1) Now, let's convert our code to count odd primes instead:
(+1) 现在,让我们将代码转换为计算奇数素数:
def count_odd_primes(number):
count = 0
for candidate in range(3, number, 2):
for divisor in range(3, int(candidate ** 0.5) + 1, 2):
if candidate % divisor == 0:
break
else: # no break
count += 1
return count
print(count_odd_primes(40))
OUTPUT OUTPUT
> python3 test.py
11
>
Something like this should work.像这样的东西应该工作。 You have to set a variable, because
15%9 != 0
, outputs True.您必须设置一个变量,因为
15%9 != 0
输出 True。
import math
def count_primes(num):
out = []
for i in range(3,num,2):
prime = True
for j in range(3, int(math.sqrt(i))+1,2):
if i%j == 0:
prime = False
if prime:
out.append(i)
print(out)
count_primes(15)
The reason your code and the other is is different is because their use of the all()
method.您的代码和另一个代码不同的原因是它们使用了
all()
方法。 Have a look at how i implemented the method using bool
s:看看我是如何使用
bool
s 实现该方法的:
import math
def count_primes(num):
out = []
for i in range(3,num,2):
f = True
for j in range(3,int(math.sqrt(i))+1,2):
if i%j==0:
f = False
break
if f:
out.append(i)
print(out)
count_primes(20)
Output: Output:
[3, 5, 7, 11, 13, 17, 19]
You're appending to the result of the module is unequal to zero.您附加到模块的结果不等于零。 However, it's only a prime number if all modulo are unequal to zero (there is an all statement missing in your code).
但是,如果所有模都不等于零,它只是一个素数(代码中缺少 all 语句)。
Depending on what program you're running for your code-writing, an alternative method—as opposed to the other answers to this question—would be to write:根据您为编写代码而运行的程序,另一种方法(与此问题的其他答案相反)将是:
n = int(input("Write an integer:"))
m = 2
if n == 1:
print(n, "is a prime number!")
if n == 2:
print(n, "is not a prime number.")
while n > m:
if n % m == 0:
m = m + 1
print(n, "is not a prime number.")
break
if n > n % m > 0:
m = m + 1
print(n, "is a prime number!")
break
It may not be the most efficient, but it gives you a really nice, straight answer to whether or not "x" is a prime number!它可能不是最有效的,但它为您提供了一个非常好的、直接的答案来判断“x”是否是质数!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.