Skip to content

Instantly share code, notes, and snippets.

@Riduidel
Created February 9, 2011 18:03
Show Gist options
  • Select an option

  • Save Riduidel/818913 to your computer and use it in GitHub Desktop.

Select an option

Save Riduidel/818913 to your computer and use it in GitHub Desktop.
// An offset defining a kind of header we don't want to see as it pollutes computations
// Yup, it's a hack
def OFFSET = 1000
def SCANNED_SIZE = 10000
def UPSCALE = SCANNED_SIZE+OFFSET
def MAX = 1000
long start = System.currentTimeMillis();
// detector regexp pattern
def detector = ~/([0-9]*)\1+.*/
def initial = 1g
initial = initial.toBigDecimal()
def scale = initial.scale()+UPSCALE
def matchings = [:]
for(n in 1..MAX) {
if(n%2!=0) {
if((n+1)%100==0) {
println "scanning $n"
}
def decimals = initial.divide(n, scale, java.math.RoundingMode.HALF_EVEN)
def decimalsStr = "$decimals"
def initialDecimals = decimalsStr = decimalsStr.substring(OFFSET)
// fight pattern and string and as long as it matches, repeat operation
def matcher = detector.matcher(decimalsStr)
for(; matcher.matches() && matcher[0][1].size()>0 && decimalsStr.size()>1; matcher =detector.matcher(decimalsStr)) {
decimalsStr = matcher[0][1]
}
matchings[n]=decimalsStr
}
}
greatest = matchings.max {a, b -> a.value.size() <=> b.value.size() }
println "${greatest.key} has cycle of ${greatest.value.size()} length"
long end = System.currentTimeMillis();
println "duration "+((end-start)/1000.0)+" s";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment