Count occurrences of a prime number in the prime factorization of every element from the given range | Function to return the count of times p appears in the prime factors of the elements from the range [ l , r ] ; To store the required count ; Number of values in the range [ 0 , r ] that are divisible by val ; Number of values in the range [ 0 , l - 1 ] that are divisible by val ; Increment the power of the val ; ( a - b ) is the count of numbers in the range [ l , r ] that are divisible by val ; No values that are divisible by val thus exiting from the loop ; Driver code
function getCount ( l , r , p ) { let cnt = 0 ; let val = p ; while ( 1 ) { let a = parseInt ( r / val ) ; let b = parseInt ( ( l - 1 ) / val ) ; val *= p ; if ( a - b ) { cnt += ( a - b ) ; } else break ; } return cnt ; } let l = 2 , r = 8 , p = 2 ; document . write ( getCount ( l , r , p ) ) ;