text: Count ways to represent a number as sum of perfect squares | Store perfect squares less than or equal to N ; Utility function to calculate perfect squares less than or equal to N ; Function to find the number of ways to represent a number as sum of perfect squares ; Handle the base cases ; Include the i - th index element ; Exclude the i - th index element ; Return the result ; Given Input ; Precalculate perfect squares <= N ; Function Call
code: var psquare = [ ] function calcPsquare ( N ) { var i ; for ( i = 1 ; i * i <= N ; i ++ ) psquare . push ( i * i ) ; } function countWays ( index , target ) { if ( target == 0 ) return 1 ; if ( index < 0 target < 0 ) return 0 ; var inc = countWays ( index , target - psquare [ index ] ) ; var exc = countWays ( index - 1 , target ) ; return inc + exc ; } var N = 9 ; calcPsquare ( N ) ; document . write ( countWays ( psquare . length - 1 , N ) ) ;