text: K distant prime pairs in a given range | C # program for the above approach ; 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 ; Traverse the Map M ; If it . first & ( it . first + K ) is prime then print this pair ; Function to print all the prime pairs in the given range that differs by K ; Generate all prime number ; Driver Code ; Given range ; Given K ; Function Call
code: using System ; using System . Collections . Generic ; class solution { static void findPrimeNos ( int L , int R , Dictionary < int , int > M , int K ) { for ( int i = L ; i <= R ; i ++ ) { if ( M . ContainsKey ( i ) ) M . Add ( i , M [ i ] + 1 ) ; else M . Add ( i , 1 ) ; } if ( M [ 1 ] != 0 ) { M . Remove ( 1 ) ; } for ( int i = 2 ; i <= Math . Sqrt ( R ) ; i ++ ) { int multiple = 2 ; while ( ( i * multiple ) <= R ) { if ( M . ContainsKey ( i * multiple ) ) { M . Remove ( i * multiple ) ; } multiple ++ ; } } foreach ( KeyValuePair < int , int > entry in M ) { if ( M . ContainsKey ( entry . Key + K ) ) { Console . Write ( " ( " + entry . Key + " , ▁ " + ( entry . Key + K ) + " ) ▁ " ) ; } } } static void getPrimePairs ( int L , int R , int K ) { Dictionary < int , int > M = new Dictionary < int , int > ( ) ; findPrimeNos ( L , R , M , K ) ; } public static void Main ( String [ ] args ) { int L = 1 , R = 19 ; int K = 6 ; getPrimePairs ( L , R , K ) ; } }