Friday, December 27, 2024
Google search engine
HomeData Modelling & AICount of integers of the form (2^x * 3^y) in the range...

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:
The numbers are 1, 2, 3, 4, 6, 8 and 9
Input: L = 100, R = 200 
Output:
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




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
#define MAXI (int)(1e8)
 
// To store all valid integers
vector<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 code
int main()
{
    int l = 100, r = 200;
 
    // Pre-compute all the valid numbers
    precompute();
 
    countNum(l, r);
 
    return 0;
}


Java




// Java implementation of the approach
import java.util.*;
 
class GFG {
  static int MAXI = 100000000;
 
  // To store all valid integers
  static ArrayList<Integer> v = new ArrayList<Integer>();
 
  static int upper_bound(ArrayList<Integer> arr, int elem)
  {
    for (var i = 0; i < arr.size(); i++)
      if (elem < arr.get(i))
        return i;
    return arr.size();
  }
  // Function to find all the integers of the
  // form (2^x * 3^y) in the range [0, 1000000]
  static 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.add(x * y);
 
        // Next power of 3
        y *= 3;
      }
 
      // Next power of 2
      x *= 2;
 
      // Reset to 3^0
      y = 1;
    }
 
    // Sort the vector
    Collections.sort(v);
  }
 
  // Function to find the count of numbers
  // in the range [l, r] which are
  // of the form (2^x * 3^y)
  static void countNum(int l, int r)
  {
    System.out.println(upper_bound(v, r)
                       - upper_bound(v, l - 1));
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int l = 100, r = 200;
 
    // Pre-compute all the valid numbers
    precompute();
 
    countNum(l, r);
  }
}
 
// This code is contributed by phasing17


Python3




# Python3 implementation of the approach
import bisect
MAXI=int(1e8)
 
# To store all valid integers
v=[]
 
# 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 code
if __name__ == '__main__':
    l = 100; r = 200
 
    # Pre-compute all the valid numbers
    precompute()
 
    countNum(l, r)


C#




// C# implementation of the approach
using System;
using System.Collections.Generic;
 
class GFG
{
    static int MAXI = 100000000;
     
    // To store all valid integers
    static List<int> v = new List<int>();
     
    static int upper_bound(List<int> arr, int elem)
    {
        for (var i = 0; i < arr.Count; i++)
            if (elem < arr[i])
                return i;
        return arr.Count;
    }
    // Function to find all the integers of the
    // form (2^x * 3^y) in the range [0, 1000000]
    static 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.Add(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)
    static void countNum(int l, int r)
    {
        Console.WriteLine(upper_bound(v, r)
                    - upper_bound(v, l - 1));
    }
     
    // Driver code
    public static void Main(string[] args)
    {
        int l = 100, r = 200;
     
        // Pre-compute all the valid numbers
        precompute();
     
        countNum(l, r);
     
    }
}
 
// This code is contributed by phasing17


Javascript




// JavaScript implementation of the approach
let MAXI = 100000000
 
// To store all valid integers
let v = [];
 
function upper_bound(arr, elem)
{
    for (var i = 0; i < arr.length; i++)
        if (elem < arr[i])
            return i
    return arr.length;
}
 
// Function to find all the integers of the
// form (2^x * 3^y) in the range [0, 1000000]
function precompute()
{
 
    // To store powers of 2 and 3
    // initialized to 2^0 and 3^0
    let 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(parseInt(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(a, b) {
  return a - b;
});
}
 
// Function to find the count of numbers
// in the range [l, r] which are
// of the form (2^x * 3^y)
function countNum(l, r)
{
    console.log(upper_bound(v, r) - upper_bound(v, l - 1));
}
 
// Driver code
let l = 100, r = 200;
 
// Pre-compute all the valid numbers
precompute();
 
countNum(l, r);
 
// This code is contributed by phasing17


Output: 

5

 

Time Complexity: O(N * log(N)), where N = logX + logY
Auxiliary Space: O(N), as N extra space has been taken.

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments