Given two positive integers L and R, the task is to count the elements from the range [L, R] whose prime factors are only 2 and 3.
Input: L = 1, R = 10
2 = 2
3 = 3
4 = 2 * 2
6 = 2 * 3
8 = 2 * 2 * 2
9 = 3 * 3
Input: L = 100, R = 200
For a simpler approach, refer to Count numbers from range whose prime factors are only 2 and 3.
To solve the problem in an optimized way, follow the steps given below:
- Store all the powers of 2 which are less than or equal to R in an array power2[ ].
- Similarly, store all the powers of 3 which are less than or equal to R in another array power3.
- Initialise third array power23 and store the pairwise product of each element of power2 with each element of power3 which are less than or equal to R.
- Now for any range [L, R], we will simply iterate over array power23 and count the numbers in the range [L, R].
Below is the implementation of above approach:
Time Complexity: O(log2(R) * log3(R))
Note: The approach can be further optimized. After storing powers of 2 and 3, the answer can be calculated using two pointers instead of generating all the numbers
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Count numbers from range whose prime factors are only 2 and 3
- Count all prime numbers in a given range whose sum of digits is also prime
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Check if a number exists having exactly N factors and K prime factors
- Find number of factors of N when location of its two factors whose product is N is given
- Maximum number of prime factors a number can have with exactly x factors
- Find and Count total factors of co-prime A or B in a given range 1 to N
- Count of nodes in a Binary Tree whose child is its prime factors
- Count of Nodes whose both immediate children are its prime factors
- Find the row whose product has maximum count of prime factors
- Super Ugly Number (Number whose prime factors are in given set)
- K-Primes (Numbers with k prime factors) in a range
- Sum of numbers in a range [L, R] whose count of divisors is prime
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Sum of element whose prime factors are present in array
- Find prime factors of Array elements whose sum of exponents is divisible by K
- Count common prime factors of two numbers
- Count of numbers having only 1 set bit in the range [0, n]
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.