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 = 10Output:6

2 = 2

3 = 3

4 = 2 * 2

6 = 2 * 3

8 = 2 * 2 * 2

9 = 3 * 3

Input:L = 100, R = 200Output:5

**Approach:** Start a loop from **L** to **R** and for every element **num**:

- While
**num**is divisible by**2**, divide it by**2**. - While
**num**is divisible by**3**, divide it by**3**. - If
**num = 1**then increment the**count**as**num**has only**2**and**3**as its prime factors.

Print the **count** in the end.

Below is the implementation of the above approach:

`// C++ program to count the numbers within a range ` `// whose prime factors are only 2 and 3 ` `#include <bits/stdc++.h> ` `using` `namespace` `std; `
` ` `// Function to count the number within a range ` `// whose prime factors are only 2 and 3 ` `int` `findTwoThreePrime(` `int` `l, ` `int` `r) `
`{ ` ` ` `// Start with 2 so that 1 doesn't get counted `
` ` `if` `(l == 1) `
` ` `l++; `
` ` ` ` `int` `count = 0; `
` ` ` ` `for` `(` `int` `i = l; i <= r; i++) { `
` ` `int` `num = i; `
` ` ` ` `// While num is divisible by 2, divide it by 2 `
` ` `while` `(num % 2 == 0) `
` ` `num /= 2; `
` ` ` ` `// While num is divisible by 3, divide it by 3 `
` ` `while` `(num % 3 == 0) `
` ` `num /= 3; `
` ` ` ` `// If num got reduced to 1 then it has `
` ` `// only 2 and 3 as prime factors `
` ` `if` `(num == 1) `
` ` `count++; `
` ` `} `
` ` ` ` `return` `count; `
`} ` ` ` `// Driver code ` `int` `main() `
`{ ` ` ` `int` `l = 1, r = 10; `
` ` `cout << findTwoThreePrime(l, r); `
` ` `return` `0; `
`} ` |

*chevron_right*

*filter_none*

`//Java program to count the numbers within a range ` `// whose prime factors are only 2 and 3 ` ` ` `import` `java.io.*; `
` ` `class` `GFG { `
` ` `// Function to count the number within a range ` `// whose prime factors are only 2 and 3 ` `static` `int` `findTwoThreePrime(` `int` `l, ` `int` `r) `
`{ ` ` ` `// Start with 2 so that 1 doesn't get counted `
` ` `if` `(l == ` `1` `) `
` ` `l++; `
` ` ` ` `int` `count = ` `0` `; `
` ` ` ` `for` `(` `int` `i = l; i <= r; i++) { `
` ` `int` `num = i; `
` ` ` ` `// While num is divisible by 2, divide it by 2 `
` ` `while` `(num % ` `2` `== ` `0` `) `
` ` `num /= ` `2` `; `
` ` ` ` `// While num is divisible by 3, divide it by 3 `
` ` `while` `(num % ` `3` `== ` `0` `) `
` ` `num /= ` `3` `; `
` ` ` ` `// If num got reduced to 1 then it has `
` ` `// only 2 and 3 as prime factors `
` ` `if` `(num == ` `1` `) `
` ` `count++; `
` ` `} `
` ` ` ` `return` `count; `
`} ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) { `
` ` ` ` `int` `l = ` `1` `, r = ` `10` `; `
` ` `System.out.println (findTwoThreePrime(l, r)); `
` ` `} `
`//This code is contributed by ajit ` `} ` |

*chevron_right*

*filter_none*

