Find Largest Special Prime which is less than or equal to a given number | Function to check whether the number is a special prime or not ; While number is not equal to zero ; If the number is not prime return false . ; Else remove the last digit by dividing the number by 10. ; If the number has become zero then the number is special prime , hence return true ; Function to find the Largest Special Prime which is less than or equal to a given number ; Initially all numbers are considered Primes . ; There is always an answer possible ; Checking if the number is a special prime or not ; If yes print the number and break the loop . ; Else decrement the number . ; Driver code
function checkSpecialPrime ( sieve , num ) { while ( num != 0 ) { if ( ! sieve [ num ] ) { return false ; } num = Math . floor ( num / 10 ) ; } return true ; } function findSpecialPrime ( N ) { let sieve = new Array ( N + 10 ) ; sieve [ 0 ] = sieve [ 1 ] = false ; for ( let i = 0 ; i < N + 10 ; i ++ ) sieve [ i ] = true ; for ( let i = 2 ; i <= N ; i ++ ) { if ( sieve [ i ] ) { for ( let j = i * i ; j <= N ; j += i ) { sieve [ j ] = false ; } } } while ( true ) { if ( checkSpecialPrime ( sieve , N ) ) { document . write ( N + " " ) ; break ; } else N -- ; } } findSpecialPrime ( 379 ) ; findSpecialPrime ( 100 ) ;