Problem:
We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime.
What is the largest n-digit pandigital prime that exists?
Solution:
7652413
Code:The solution may include methods that will be found here: Library.java .
public interface EulerSolution{ public String run(); }
/* * Solution to Project Euler problem 41 * By Nayuki Minase * * http://nayuki.eigenstate.org/page/project-euler-solutions * https://github.com/nayuki/Project-Euler-solutions */ public final class p041 implements EulerSolution { public static void main(String[] args) { System.out.println(new p041().run()); } public String run() { for (int n = 9; n >= 1; n--) { int[] digits = new int[n]; for (int i = 0; i < digits.length; i++) digits[i] = i + 1; int result = -1; do { if (Library.isPrime(toInteger(digits))) result = toInteger(digits); } while (Library.nextPermutation(digits)); if (result != -1) return Integer.toString(result); } throw new RuntimeException("Not found"); } private static int toInteger(int[] digits) { int result = 0; for (int x : digits) result = result * 10 + x; return result; } }
No comments :
Post a Comment