If prime is a factor of n then increment count
if ( n % prime [ i ] == 0 ) { a [ j ] = prime [ i ] ; j ++ ; } while ( n % prime [ i ] == 0 ) n = Math . floor ( n / prime [ i ] ) ; i ++ ; } if ( n != 1 ) { a [ j ] = n ; j ++ ; } return m - count ( a , j , m ) ; } function countRelPrimeInRange ( n , l , r ) { sieve ( Math . floor ( Math . sqrt ( maxN ) ) ) ; let result = countRelPrime ( n , r ) - countRelPrime ( n , l - 1 ) ; document . write ( result ) ; }