Check if concatenation of first and last digits forms a prime number or not for each array element | Stores if i is prime ( 1 ) or non - prime ( 0 ) ; Function to build sieve array ; Inititalize all the values in sieve equals to 1 ; Sieve of Eratosthenes ; If current number is prime ; Set all multiples as non - prime ; Function to check if the numbers formed by combining first and last digits generates a prime number or not ; Check if any of the numbers formed is a prime number or not ; Traverse the array of queries ; Extract the last digit ; Extract the first digit ; If any of the two numbers is prime ; Otherwise ; Driver Code ; Computes and stores primes using Sieve ; Function call to perform queries
var sieve = Array . from ( { length : 105 } , ( _ , i ) => 0 ) ; function buildSieve ( ) { for ( i = 2 ; i < 100 ; i ++ ) sieve [ i ] = 1 ; for ( i = 2 ; i < 100 ; i ++ ) { if ( sieve [ i ] == 1 ) { for ( j = i * i ; j < 100 ; j += i ) sieve [ j ] = 0 ; } } } function isAnyPrime ( first , last ) { var num1 = first * 10 + last ; var num2 = last * 10 + first ; if ( sieve [ num1 ] == 1 sieve [ num2 ] == 1 ) return true ; else return false ; } function performQueries ( q ) { for ( i = 0 ; i < q . length ; i ++ ) { var A = q [ i ] ; var last = A % 10 ; var first ; while ( A >= 10 ) A = A / 10 ; first = A ; if ( isAnyPrime ( first , last ) ) document . write ( " " ) ; else document . write ( " " ) ; } } var q = [ 30 , 66 ] ; buildSieve ( ) ; performQueries ( q ) ;