|
| 1 | +package medium; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | + |
| 5 | +/** |
| 6 | + * Have the function PrimeChecker(num) take num and return 1 |
| 7 | + * if any arrangement of num comes out to be a prime number, otherwise return 0. |
| 8 | + * For example: if num is 910, the output should be 1 |
| 9 | + * because 910 can be arranged into 109 or 019, both of which are primes |
| 10 | + */ |
| 11 | +public class PrimeChecker { |
| 12 | + |
| 13 | + /** |
| 14 | + * Collection of permutations. |
| 15 | + * E.g. [5, 9, 1, 2, 1, 5] -> 591521, 591512, 591251, 591215, ... |
| 16 | + */ |
| 17 | + private static final ArrayList<Integer> permutations = new ArrayList<>(); |
| 18 | + |
| 19 | + /** |
| 20 | + * Check if a given number is a prime number. |
| 21 | + * |
| 22 | + * @param num input number |
| 23 | + * @return true if is a prime number |
| 24 | + */ |
| 25 | + private static boolean isPrime(int num) { |
| 26 | + // prime numbers are greater than 1 |
| 27 | + if (num <= 1) { |
| 28 | + return false; |
| 29 | + } |
| 30 | + // 2 is an only even prime number |
| 31 | + // http://mathworld.wolfram.com/EvenPrime.html |
| 32 | + if (num == 2) { |
| 33 | + return true; |
| 34 | + } |
| 35 | + // eliminate all even numbers to reduce time complexity |
| 36 | + // (cannot be a prime number if is divisible by 2) |
| 37 | + if (num % 2 == 0) { |
| 38 | + return false; |
| 39 | + } |
| 40 | + // no need to check the entire range from 0 to num |
| 41 | + // (square root of num) + 1 is the sufficient upper limit |
| 42 | + // which greatly reduces time complexity |
| 43 | + double upper = Math.sqrt(num) + 1; |
| 44 | + // since the even numbers are eliminated, we can check the odd numbers only |
| 45 | + for (int i = 3; i < upper; i += 2) { |
| 46 | + if (num % i == 0) { |
| 47 | + // not a prime number |
| 48 | + return false; |
| 49 | + } |
| 50 | + } |
| 51 | + // must be a prime number! |
| 52 | + return true; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Swap two elements in array. |
| 57 | + * |
| 58 | + * @param coll array of characters |
| 59 | + * @param i number 1 |
| 60 | + * @param j number 2 |
| 61 | + */ |
| 62 | + private static void swap(char[] coll, int i, int j) { |
| 63 | + char temp = coll[i]; |
| 64 | + coll[i] = coll[j]; |
| 65 | + coll[j] = temp; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Produce permutations of elements in a given array. |
| 70 | + * |
| 71 | + * @param coll array of characters |
| 72 | + * @param index start index |
| 73 | + */ |
| 74 | + private static void getPermutations(char[] coll, int index) { |
| 75 | + if (index == coll.length - 1) { |
| 76 | + permutations.add(Integer.parseInt(String.valueOf(coll))); |
| 77 | + } |
| 78 | + for (int i = index; i < coll.length; i++) { |
| 79 | + swap(coll, index, i); |
| 80 | + getPermutations(coll, index + 1); |
| 81 | + swap(coll, index, i); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Prime Checker function. |
| 87 | + * |
| 88 | + * @param num input number |
| 89 | + * @return 1 if permutations include a prime number |
| 90 | + */ |
| 91 | + private static int primeChecker(int num) { |
| 92 | + getPermutations(String.valueOf(num).toCharArray(), 0); |
| 93 | + for (int i : permutations) { |
| 94 | + if (isPrime(i)) { |
| 95 | + // prime number is found in permutation |
| 96 | + return 1; |
| 97 | + } |
| 98 | + } |
| 99 | + // permutations do not include a prime number |
| 100 | + return 0; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Entry point. |
| 105 | + * |
| 106 | + * @param args command line arguments |
| 107 | + */ |
| 108 | + public static void main(String[] args) { |
| 109 | + int result1 = primeChecker(591521); |
| 110 | + System.out.println(result1); |
| 111 | + int result2 = primeChecker(910); |
| 112 | + System.out.println(result2); |
| 113 | + } |
| 114 | + |
| 115 | +} |
0 commit comments