package margusmartseppcode.From_40_to_49; import java.util.Arrays; public class Problem_49 { // Call only if: (n>0&&(i%2==0||i%3==0||i%5==0||i%7==0)) static boolean isPrimeS(final int n) { final int r = (int) Math.floor(Math.sqrt(n)); for (int f = 5; f <= r; f += 6) if (n % f == 0 || n % (f + 2) == 0) return false; return true; } static boolean isPerm(char[] a, char[] b) { if (a.length != b.length) return false; Arrays.sort(a); Arrays.sort(b); return Arrays.equals(a, b); } static boolean arePerms(int... n) { if (n.length == 0) return true; for (int i = 1; i < n.length; i++) if (!isPerm(("" + n[i - 1]).toCharArray(), ("" + n[i]) .toCharArray())) return false; return true; } static boolean arePrimes(int... n) { for (int i : n) if (!isPrimeS(i)) return false; return true; } public static void main(String[] args) { int a, b, c; a = b = c = 0; for (a = 1489;; a += 2) { if (a % 3 == 0 || a % 5 == 0 || a % 7 == 0) continue; b = a + 3330; c = a + 6660; if (arePrimes(a, b, c)) if (arePerms(a, b, c)) break; } System.out.println("" + a + b + c); } }
Sunday, September 13, 2009
Euler Problem 49 solution
Time (s): ~0.002
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment