K distant prime pairs in a given range | Function to generate prime numbers in the given range [ L , R ] ; Store all value in the range ; Erase 1 as its non - prime ; Perform Sieve of Eratosthenes ; Find current multiple ; Erase as it is a non - prime ; Increment multiple ; Function to print all the prime pairs in the given range that differs by K ; Generate all prime number ; Traverse the Map M ; If it . first & ( it . first + K ) is prime then print this pair ; Given range ; Given K ; Function Call
function findPrimeNos ( L , R , M ) { for ( var i = L ; i <= R ; i ++ ) { if ( M . has ( i ) ) M . set ( i , M . get ( i ) + 1 ) else M . set ( i , 1 ) } if ( M . has ( 1 ) ) { M . delete ( 1 ) ; } for ( var i = 2 ; i <= parseInt ( Math . sqrt ( R ) ) ; i ++ ) { var multiple = 2 ; while ( ( i * multiple ) <= R ) { if ( M . has ( i * multiple ) ) { M . delete ( i * multiple ) ; } multiple ++ ; } } return M ; } function getPrimePairs ( L , R , K ) { var M = new Map ( ) ; M = findPrimeNos ( L , R , M ) ; M . forEach ( ( value , key ) => { if ( M . has ( key + K ) ) { document . write ( " " + key + " " + ( key + K ) + " " ) ; } } ) ; } var L = 1 , R = 19 ; var K = 6 ; getPrimePairs ( L , R , K ) ;