Count numbers from range whose prime factors are only 2 and 3 using Arrays | Set 2 | Java program to count the elements in the range [ L , R ] whose prime factors are only 2 and 3. ; Function which will calculate the elements in the given range ; Store the current power of 2 ; Store the current power of 3 ; power23 [ ] will store pairwise product of elements of power2 and power3 that are <= r ; Insert in power23 ] [ ] only if mul <= r ; Store the required answer ; Print the result ; Driver code
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 + "NEW_LINE"); } public static void main ( String [ ] args ) { int l = 1 , r = 10 ; calc_ans ( l , r ) ; } }