Find two distinct prime numbers with given product | Function to generate all prime numbers less than n ; Initialize all entries of bool array as true . A value in isPrime [ i ] will finally be false if i is Not a prime , else true isPrime [ n + 1 ] ; ; If isPrime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Function to print a prime pair with given product ; Generating primes using Sieve ; Traversing all numbers to find first pair ; Driver code
function SieveOfEratosthenes ( n , isPrime ) { isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( var i = 2 ; i <= n ; i ++ ) isPrime [ i ] = true ; for ( var p = 2 ; p * p <= n ; p ++ ) { if ( isPrime [ p ] == true ) { for ( var i = p * 2 ; i <= n ; i += p ) isPrime [ i ] = false ; } } } function findPrimePair ( n ) { var flag = 0 ; var isPrime = [ ] SieveOfEratosthenes ( n , isPrime ) ; for ( var i = 2 ; i < n ; i ++ ) { var x = n / i ; if ( isPrime [ i ] && isPrime [ x ] && x != i && x * i == n ) { document . write ( i + " " + x ) ; flag = 1 ; return ; } } if ( flag == 0 ) document . write ( " " ) ; } var n = 39 ; findPrimePair ( n ) ;