fork download
  1. class Permutations {
  2. static def list = []
  3. public static void printPermutation(char[] a, int startIndex, int endIndex) {
  4. if (startIndex == endIndex)
  5. list << ((new String(a)) as Integer)
  6. else {
  7. for (int x = startIndex; x < endIndex; x++) {
  8. swap(a, startIndex, x)
  9. printPermutation(a, startIndex + 1, endIndex)
  10. swap(a, startIndex, x)
  11. }
  12. }
  13. }
  14. private static void swap(char[] a, int i, int x) {
  15. char t = a[i]
  16. a[i] = a[x]
  17. a[x] = t
  18. }
  19.  
  20. }
  21. def n = 7876541
  22. def cArray = n.toString().toCharArray()
  23. Permutations.printPermutation(cArray, 0, cArray.size())
  24. println Permutations.list.findAll { it.mod(2) == 0}?.max()
Success #stdin #stdout 1.03s 4456448KB
stdin
Standard input is empty
stdout
8776514