简体   繁体   English

如何使用python查找素数

[英]how to find Prime factors using python

I need to find prime factors of a given number.我需要找到给定数字的质因数。

first i have written code to find the factors of a given numbers and stored as list and tried to loop through the listing if the current number is a prime number or not.首先,我编写了代码来查找给定数字的因数并存储为列表,如果当前数字是否为质数,我会尝试遍历列表。 However, i am getting error as one parameter in is_prime function is not defined.但是,我收到错误,因为 is_prime 函数中的一个参数未定义。

below the is the code written.下面是写的代码。 Could someone help understand how we can find prime factors using below code.有人可以帮助理解我们如何使用下面的代码找到主要因素。

def factors(num):
  factor=[]
  for i in range(2,num+1):
    if num%i==0:
      factor.append(i)
  return factor

num=int(input("Enter your number: "))

number=factors(num)

print("Factors for", num, "are: ", number)

def is_prime(factor):
  for n in factor:
    for i in range(2,n):
      if n%i==0:
        return False
    return True

Prime_factors=[]
prime=is_prime(factor)

if prime:
  prime_factors.append(n)

print(Prime_factors)

The only error I get is that factor is not a valid variable, which is true because it defined only in factors() function, and therefore is accessable only in the function scope.我得到的唯一错误是 factor 不是有效变量,这是正确的,因为它仅在 factor() 函数中定义,因此只能在函数范围内访问。

More, you can take advantage of the first function to figure out if the number is prime or not.此外,您可以利用第一个函数来确定数字是否为质数。 try this:尝试这个:

import math

def factors(num):
  factor=[]
  for i in range(1, math.isqrt(num)+1):
    if num % i == 0:
      factor.append(i)
      if i != num//i:
        factor.append(num//i)
  return factor

num=int(input("Enter your number: "))

number=factors(num)

print("Factors for", num, "are: ", number)

def is_prime(factor):
  if len(factors(factor)) == 2 and factor != 1:
      return True
  else:
      return False


prime_factors=[]

for i in number:
    if is_prime(i):
      prime_factors.append(i)

print(prime_factors)

There were some syntax and logical errors in your code, I have edited the code, removed factor as undefined and moved for loop out from method is_prime to make sense.您的代码中存在一些语法和逻辑错误,我已经编辑了代码,将factor删除为未定义并将 for 循环从方法 is_prime 移出以使其有意义。 The code is as follows:代码如下:

def factors(num):
   factor=[]
   for i in range(2,num+1):
    if num%i==0:
     factor.append(i)
    return factor

num=int(input("Enter your number: "))

number=factors(num)

print("Factors for", num, "are: ", number)

def is_prime(n):
  for i in range(2,n):
    if n%i==0:
      return False
    return True

Prime_factors=[]

for n in number:
  if is_prime(n):
   Prime_factors.append(n)

print(Prime_factors)

You need to find all factors and then select if those factors are prime.您需要找到所有因素,然后选择这些因素是否为素数。 You can build the factors array first and then select prime numbers out of this array like here:您可以先构建 factor 数组,然后从该数组中选择素数,如下所示:

import math

def factors(num):
  factor=[]
  for i in range(2,num+1):
    if num%i==0:
      factor.append(i)
  return factor

num=int(input("Enter your number: "))

number=factors(num)

print("Factors for", num, "are: ", number)

def is_prime(n):
    if n==2: 
      return True
    if n%2==0:
      return False
    return all( n%i !=0 for i in range(3, int(math.sqrt(n)+1),2))

Prime_factors=[]

for n in number:
  if is_prime(n):
   Prime_factors.append(n)

print("Prime factors are: ",Prime_factors)

Output:输出:

Enter your number: 56
Factors for 56 are:  [2, 4, 7, 8, 14, 28, 56]
Prime factors are:  [2, 7]

Save computational time by checking for primary numbers only up to square root of the number.通过只检查主数的平方根来节省计算时间。

Or you can save a little bit of computations and do it in one loop, plus you can check if the number is prime by start and if it is, then you do not need to check for other factors, if it is not prime, then you only need to check for half of its value:或者您可以节省一点计算并在一个循环中进行,另外您可以通过开始检查数字是否为素数,如果是,则不需要检查其他因素,如果不是素数,则您只需要检查其值的一半:

import math

def is_prime(n):
    if n==2:
      return True
    if n%2==0:
      return False
    return all( n%i !=0 for i in range(3, int(math.sqrt(n)+1),2))

def factors(num):
  factor=[]
  if is_prime(num):
    factor.append(num)
  else:
    for i in range(2,int((num+1)/2)):
      if num%i==0 and is_prime(i):
        factor.append(i)
  return factor

num=int(input("Enter your number: "))

factors=factors(num)

print("Prime factors are: ",factors)

And the output is now:现在的输出是:

Enter your number: 17
Prime factors are:  [17]
Enter your number: 116
Prime factors are:  [2, 29]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM