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
sieve = [ 0 for i in range ( 105 ) ] NEW_LINE def buildSieve ( ) : NEW_LINE INDENT global sieve NEW_LINE for i in range ( 2 , 100 ) : NEW_LINE INDENT sieve [ i ] = 1 NEW_LINE DEDENT for i in range ( 2 , 100 ) : NEW_LINE INDENT if ( sieve [ i ] == 1 ) : NEW_LINE INDENT for j in range ( i * i , 100 , i ) : NEW_LINE INDENT sieve [ j ] = 0 NEW_LINE DEDENT DEDENT DEDENT DEDENT def isAnyPrime ( first , last ) : NEW_LINE INDENT global sieve NEW_LINE num1 = first * 10 + last NEW_LINE num2 = last * 10 + first NEW_LINE if ( sieve [ num1 ] == 1 or sieve [ num2 ] == 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def performQueries ( q ) : NEW_LINE INDENT for i in range ( len ( q ) ) : NEW_LINE INDENT A = q [ i ] NEW_LINE last = A % 10 NEW_LINE first = 0 NEW_LINE while ( A >= 10 ) : NEW_LINE INDENT A = A // 10 NEW_LINE DEDENT first = A NEW_LINE if ( isAnyPrime ( first , last ) ) : NEW_LINE INDENT print ( " True " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " False " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT q = [ 30 , 66 ] NEW_LINE buildSieve ( ) NEW_LINE performQueries ( q ) NEW_LINE DEDENT