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

15 Dec, 2021
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 9Input: 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: 
CPP
Python3
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; #define MAXI (int)(1e8) // To store all valid integersvector<int> v; // Function to find all the integers of the// form (2^x * 3^y) in the range [0, 1000000]void precompute(){     // To store powers of 2 and 3    // initialized to 2^0 and 3^0    int x = 1, y = 1;     // While current power of 2    // is within the range    while (x <= MAXI) {         // While number is within the range        while (x * y <= MAXI) {             // Add the number to the vector            v.push_back(x * y);             // Next power of 3            y *= 3;        }         // Next power of 2        x *= 2;         // Reset to 3^0        y = 1;    }     // Sort the vector    sort(v.begin(), v.end());} // Function to find the count of numbers// in the range [l, r] which are// of the form (2^x * 3^y)void countNum(int l, int r){    cout << upper_bound(v.begin(), v.end(), r)                - upper_bound(v.begin(), v.end(), l - 1);} // Driver codeint main(){    int l = 100, r = 200;     // Pre-compute all the valid numbers    precompute();     countNum(l, r);     return 0;}
# Python3 implementation of the approachimport bisectMAXI=int(1e8) # To store all valid integersv=[] # Function to find all the integers of the# form (2^x * 3^y) in the range [0, 1000000]def precompute():     # To store powers of 2 and 3    # initialized to 2^0 and 3^0    x = 1; y = 1     # While current power of 2    # is within the range    while (x <= MAXI) :         # While number is within the range        while (x * y <= MAXI) :             # Add the number to the vector            v.append(x * y)             # Next power of 3            y *= 3                  # Next power of 2        x *= 2         # Reset to 3^0        y = 1          # Sort the vector    v.sort()  # Function to find the count of numbers# in the range [l, r] which are# of the form (2^x * 3^y)def countNum(l, r):    print(bisect.bisect_right(v, r) - bisect.bisect_right(v,l - 1))  # Driver codeif __name__ == '__main__':    l = 100; r = 200     # Pre-compute all the valid numbers    precompute()     countNum(l, r)
5
 
Time Complexity: O(N * log(N)), where N = logX + logYAuxiliary Space: O(N)
pankajsharmagfg
amartyaghoshgfg
Binary Search
Algorithms
Competitive Programming
Mathematical
Sorting
Mathematical
Sorting
Binary Search
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
SDE SHEET - A Complete Guide for SDE Preparation
DSA Sheet by Love Babbar
Understanding Time Complexity with Simple Examples
Introduction to Algorithms
How to write a Pseudo Code?
Competitive Programming - A Complete Guide
Practice for cracking any coding interview
Arrow operator -> in C/C++ with Examples
Top 10 Algorithms and Data Structures for Competitive Programming
Modulo 10^9+7 (1000000007)