简体   繁体   中英

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. 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

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

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

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. 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. If it is a prime number then I am incrementing the total by 1. Execution time is 45 seconds on my system.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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