Skip to content

Instantly share code, notes, and snippets.

@StefanKarpinski
Created May 27, 2015 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StefanKarpinski/43a31921d4b7c7d17898 to your computer and use it in GitHub Desktop.
Save StefanKarpinski/43a31921d4b7c7d17898 to your computer and use it in GitHub Desktop.
using Compat
function julia1(k)
for n = 10^(k-1):10^k-1
front = n*(10^k + n)
root = floor(Int,sqrt(front))
for t = root-2:root+2
back = t * (t - 1)
length(string(t)) > k && break
back > front && break
if back == front
println(STDERR,n,t)
break
end
end
end
end
function julia2(k)
for a = 10^(k-1):10^k-1
front = a * (10^k + a)
root = floor(front^0.5)
for b = root-1:root+1
back = b * (b - 1);
back > front && break
log(10,b) > k && continue
if front == back
@printf STDERR "%d%d\n" a b
# missing break?
end
end
end
end
function julia3(k)
for n = 10^(k-1):10^k-1
front = n*(10^k + n)
root = isqrt(front)
for t = root-2:root+2
back = t * (t - 1)
ndigits(t) > k && break
back > front && break
if back == front
println(STDERR,n,t)
break
end
end
end
end
digits = @compat parse(Int,ARGS[1])
digits > 0 && iseven(digits) || error("""
Number of digits must be even and non-zero! You said [$digits]
""")
k = div(digits,2)
for _ = 1:10
println("julia1,$digits,", @elapsed julia1(k))
println("julia2,$digits,", @elapsed julia2(k))
println("julia3,$digits,", @elapsed julia3(k))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment