Find all the possible remainders when N is divided by all positive integers from 1 to N + 1 | Function to find all the distinct remainders when n is divided by all the elements from the range [ 1 , n + 1 ] ; Set will be used to store the remainders in order to eliminate duplicates ; Find the remainders ; Print the contents of the set ; Driver code
function findRemainders ( n ) { var vc = new Set ( ) ; for ( var i = 1 ; i <= Math . ceil ( Math . sqrt ( n ) ) ; i ++ ) vc . add ( parseInt ( n / i ) ) ; for ( var i = parseInt ( n / Math . ceil ( Math . sqrt ( n ) ) ) - 1 ; i >= 0 ; i -- ) vc . add ( i ) ; [ ... vc ] . sort ( ( a , b ) => a - b ) . forEach ( it => { document . write ( it + " " ) ; } ) ; } var n = 5 ; findRemainders ( n ) ;