Check whether the given number is Euclid Number or not | Javascript program to check Euclid Number ; Function to generate the Prime numbers and store their products ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; store prefix product of prime numbers to unordered_set ' s ' ; update product by multiplying next prime ; insert ' produc + 1' to set ; Function to check the number for Euclid Number ; Check if number exist in unordered set or not If exist , return true ; Get the prime numbers ; Get n ; Check if n is Euclid Number ; Get n ; Check if n is Euclid Number
let MAX = 10000 ; let s = new Set ( ) ; function SieveOfEratosthenes ( ) { let prime = new Array ( MAX ) ; for ( let i = 0 ; i < prime . length ; i ++ ) { prime [ i ] = true ; } prime [ 0 ] = false ; prime [ 1 ] = false ; for ( let p = 2 ; p * p < MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( let i = p * 2 ; i < MAX ; i += p ) prime [ i ] = false ; } } let product = 1 ; for ( let p = 2 ; p < MAX ; p ++ ) { if ( prime [ p ] ) { product = product * p ; s . add ( product + 1 ) ; } } } function isEuclid ( n ) { if ( s . has ( n ) ) return true ; else return false ; } SieveOfEratosthenes ( ) ; let n = 31 ; if ( isEuclid ( n ) ) document . write ( " " ) ; else document . write ( " " ) ; n = 42 ; if ( isEuclid ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;