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
- Count numbers from range whose prime factors are only 2 and 3
- Count numbers in a range having GCD of powers of prime factors equal to 1
- K-Primes (Numbers with k prime factors) in a range
- Find and Count total factors of co-prime A or B in a given range 1 to N
- Count common prime factors of two numbers
- Count all prime numbers in a given range whose sum of digits is also prime
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Sum of numbers in a range [L, R] whose count of divisors is prime
- Numbers in range [L, R] such that the count of their divisors is both even and prime
- Count of Double Prime numbers in a given range L to R
- Count of natural numbers in range [L, R] which are relatively prime with N
- Queries for the difference between the count of composite and prime numbers in a given range
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Count of distinct sums that can be obtained by adding prime numbers from given arrays
- Common prime factors of two numbers
- Sum of all odd factors of numbers in the range [l, r]
- Sum of all even factors of numbers in the range [l, r]
- Find sum of exponents of prime factors of numbers 1 to N
- Count of Nodes whose both immediate children are its prime factors
- Count occurrences of a prime number in the prime factorization of every element from the given range
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.