human
Absolute difference between the Product of Non | Function to find the difference between the product of non - primes and the product of primes of an array ..
Find maximum value in the array.
USE SIEVE TO FIND ALL PRIME NUMBERS LESS THAN OR EQUAL TO max_val Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true ..
Remaining part of SIEVE.
If prime [ p ] is not changed , then it is a prime.
Update all multiples of p.
Store the product of primes in P1 and the product of non primes in P2.
the number is prime.
the number is non - prime.
Return the absolute difference.
Driver Code.
Find the absolute difference"
Solution in Python:
assistant
#Here is code to absolute difference between the Product of Non and the other steps required.
 def calculateDifference ( arr , n ) :
	max_val = max ( arr )
	prime = ( max_val + 1 ) * [ True ]
	prime [ 0 ] = False
	prime [ 1 ] = False
	p = 2
	while p * p <= max_val :
		if prime [ p ] == True :
			for i in range ( p * 2 , max_val + 1 , p ) :
				prime [ i ] = False
		p += 1
	P1 = 1 ; P2 = 1
	for i in range ( n ) :
		if prime [ arr [ i ] ] :
			P1 *= arr [ i ]
		elif arr [ i ] != 1 :
			P2 *= arr [ i ]
	return abs ( P2 - P1 )
if __name__ == ' _ _ main _ _ ' :
	arr = [ 1 , 3 , 5 , 10 , 15 , 7 ]
	n = len ( arr )
	print ( calculateDifference ( arr , n ) )