Print all numbers that can be obtained by adding A or B to N exactly M times | Function to find all possible numbers that can be obtained by adding A or B to N exactly M times ; For maintaining increasing order ; Smallest number that can be obtained ; If A and B are equal , then only one number can be obtained , i . e . N + M * A ; For finding others numbers , subtract A from number once and add B to number once ; Given Input ; Function Call
function possibleNumbers ( N , M , A , B ) { var i ; if ( A > B ) { var temp = A ; A = B ; B = temp ; } var number = N + M * A ; document . write ( number + " " ) ; if ( A != B ) { for ( i = 0 ; i < M ; i ++ ) { number = number - A + B ; document . write ( number + " " ) ; } } } var N = 5 , M = 3 , A = 4 , B = 6 ; possibleNumbers ( N , M , A , B ) ;