Super Niven Numbers | Checks if sums of all subsets of digits array divides the number N ; to calculate length of array arr ; There are totoal 2 ^ n subsets ; Consider all numbers from 0 to 2 ^ n - 1 ; Consider binary representation of current i to decide which elements to pick . ; check sum of picked elements . ; Function to check if a number is a super - niven number ; to stor digits of N ; Driver code
function isDivBySubsetSums ( arr , num ) { var n = arr . length ; var total = 1 << n ; for ( var i = 0 ; i < total ; i ++ ) { var sum = 0 ; for ( var j = 0 ; j < n ; j ++ ) if ( i & ( 1 << j ) ) sum += arr [ j ] ; if ( sum != 0 && num % sum != 0 ) return false ; } return true ; } function isSuperNivenNum ( n ) { var temp = n ; var digits = [ ] ; while ( n != 0 ) { var digit = n % 10 ; digits . push ( digit ) ; n = parseInt ( n / 10 ) ; } return isDivBySubsetSums ( digits , temp ) ; } var n = 500 ; if ( isSuperNivenNum ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;