`# Python3 program to count the numbers ` `# within a range whose prime factors ` `# are only 2 and 3 ` ` ` `# Function to count the number within ` `# a range whose prime factors are only ` `# 2 and 3 ` `def` `findTwoThreePrime(l, r) : `
` ` ` ` `# Start with 2 so that 1 `
` ` `# doesn't get counted `
` ` `if` `(l ` `=` `=` `1` `) : `
` ` `l ` `+` `=` `1`
` ` ` ` `count ` `=` `0`
` ` ` ` `for` `i ` `in` `range` `(l, r ` `+` `1` `) : `
` ` `num ` `=` `i `
` ` ` ` `# While num is divisible by 2, `
` ` `# divide it by 2 `
` ` `while` `(num ` `%` `2` `=` `=` `0` `) : `
` ` `num ` `/` `/` `=` `2` `; `
` ` ` ` `# While num is divisible by 3, `
` ` `# divide it by 3 `
` ` `while` `(num ` `%` `3` `=` `=` `0` `) : `
` ` `num ` `/` `/` `=` `3`
` ` ` ` `# If num got reduced to 1 then it has `
` ` `# only 2 and 3 as prime factors `
` ` `if` `(num ` `=` `=` `1` `) : `
` ` `count ` `+` `=` `1`
` ` ` ` `return` `count `
` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: `
` ` ` ` `l ` `=` `1`
` ` `r ` `=` `10`
` ` ` ` `print` `(findTwoThreePrime(l, r)) `
` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

`// C# program to count the numbers ` `// within a range whose prime factors ` `// are only 2 and 3 ` `using` `System; `
` ` `class` `GFG `
`{ ` ` ` `// Function to count the number ` `// within a range whose prime ` `// factors are only 2 and 3 ` `static` `int` `findTwoThreePrime(` `int` `l, ` `int` `r) `
`{ ` ` ` `// Start with 2 so that 1 `
` ` `// doesn't get counted `
` ` `if` `(l == 1) `
` ` `l++; `
` ` ` ` `int` `count = 0; `
` ` ` ` `for` `(` `int` `i = l; i <= r; i++) `
` ` `{ `
` ` `int` `num = i; `
` ` ` ` `// While num is divisible by 2, `
` ` `// divide it by 2 `
` ` `while` `(num % 2 == 0) `
` ` `num /= 2; `
` ` ` ` `// While num is divisible by 3, `
` ` `// divide it by 3 `
` ` `while` `(num % 3 == 0) `
` ` `num /= 3; `
` ` ` ` `// If num got reduced to 1 then it `
` ` `// has only 2 and 3 as prime factors `
` ` `if` `(num == 1) `
` ` `count++; `
` ` `} `
` ` `return` `count; `
`} ` ` ` `// Driver code ` `static` `public` `void` `Main () `
`{ ` ` ` `int` `l = 1, r = 10; `
` ` `Console.WriteLine(findTwoThreePrime(l, r)); `
`} ` `} ` ` ` `// This code is contributed by akt_mit ` |

*chevron_right*

*filter_none*

`<?php ` `// PHP program to count the numbers ` `// within a range whose prime factors ` `// are only 2 and 3 ` ` ` `// Function to count the number ` `// within a range whose prime ` `// factors are only 2 and 3 ` `function` `findTwoThreePrime(` `$l` `, ` `$r` `) `
`{ ` ` ` `// Start with 2 so that 1 `
` ` `// doesn't get counted `
` ` `if` `(` `$l` `== 1) `
` ` `$l` `++; `
` ` ` ` `$count` `= 0; `
` ` ` ` `for` `(` `$i` `= ` `$l` `; ` `$i` `<= ` `$r` `; ` `$i` `++) `
` ` `{ `
` ` `$num` `= ` `$i` `; `
` ` ` ` `// While num is divisible by 2, `
` ` `// divide it by 2 `
` ` `while` `(` `$num` `% 2 == 0) `
` ` `$num` `/= 2; `
` ` ` ` `// While num is divisible by 3, `
` ` `// divide it by 3 `
` ` `while` `(` `$num` `% 3 == 0) `
` ` `$num` `/= 3; `
` ` ` ` `// If num got reduced to 1 then it has `
` ` `// only 2 and 3 as prime factors `
` ` `if` `(` `$num` `== 1) `
` ` `$count` `++; `
` ` `} `
` ` `return` `$count` `; `
`} ` ` ` `// Driver code ` `$l` `= 1; `
`$r` `= 10; `
`echo` `findTwoThreePrime(` `$l` `, ` `$r` `); `
` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

6

## Recommended Posts:

- 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
- Count common prime factors of two numbers
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Numbers in range [L, R] such that the count of their divisors is both even and prime
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Queries for the difference between the count of composite and prime numbers in a given range
- Common prime factors of two numbers
- Sum of all even factors of numbers in the range [l, r]
- Sum of all odd factors of numbers in the range [l, r]
- Sort an array according to the increasing count of distinct Prime Factors
- Number of distinct prime factors of first n natural numbers
- Number which has the maximum number of distinct prime factors in the range M to N
- Sum of all the prime numbers in a given range
- Prime numbers in a given range using STL | Set 2

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.

**Practice Tags :**