package margusmartseppcode.From_30_to_39; import java.util.HashSet; import java.util.Set; public class Problem_38 { static final int nbrs = 9; static boolean isPandigital(String s) { return isPandigital(s, nbrs); } static boolean isPandigital(String s, int nr) { if (s.length() != nr) return false; Set<Character> set = new HashSet<Character>(); for (int i = 0; i < nr; i++) set.add(s.charAt(i)); if (set.size() != nr) return false; if (set.contains('0')) return false; return true; } public static void main(String[] args) { String maxPan = ""; for (int i = 9876; i > 9123; i--) if (isPandigital(maxPan = "" + i + (i + i))) break; System.out.println(maxPan); } }
Wednesday, September 9, 2009
Euler Problem 38 solution
Time (s): ~0.012
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment