First element of every K sets having consecutive elements with exactly K prime factors less than N | Java program for the above approach ; For storing smallest prime factor ; Function consmallest prime factor array ; Mark smallest prime factor for every number to be itself . ; Separately mark spf for every even number as 2 ; Check if i is prime ; Mark SPF for all numbers divisible by i ; Mark spf [ j ] if it is not previously marked ; Function for counts total number of prime factors ; Function to print elements of sets of K consecutive elements having K prime factors ; To store the result ; Count number of prime factors of number ; If number has exactly K factors push in result [ ] ; Iterate till we get K consecutive elements in result [ ] ; Count sequence until K ; Print the element if count >= K ; Driver code ; To construct spf [ ] ; Given N and K ; Function call
import java . util . * ; class GFG { static final int x = 2000021 ; static int [ ] v = new int [ x ] ; static void sieve ( ) { v [ 1 ] = 1 ; for ( int i = 2 ; i < x ; i ++ ) v [ i ] = i ; for ( int i = 4 ; i < x ; i += 2 ) v [ i ] = 2 ; for ( int i = 3 ; i * i < x ; i ++ ) { if ( v [ i ] == i ) { for ( int j = i * i ; j < x ; j += i ) { if ( v [ j ] == j ) { v [ j ] = i ; } } } } } static int prime_factors ( int n ) { HashSet < Integer > s = new HashSet < Integer > ( ) ; while ( n != 1 ) { s . add ( v [ n ] ) ; n = n / v [ n ] ; } return s . size ( ) ; } static void distinctPrimes ( int m , int k ) { Vector < Integer > result = new Vector < Integer > ( ) ; for ( int i = 14 ; i < m + k ; i ++ ) { long count = prime_factors ( i ) ; if ( count == k ) { result . add ( i ) ; } } int p = result . size ( ) ; for ( int index = 0 ; index < p - 1 ; index ++ ) { long element = result . get ( index ) ; int count = 1 , z = index ; while ( z < p - 1 && count <= k && result . get ( z ) + 1 == result . get ( z + 1 ) ) { count ++ ; z ++ ; } if ( count >= k ) System . out . print ( element + " ▁ " ) ; } } public static void main ( String [ ] args ) { sieve ( ) ; int N = 1000 , K = 3 ; distinctPrimes ( N , K ) ; } }