human
Minimum changes required to make all Array elements Prime | Function to generate all primes.
If p is a prime.
Mark all its multiples as non - prime.
Store all prime numbers.
Return the list of primes.
Function to calculate the minimum increments to convert every array elements to a prime.
Extract maximum element of the given array.
Extract the index which has the next greater prime.
Store the difference between the prime and the array element.
Driver code"
How can the above be solved in Python?
 What is a code to minimum changes required to make all Array elements Prime and the other steps required?
assistant
#Here is code to minimum changes required to make all Array elements Prime and the other steps required.
 def SieveOfEratosthenes ( n ) :
	prime = [ True for i in range ( 2 * n + 1 ) ]
	p = 2
	while ( p * p <= 2 * n ) :
		if ( prime [ p ] == True ) :
			i = p * p
			while ( i <= n * 2 ) :
				prime [ i ] = False
				i += p
		p += 1
	primes = [ ]
	for p in range ( 2 , ( 2 * n ) + 1 ) :
		if ( prime [ p ] ) :
			primes . append ( p )
	return primes
def minChanges ( arr ) :
	n = len ( arr )
	ans = 0
	maxi = max ( arr )
	primes = SieveOfEratosthenes ( maxi )
	for i in range ( n ) :
		x = - 1
		for j in range ( len ( primes ) ) :
			if ( arr [ i ] == primes [ j ] ) :
				x = j
				break
			elif ( arr [ i ] < primes [ j ] ) :
				x = j
				break
		minm = abs ( primes [ x ] - arr [ i ] )
		if ( x > 1 ) :
			minm = min ( minm , abs ( primes [ x - 1 ] - arr [ i ] ) )
		ans += minm
	return ans
arr = [ 4 , 25 , 13 , 6 , 20 ]
print ( minChanges ( arr ) )