human
What is a code to sum of all subsets whose sum is a Perfect Number from a given array and the other steps required given the following:
Sum of all subsets whose sum is a Perfect Number from a given array | Function to check is a given number is a perfect number or not.
Stores sum of divisors.
Add all divisors of x to sum_div.
If the sum of divisors is equal to the given number , return true.
Otherwise , return false.
Function to find the sum of all the subsets from an array whose sum is a perfect number.
Stores the total number of subsets , i . e . 2 ^ n.
Consider all numbers from 0 to 2 ^ n - 1.
Consider array elements from positions of set bits in the binary representation of n.
If sum of chosen elements is a perfect number.
Driver Code"
Solution in Python:
assistant
#Here is code to sum of all subsets whose sum is a Perfect Number from a given array and the other steps required.
 def isPerfect ( x ) :
	sum_div = 1
	for i in range ( 2 , int ( x / 2 + 1 ) ) :
		if ( x % i == 0 ) :
			sum_div = sum_div + i
	if ( sum_div == x ) :
		return 1
	else :
		return 0
def subsetSum ( arr , n ) :
	total = 1 << n
	for i in range ( total ) :
		sum = 0
		for j in range ( n ) :
			if ( i & ( 1 << j ) != 0 ) :
				sum = sum + arr [ j ]
		if ( isPerfect ( sum ) ) :
			print ( sum , " ▁ " )
arr = [ 5 , 4 , 6 ]
N = len ( arr )
subsetSum ( arr , N )