Skip to content

Instantly share code, notes, and snippets.

@jonatasemidio
Last active June 27, 2016 18:54
Show Gist options
  • Select an option

  • Save jonatasemidio/c9aa1b89dbd8e32c379ed82a2408bf6e to your computer and use it in GitHub Desktop.

Select an option

Save jonatasemidio/c9aa1b89dbd8e32c379ed82a2408bf6e to your computer and use it in GitHub Desktop.
Palindrome Algorithms Ideas
n = "99"
s = n.length()
println s%2==0 ? "${n[0..(s/2-1)]} ${n[s/2..-1]}" : "${n[0..(s/2-1)]} ${n[s.intdiv(2)]} ${n[s/2+1..-1]}"
println s%2==0 ? "${n[0..(s/2-1)] == n[s/2..-1]}" : "${n[0..(s/2-1)] == n[s/2+1..-1]}"
println s%2==0 ? n[0..(s/2-1)] == n[s/2..-1] : n[0..(s/2-1)] == n[s/2+1..-1]
a = b = 100
def isPl(n){s=n.length(); return s%2==0 ? n[0..(s/2-1)] == n[s/2..-1] : n[0..(s/2-1)] == n[s/2+1..-1] }
def calc(a,b){
if(a<=9 || b<=9){
return false
}
println "a: $a and b: $b = ${ a*b } is palindrome (${ isPl((a*b).toString()) } )"
b>=a ? calc(a, --b) : calc(--a, b)
}
calc(a,b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment