human
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.
Print results for each query.
Driver Code"
Solution in Python:
assistant
#Here is code to queries to count Composite Magic Numbers from a given range [ L , R ] and the other steps required.
 def isMagic ( num ) :
	return ( num % 9 == 1 )
def isComposite ( n ) :
	if ( n <= 1 ) :
		return False
	if ( n <= 3 ) :
		return False
	if ( n % 2 == 0 or n % 3 == 0 ) :
		return True
	for i in range ( 5 , n + 1 , 6 ) :
		if i * i > n + 1 :
			break
		if ( n % i == 0 or n % ( i + 2 ) == 0 ) :
			return True
	return False
def find ( L , R , q ) :
	dp = [ 0 ] * 1000005
	dp [ 0 ] = 0
	dp [ 1 ] = 0
	for i in range ( 1 , 1000005 ) :
		if ( isComposite ( i ) and isMagic ( i ) ) :
			dp [ i ] = dp [ i - 1 ] + 1
		else :
			dp [ i ] = dp [ i - 1 ]
	for i in range ( q ) :
		print ( dp [ R [ i ] ] - dp [ L [ i ] - 1 ] )
if __name__ == ' _ _ main _ _ ' :
	L = [ 10 , 3 ]
	R = [ 100 , 2279 ]
	Q = 2
	find ( L , R , Q )