 Count of integers of the form (2^x * 3^y) in the range [L, R]

Given a range [L, R] where 0 ≤ L ≤ R ≤ 108. The task is to find the count of integers from the given range that can be represented as (2x) * (3y).

Examples:

Input: L = 1, R = 10
Output: 7
The numbers are 1, 2, 3, 4, 6, 8 and 9

Input: L = 100, R = 200
Output: 5
The numbers are 108, 128, 144, 162 and 192

Approach: Since the numbers, which are powers of two and three, quickly grow, you can use the following algorithm. For all the numbers of the form (2x) * (3y) in the range [1, 108] store them in a vector. Later sort the vector. Then the required answer can be calculated using an upper bound. Pre-calculating these integers will be helpful when there are a number of queries of the form [L, R].

Below is the implementation of the above approach:

 #include using namespace std;    #define MAXI (int)(1e8)    vector v;    void precompute() {                  int x = 1, y = 1;                  while (x <= MAXI) {                     while (x * y <= MAXI) {                             v.push_back(x * y);                             y *= 3;         }                     x *= 2;                     y = 1;     }             sort(v.begin(), v.end()); }    void countNum(int l, int r) {     cout << upper_bound(v.begin(), v.end(), r)                 - upper_bound(v.begin(), v.end(), l - 1); }    int main() {     int l = 100, r = 200;             precompute();        countNum(l, r);        return 0; } If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.