Queries to count Composite Magic Numbers from a given range [ L , R ] | Check if a number is magic number or not ; Check number is composite number or not ; Corner cases ; Check if the number is a multiple of 2 or 3 ; Check for multiples of remaining primes ; Function to find Composite Magic Numbers in given ranges ; dp [ i ] : Stores the count of composite Magic numbers up to i ; Traverse in the range [ 1 , 1e5 ) ; Check if number is Composite number as well as a Magic number or not ; Prlet results for each query ; Driver code
function isMagic ( num ) { return ( num % 9 == 1 ) ; } function isComposite ( n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return false ; if ( n % 2 == 0 n % 3 == 0 ) return true ; for ( let i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return true ; return false ; } function find ( L , R , q ) { let dp = [ ] ; dp [ 0 ] = 0 ; dp [ 1 ] = 0 ; for ( let i = 1 ; i < 1000005 ; i ++ ) { if ( isComposite ( i ) && isMagic ( i ) == true ) { dp [ i ] = dp [ i - 1 ] + 1 ; } else dp [ i ] = dp [ i - 1 ] ; } for ( let i = 0 ; i < q ; i ++ ) document . write ( dp [ R [ i ] ] - dp [ L [ i ] - 1 ] + " " ) ; } let L = [ 10 , 3 ] ; let R = [ 100 , 2279 ] ; let Q = 2 ; find ( L , R , Q ) ;