Print all possible ways to write N as sum of two or more positive integers | Function to print the values stored in vector arr ; Traverse the vector arr ; Recursive function to print different ways in which N can be written as a sum of at 2 or more positive integers ; If n is zero then print this ways of breaking numbers ; Start from previous element in the representation till n ; Include current element from representation ; Call function again with reduced sum ; Backtrack to remove current element from representation ; Given sum N ; To store the representation of breaking N ; Function Call
function printVector ( arr ) { if ( arr . length != 1 ) { for ( var i = 0 ; i < arr . length ; i ++ ) { document . write ( arr [ i ] + " " ) ; } document . write ( " " ) ; } } function findWays ( arr , i , n ) { if ( n == 0 ) printVector ( arr ) ; for ( var j = i ; j <= n ; j ++ ) { arr . push ( j ) ; findWays ( arr , j , n - j ) ; arr . pop ( ) ; } } var n = 4 ; var arr = [ ] ; findWays ( arr , 1 , n ) ;