Remove all triplets which have gcd equal to a multiple of i
for ( var i = 1000000 ; i >= 1 ; i -- ) { for ( var j = 2 * i ; j <= 1000000 ; j += i ) { cnt [ i ] -= cnt [ j ] ; } } document . write ( " " + " " + K ) ; document . write ( " " + cnt [ K ] ) ; }