Coding Problem - Prime Path

The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices. * It is a matter of security to change such things every now and then, to keep the enemy in the dark. * But look, I have chosen my number 1033 for good reasons. I am the Prime minister, you know! * I know, so therefore your new number 8179 is also a prime. You will just have to paste four new digits over the four old ones on your office door. * No, it’s not that simple. Suppose that I change the first digit to an 8, then the number will read 8033 which is not a prime! * I see, being the prime minister you cannot stand having a non-prime number on your door even for a few seconds. * Correct! So I must invent a scheme for going from 1033 to 8179 by a path of prime numbers where only one digit is changed from one prime to the next prime.

Now, the minister of finance, who had been eavesdropping, intervened.

  • No unnecessary expenditure, please! I happen to know that the price of a digit is one pound.
  • Hmm, in that case I need a computer program to minimize the cost. You don’t know some very cheap software gurus, do you?
  • In fact, I do. You see, there is this programming contest going on…

Help the prime minister to find the cheapest prime path between any two given four-digit primes! The first digit must be nonzero, of course. Here is a solution in the case above.

1033
1733     
3733     
3739     
3779
8779
8179     

The cost of this solution is 6 pounds. Note that the digit 1 which got pasted over in step 2 can not be reused in the last step – a new 1 must be purchased.

Input

One line with a positive number: the number of test cases (at most 100). Then for each test case, one line with two numbers separated by a blank. Both numbers are four-digit primes (without leading zeros).

3
1033 8179
1373 8017
1033 1033

Output

One line for each case, either with a number stating the minimal cost or containing the word “Impossible”.

6
7
0

Solution

Lets see how would we do the algorithm for this, first before we proceed we need a way to check if the number is prime or not, so we will need a list of prime numbers up to 10000.

To do this we can use Sieve of Eratosthenes, Trial division, or some other method to generate a list of prime numbers.

This is a graph problem, so we can use Breadth-first search or Dijkstra’s algorithm algorithms to find the shortest path between the number that we want.

Now let’s see a working implementation of this in Java

Java
import java.io.BufferedInputStream;
import java.util.LinkedList;
import java.util.Scanner;

class PrimePath {

    static boolean[] isPrime = getPrimes(10000);

    public static boolean[] getPrimes(int n) {
        int i, j, k, x;
        boolean[] a = new boolean[n];
        n++;
        n /= 2;
        int[] b = new int[(n + 1) * 2];
        a[2] = true;
        a[3] = true;
        for (i = 1; i <= 2 * n; i++) {
            b[i] = 0;
        }
        for (i = 3; i <= n; i += 3) {
            for (j = 0; j < 2; j++) {
                x = 2 * (i + j) - 1;
                while (b[x] == 0) {
                    a[x] = true;
                    for (k = x; k <= 2 * n; k += x) {
                        b[k] = 1;
                    }
                }
            }
        }
        return a;
    }

    public static void main(String[] args) {

        Scanner scan = new Scanner(new BufferedInputStream(System.in));
        int cas = scan.nextInt();
        for (int i = 1; i <= cas; i++) {
            int start = scan.nextInt();
            int end = scan.nextInt();
            boolean[] isVisited = new boolean[10000];
            int[] step = new int[10000];
            LinkedList<Integer> queue = new LinkedList();
            queue.addLast(start);
            isVisited[start] = true;
            while (!queue.isEmpty()) {
                int current = queue.pop();
                if (current == end) {
                    break;
                }
                for (int j = 0; j <= 9; j++) {
                    int next1 = getNext(1, j, current);
                    int next2 = getNext(2, j, current);
                    int next3 = getNext(3, j, current);
                    int next4 = getNext(4, j, current);
                    if (!isVisited[next1]) {
                        queue.addLast(next1);
                        step[next1] = step[current] + 1;
                        isVisited[next1] = true;
                    }
                    if (!isVisited[next2]) {
                        queue.addLast(next2);
                        step[next2] = step[current] + 1;
                        isVisited[next2] = true;
                    }
                    if (!isVisited[next3]) {
                        queue.addLast(next3);
                        step[next3] = step[current] + 1;
                        isVisited[next3] = true;
                    }
                    if (!isVisited[next4]) {
                        queue.addLast(next4);
                        step[next4] = step[current] + 1;
                        isVisited[next4] = true;
                    }
                }
            }
            System.out.println(step[end]);
        }
    }

    public static int getNext(int flag, int i, int current) {
        int next = 0;
        if (flag == 1) {
            if (i == 0) {
                return current;
            }
            next = current % 1000 + i * 1000;
        }
        if (flag == 2) {
            int t = current / 1000;
            next = t * 1000 + current % 1000 % 100 + i * 100;
        }
        if (flag == 3) {
            int t = current / 100;
            int tt = current % 10;
            next = t * 100 + i * 10 + tt;
        }
        if (flag == 4) {
            next = current / 10 * 10 + i;

        }
        if (!isPrime[next]) {
            return current;
        }
        return next;
    }

}