Find N fractions that sum upto a given fraction N / D | Function to split the fraction into the N parts ; Loop to find the N - 1 fraction ; Loop to print the Fractions ; Driver Code ; Function Call
function splitFraction ( n , d ) { var ar = new Array ( n ) ; var first = d + n - 1 ; ar [ 0 ] = first ; for ( var i = 1 ; i < n ; i ++ ) { ar [ i ] = first * -- first ; } for ( var i = 0 ; i < n ; i ++ ) { if ( ar [ i ] % n === 0 ) { document . write ( " " + ar [ i ] / n + " " ) ; } else { document . write ( n + " " + ar [ i ] + " " ) ; } } } var N = 4 ; var D = 2 ; splitFraction ( N , D ) ;