Prime with Prime

How many 2 digit prime numbers are there such that the first digit is a prime and the second digit is also a prime?

6 9 4 3

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

3 solutions

Ashwin Hebbar
Mar 28, 2015

The 4 one - digit prime no's are 2 , 3 , 5 and 7 . Arranging them to form 2 digit no's , the required 2 digit prime no's are 23 , 37 , 53 , 73 . Therefore , required answer is 4

Yes. It seems like many people thought that 1 is a prime, which resulted in an incorrect solution.

Chung Kevin - 6 years, 2 months ago

// Another Java Solution Code for the problem

public static boolean[] getPrimes(int range) {
    boolean[] bs = new boolean[range + 1];
    Arrays.fill(bs, true);
    bs[0] = bs[1] = false;
    for (int i = 2; i * i < bs.length; i++) {
        if (bs[i]) {
            for (int j = i * 2; j < bs.length; j += i) {
                bs[j] = false;
            }
        }
    }
    return bs;
}


// No throws clause here
public static void main(String[] args) throws java.io.IOException {

    BufferedReader reader = new BufferedReader(new InputStreamReader(
            System.in));
    int t = Integer.valueOf(reader.readLine());
    boolean[] bs = getPrimes(10000);
    while (t-- > 0) {
        String[] strings = reader.readLine().split(" ");
        int start = Integer.valueOf(strings[0]), end = Integer
                .valueOf(strings[1]);
        int counter = 0;
        for (int i = start; i <= end; i++) {
            if (bs[i] && bs[i % 10] && bs[i / 10]) {
                counter++;
            }
        }
        System.out.println(counter);
    }
Aryan Gaikwad
Mar 14, 2015

Java solution -

Actually this program could be much more shorter but I value efficiency more than shortness :)

public static void main(String[] args){
    for(int i = 10; i < 100; i++){
//loop from 10 to 100 (2 digits numbers)
        String t = String.valueOf(i); 
//convert the current number (in the loop) into a string
        if(prime(i)) 
//check if the number is prime
            if(prime(Integer.parseInt(t.substring(0,1))) && prime(Integer.parseInt(t.substring(1,2)))) 
//if the number is prime then split the number into two parts or digits and check if they are prime
                System.out.println(i); 
//if the two digits are prime, then print the number as the number itself is prime and it's digits are prime too.
    }
}

static boolean prime(int n){
 //check for prime numbers. Returns true if the number is prime and false if the number is composite or neither of the two
    if(n==2) return true; 
//if the number is 2, then return 2
    if(n==1) return false; 
//if it's 1, then return false
//this is because the loop below considers 1 and 2 prime

    if(n%2==0) return false; 
//if the number is divisible by 2 then return false (it's obviously not prime)
    for(int i=3;i*i<=n;i+=2) 
//then just loop through odd numbers
        if(n%i==0)
            return false;
    return true;
}

Otherwise, I could have done it with a external library for checking primes (which is not very efficient and not legible too) -

for(int i = 10; i < 100; i++)
if(prime(i) && prime(Integer.parseInt(String.valueOf(i).substring(0,1))) && prime(Integer.parseInt(String.valueOf(i).substring(1,2))))  System.out.println(i);

Efficiency of code is important. Can you include a brief description of what your code does?

Chung Kevin - 6 years, 3 months ago

Log in to reply

I added comments below lines to explain what they do

Aryan Gaikwad - 6 years, 3 months ago

Log in to reply

Great, thanks!

Chung Kevin - 6 years, 3 months ago

Log in to reply

@Chung Kevin You are welcome

Aryan Gaikwad - 6 years, 3 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...