Check whether the given number is Euclid Number or not | Javascript program to check Euclid Number ; Function to generate prime numbers ; 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 all prime numbers to vector ' arr ' ; Function to check the number for Euclid Number ; Multiply next prime number and check if product + 1 = n holds or not ; Get the prime numbers ; Get n ; Check if n is Euclid Number ; Get n ; Check if n is Euclid Number
var MAX = 10000 ; var arr = [ ] ; function SieveOfEratosthenes ( ) { var prime = Array ( MAX ) . fill ( true ) ; ; for ( var p = 2 ; p * p < MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( var i = p * 2 ; i < MAX ; i += p ) prime [ i ] = false ; } } for ( var p = 2 ; p < MAX ; p ++ ) if ( prime [ p ] ) arr . push ( p ) ; } function isEuclid ( n ) { var product = 1 ; var i = 0 ; while ( product < n ) { product = product * arr [ i ] ; if ( product + 1 == n ) return true ; i ++ ; } return false ; } SieveOfEratosthenes ( ) ; var n = 31 ; if ( isEuclid ( n ) ) document . write ( " " ) ; else document . write ( " " ) ; n = 42 ; if ( isEuclid ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;