Semiperfect Number | Code to find all the factors of the number excluding the number itself ; vector to store the factors ; note that this loop runs till Math . sqrt ( n ) ; if the value of i is a factor ; condition to check the divisor is not the number itself ; return the vector ; Function to check if the number is semi - perfect or not ; find the divisors ; sorting the vector ; subset to check if no is semiperfect ; initialising 1 st column to true ; initializing 1 st row except zero position to 0 ; loop to find whether the number is semiperfect ; calculation to check if the number can be made by summation of divisors ; If not possible to make the number by any combination of divisors ; Driver code to check if possible
function factors ( n ) { let v = [ ] ; v . push ( 1 ) ; for ( let i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { v . push ( i ) ; if ( Math . floor ( n / i ) != i ) { v . push ( Math . floor ( n / i ) ) ; } } } return v ; } function check ( n ) { let v = [ ] ; v = factors ( n ) ; v . sort ( function ( a , b ) { return a - b ; } ) ; let r = v . length ; let subset = new Array ( r + 1 ) ; for ( let i = 0 ; i < r + 1 ; i ++ ) { subset [ i ] = new Array ( n + 1 ) ; for ( let j = 0 ; j < n + 1 ; j ++ ) { subset [ i ] [ j ] = false ; } } for ( let i = 0 ; i <= r ; i ++ ) subset [ i ] [ 0 ] = true ; for ( let i = 1 ; i <= n ; i ++ ) subset [ 0 ] [ i ] = false ; for ( let i = 1 ; i <= r ; i ++ ) { for ( let j = 1 ; j <= n ; j ++ ) { if ( j < v [ i - 1 ] ) subset [ i ] [ j ] = subset [ i - 1 ] [ j ] ; else { subset [ i ] [ j ] = subset [ i - 1 ] [ j ] || subset [ i - 1 ] [ j - v [ i - 1 ] ] ; } } } if ( ( subset [ r ] [ n ] ) == false ) return false ; else return true ; } let n = 40 ; if ( check ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;