简体   繁体   English

使用Python查找给定数组中Distinct Prime的总数

[英]Find total count of Distinct Prime in given array using Python

The problem is like this - You have given an array A having N integers. 问题是这样的-给定的数组A具有N个整数。 Let say G is the product of all elements of A. You have to find the number of distinct prime divisors of G. Example - Input : A = 1, 2, 3, 4 Output : 2 假设G是A的所有元素的乘积。您必须找到G的不同素数的数量。示例-输入:A = 1,2,3,4输出:2

Explanation : Here g = 1*2*3*4 and distinct prime divisor of g are 2 and 3 To total count of distinct prime divisor = 2 说明:这里g = 1 * 2 * 3 * 4,且g的不同质数为2和3。

Below is the code i wrote but the output which i am getting is wrong - 下面是我写的代码,但是我得到的输出是错误的-

  class prime:
  # @param A : list of integers
  # @return an integer
      def prime(self,num):
          if(num>1):
              for i in range(2,num):
                  if(num%i==0):
                      break
                  else:
                      return num


      def solve(self, A):
          prod = 1
          tot = 0
          for i in range(0,len(A)):
              prod = prod*A[i]
          for i in range(0,len(A)):
              if(self.prime(A[i])):
                  if(prod%self.prime(A[i])==0):
                      tot = tot+1
          return tot



  A = [1,2,3,4]
  prime().solve(A))
class prime:
# @param A : list of integers
# @return an integer
  def prime(self,num):
      if num == 2:  # changes
          return num  # changes
      if(num>2):  # changes
          for i in range(2,num):
              if(num%i==0):
                  break
              else:
                  return num


  def solve(self, A):
      prod = 1
      tot = 0
      for i in range(0,len(A)):
          prod = prod*A[i]
      for i in range(0,len(A)):
          if(self.prime(A[i])):
              if(prod%self.prime(A[i])==0):
                  tot = tot+1
      return tot



A = [1,2,3,4]
print(prime().solve(A))

Lines that were changed were commented with # changes 更改的行用#个更改注释

from math import sqrt

from itertools import count, islice

class Prime:

    def prime(self, n):
        if n < 2:
            return False

        for number in islice(count(2), int(sqrt(n) - 1)):
            if n % number == 0:
                return False

        return True

    def solve(self, A):

        prod = 1
        tot = 0

        for i in range(0, len(A)):
            prod = prod * A[i]

        if(prod<2):
            return 0

        if(prod == 2 or prod == 3):
            return 1

        for i in range(2, prod/2+1):
            if(self.prime(i) and prod % i ==0):
                tot  =tot+1

        return tot

A = [1,2,3,4]
print(Prime().solve(A))

After going through the give inputs and outputs by the OP i understood that OP wants to count the number of primes which can completely divide the prod (product of element) and give remainder as 0. Input 1 by OP - g = 1*2*3*4 and distinct prime divisor of g are 2 and 3. Total count of distinct prime divisor = 2 Input 2 by OP - g = 96*98*5*41*80 and distinct prime divisor of g are 2,3,5,7 and 41. Total count of distinct prime divisor = 5 在经过OP的给定输入和输出后,我了解到OP希望计算可以完全除掉prod(元素乘积)并将剩余数设为0的素数。输入1除以OP-g = 1 * 2 * 3 * 4和g的不同素数的总和是2和3。不同素数的总数量= 2 OP的输入2-g = 96 * 98 * 5 * 41 * 80和g的不同素数的是2,3,5 ,7和41。不同素数除数的总数= 5

Code for the above problem - 以上问题的代码-

class Solution:
# @param A : list of integers
# @return an integer
def prime(self,num):
    if(num==1):
        return 0
    for i in range(2,(num//2+1)):
        if(num%i==0):
            return 0
    return num


def solve(self, A):
    prod = 1
    tot = 0
    for i in range(0,len(A)):
        prod = prod*A[i]
    for i in range(1,(prod//2+1)):
        pr = self.prime(i)
        if(pr):
            #77145600
            print("Prime :",pr)
            if(prod%pr==0):
                tot = tot+1
    return tot



A = [96,98,5,41,80]
print(Solution().solve(A))

But for this code, the response time is very high. 但是对于此代码,响应时间非常高。 For this input - 96,98,5,41,80 the response time was more than 5 hours. 对于此输入-96,98,5,41,80,响应时间超过5小时。 Can anyone provide a better solution for it? 谁能为此提供更好的解决方案?

I found a better solution then the above mentioned by me - 我找到了一个比我上面提到的更好的解决方案-

Updated new code - 更新了新代码-

# Python Program to find the prime number
def prime(num):
    if(num==1):
        return 0
    for i in range(2,(num//2+1)):
        if(num%i==0):
            return 0
    return num

# Python Program to find the factors of a number
def findFactors(x):
   # This function takes a number and finds the factors
   total = 0
   for i in range(1, x + 1):
       if x % i == 0:
           if(prime(i)!=0):
               print("Prime : ",prime(i))
               total+=1
               print("Total : ",total)
    return total               

# change this value for a different result.
num = 77145600
findFactors(num)

The findFactors function first finds the factors of given number and then by using prime function I am checking whether the found factor is prime or not. findFactors函数首先查找给定数的因数,然后使用质数函数检查所发现的因数是否为质数。 If it is a prime number then I am incrementing the total by 1. Execution time is 45 seconds on my system. 如果它是质数,则我将总数加1。我的系统上执行时间为45秒。

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

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