Skip to content

Instantly share code, notes, and snippets.

@HelBorn
Created August 16, 2012 21:10
Show Gist options
  • Save HelBorn/3373693 to your computer and use it in GitHub Desktop.
Save HelBorn/3373693 to your computer and use it in GitHub Desktop.
final def KAPREKARS_CONSTANT = "6174"
final def MAX_ITERATIONS = 7
def startingDigits = "1123"
def iterations = 0
def solved = false
println "Attempting to solve ${startingDigits}:"
for(res = startingDigits; iterations < MAX_ITERATIONS && !solved; iterations++) {
res = res.toList().sort()
def a = res.reverse().join().toInteger()
def b = res.join().toInteger()
res = (a - b).toString().padLeft(4, '0')
println "${iterations+1}. ${a} - ${b} = ${res}"
solved = (res == KAPREKARS_CONSTANT)
}
println ""
if(solved)
println "Solved within ${iterations} iterations."
else
println "Unable to solve."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment