Count numbers from range whose prime factors are only 2 and 3 using Arrays | Set 2 | C # 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
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 + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int l = 1 , r = 10 ; calc_ans ( l , r ) ; } }