Brilliant Numbers | Function to generate all prime numbers less than n ; Initialize all entries of let array as true . A value in isPrime [ i ] will finally be false if i is Not a prime ; If isPrime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Function to return the number of digits in a number ; Function to check if N is a Brilliant number ; Generating primes using Sieve ; Traversing all numbers to find first pair ; Given Number ; Function Call
function SieveOfEratosthenes ( n , isPrime ) { isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( let i = 2 ; i <= n ; i ++ ) isPrime [ i ] = true ; for ( let p = 2 ; p * p <= n ; p ++ ) { if ( isPrime [ p ] == true ) { for ( let i = p * 2 ; i <= n ; i += p ) isPrime [ i ] = false ; } } } function countDigit ( n ) { let count = 0 ; while ( n != 0 ) { n = parseInt ( n / 10 ) ; ++ count ; } return count ; } function isBrilliant ( n ) { let flag = 0 ; let isPrime = Array ( n + 1 ) . fill ( true ) ; SieveOfEratosthenes ( n , isPrime ) ; for ( let i = 2 ; i < n ; i ++ ) { let x = n / i ; if ( isPrime [ i ] && isPrime [ x ] && ( x * i ) == n ) { if ( countDigit ( i ) == countDigit ( x ) ) return true ; } } return false ; } let n = 1711 ; if ( isBrilliant ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;