Generating all the prime numbers from 2 to n .
function SieveofEratosthenes ( n ) { let visited = new Array ( n ) ; visited . fill ( false ) ; for ( let i = 2 ; i <= n + 1 ; i ++ ) if ( ! visited [ i ] ) { for ( let j = i * i ; j <= n + 1 ; j += i ) visited [ j ] = true ; primes . push ( i ) ; } } function specialPrimeNumbers ( n , k ) { SieveofEratosthenes ( n ) ; let count = 0 ; for ( let i = 0 ; i < primes . length ; i ++ ) { for ( let j = 0 ; j < i - 1 ; j ++ ) {