Count numbers from range whose prime factors are only 2 and 3 using Arrays | Set 2
Last Updated :
23 May, 2022
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.
Examples:
Input: L = 1, R = 10
Output: 6
Explanation:
2 = 2
3 = 3
4 = 2 * 2
6 = 2 * 3
8 = 2 * 2 * 2
9 = 3 * 3
Input: L = 100, R = 200
Output: 5
For a simpler approach, refer to Count numbers from range whose prime factors are only 2 and 3.
Approach:
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:
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
void calc_ans(ll l, ll r)
{
vector<ll> power2, power3;
ll mul2 = 1;
while (mul2 <= r) {
power2.push_back(mul2);
mul2 *= 2;
}
ll mul3 = 1;
while (mul3 <= r) {
power3.push_back(mul3);
mul3 *= 3;
}
vector<ll> power23;
for ( int x = 0; x < power2.size(); x++) {
for ( int y = 0; y < power3.size(); y++) {
ll mul = power2[x] * power3[y];
if (mul == 1)
continue ;
if (mul <= r)
power23.push_back(mul);
}
}
ll ans = 0;
for (ll x : power23) {
if (x >= l && x <= r)
ans++;
}
cout << ans << endl;
}
int main()
{
ll l = 1, r = 10;
calc_ans(l, r);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void calc_ans( int l, int r)
{
Vector<Integer> power2 = new Vector<Integer>(),
power3 = new Vector<Integer>();
int mul2 = 1 ;
while (mul2 <= r)
{
power2.add(mul2);
mul2 *= 2 ;
}
int mul3 = 1 ;
while (mul3 <= r)
{
power3.add(mul3);
mul3 *= 3 ;
}
Vector<Integer> power23 = new Vector<Integer>();
for ( int x = 0 ; x < power2.size(); x++)
{
for ( int y = 0 ; y < power3.size(); y++)
{
int mul = power2.get(x) *
power3.get(y);
if (mul == 1 )
continue ;
if (mul <= r)
power23.add(mul);
}
}
int ans = 0 ;
for ( int x : power23)
{
if (x >= l && x <= r)
ans++;
}
System.out.print(ans + "\n" );
}
public static void main(String[] args)
{
int l = 1 , r = 10 ;
calc_ans(l, r);
}
}
|
Python3
def calc_ans(l, r):
power2 = []; power3 = [];
mul2 = 1 ;
while (mul2 < = r):
power2.append(mul2);
mul2 * = 2 ;
mul3 = 1 ;
while (mul3 < = r):
power3.append(mul3);
mul3 * = 3 ;
power23 = [];
for x in range ( len (power2)):
for y in range ( len (power3)):
mul = power2[x] * power3[y];
if (mul = = 1 ):
continue ;
if (mul < = r):
power23.append(mul);
ans = 0 ;
for x in power23:
if (x > = l and x < = r):
ans + = 1 ;
print (ans);
if __name__ = = "__main__" :
l = 1 ; r = 10 ;
calc_ans(l, r);
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void calc_ans( int l, int r)
{
List< int > power2 = new List< int >(),
power3 = new List< int >();
int mul2 = 1;
while (mul2 <= r)
{
power2.Add(mul2);
mul2 *= 2;
}
int mul3 = 1;
while (mul3 <= r)
{
power3.Add(mul3);
mul3 *= 3;
}
List< int > power23 = new List< int >();
for ( int x = 0; x < power2.Count; x++)
{
for ( int y = 0; y < power3.Count; y++)
{
int mul = power2[x] *
power3[y];
if (mul == 1)
continue ;
if (mul <= r)
power23.Add(mul);
}
}
int ans = 0;
foreach ( int x in power23)
{
if (x >= l && x <= r)
ans++;
}
Console.Write(ans + "\n" );
}
public static void Main(String[] args)
{
int l = 1, r = 10;
calc_ans(l, r);
}
}
|
Javascript
<script>
function calc_ans(l, r)
{
var power2 = [], power3 = [];
var mul2 = 1;
while (mul2 <= r) {
power2.push(mul2);
mul2 *= 2;
}
var mul3 = 1;
while (mul3 <= r) {
power3.push(mul3);
mul3 *= 3;
}
var power23 = [];
for ( var x = 0; x < power2.length; x++) {
for ( var y = 0; y < power3.length; y++) {
var mul = power2[x] * power3[y];
if (mul == 1)
continue ;
if (mul <= r)
power23.push(mul);
}
}
var ans = 0;
power23.forEach(x => {
if (x >= l && x <= r)
ans++;
});
document.write( ans );
}
var l = 1, r = 10;
calc_ans(l, r);
</script>
|
Time Complexity: O(log2(R) * log3(R)), as we are traversing in nested loops where we increment in multiple of 2 and 3.
Auxiliary Space: O(log2(R) * log3(R)), as we are using extra space.
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...