text: First element of every K sets having consecutive elements with exactly K prime factors less than N | Javascript program for the above approach ; For storing smallest prime factor ; Function construct smallest 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 ; To construct spf [ ] ; Given N and K ; Function Call
code: let x = 2000021 let v = new Array ( x ) ; function sieve ( ) { v [ 1 ] = 1 ; for ( let i = 2 ; i < x ; i ++ ) v [ i ] = i ; for ( let i = 4 ; i < x ; i += 2 ) v [ i ] = 2 ; for ( let i = 3 ; i * i < x ; i ++ ) { if ( v [ i ] == i ) { for ( let j = i * i ; j < x ; j += i ) { if ( v [ j ] == j ) { v [ j ] = i ; } } } } } function prime_factors ( n ) { let s = new Set ( ) ; while ( n != 1 ) { s . add ( v [ n ] ) ; n = n / v [ n ] ; } return s . size ; } function distinctPrimes ( m , k ) { let result = new Array ( ) ; for ( let i = 14 ; i < m + k ; i ++ ) { let count = prime_factors ( i ) ; if ( count == k ) { result . push ( i ) ; } } let p = result . length ; for ( let index = 0 ; index < p - 1 ; index ++ ) { let element = result [ index ] ; let count = 1 , z = index ; while ( z < p - 1 && count <= k && result [ z ] + 1 == result [ z + 1 ] ) { count ++ ; z ++ ; } if ( count >= k ) document . write ( element + ' ' ) ; } } sieve ( ) ; let N = 1000 , K = 3 ; distinctPrimes ( N , K ) ;