Check if a number can be written as sum of three consecutive integers | function to check if a number can be written as sum of three consecutive integer . ; if n is 0 ; if n is positive , increment loop by 1. ; if n is negative , decrement loop by 1. ; Running loop from 0 to n - 2 ; check if sum of three consecutive integer is equal to n . ; Driver program to test above function
function checksum ( n ) { if ( n == 0 ) { document . write ( " " ) ; return ; } var inc ; if ( n > 0 ) inc = 1 ; else inc = - 1 ; for ( i = 0 ; i <= n - 2 ; i += inc ) { if ( i + i + 1 + i + 2 == n ) { document . write ( i + " " + ( i + 1 ) + " " + ( i + 2 ) ) ; return ; } } document . write ( " " ) ; } var n = 6 ; checksum ( n ) ;