Sum of all subsets whose sum is a Perfect Number from a given array | Python3 program for the above approach ; Function to check is a given number is a perfect number or not ; Stores the sum of its 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 sum of all subsets from an array whose sum is a perfect number ; Prthe current subset sum if it is a perfect number ; Check if sum is a perfect number or not ; Calculate sum of the subset including arr [ l ] ; Calculate sum of the subset excluding arr [ l ] ; Driver Code
import math NEW_LINE def isPerfect ( x ) : NEW_LINE INDENT sum_div = 1 NEW_LINE for i in range ( 2 , ( x // 2 ) + 1 ) : NEW_LINE INDENT if ( x % i == 0 ) : NEW_LINE INDENT sum_div += i NEW_LINE DEDENT DEDENT if ( sum_div == x ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT def subsetSum ( arr , l , r , sum ) : NEW_LINE INDENT if ( l > r ) : NEW_LINE INDENT if ( isPerfect ( sum ) != 0 ) : NEW_LINE INDENT print ( sum , end = " ▁ " ) NEW_LINE DEDENT return NEW_LINE DEDENT subsetSum ( arr , l + 1 , r , sum + arr [ l ] ) NEW_LINE subsetSum ( arr , l + 1 , r , sum ) NEW_LINE DEDENT arr = [ 5 , 4 , 6 ] NEW_LINE N = len ( arr ) NEW_LINE subsetSum ( arr , 0 , N - 1 , 0 ) NEW_LINE