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.

Revisions

  1. jonatasemidio revised this gist Jun 27, 2016. 1 changed file with 20 additions and 1 deletion.
    21 changes: 20 additions & 1 deletion palindromesDecrementadosUmPorUm.groovy
    Original file line number Diff line number Diff line change
    @@ -10,4 +10,23 @@ def calc(a,b){
    b>=a ? calc(a, --b) : calc(--a, b)
    }

    calc(a,b)
    calc(a,b)


    //----


    def isPl(n){s=n.length(); return s%2==0 ? n[0..(s/2-1)] == n[-1..s/2] : n[0..(s/2-1)] == n[-1..s/2+1] }
    a=999
    pl = [:]
    acc = []
    notFound = true
    while(a>=100 && notFound){
    for(b=999 ; b>=100; b--){
    acc << [a, b, a*b, isPl( (a*b).toString() )]
    if(isPl( (a*b).toString() )) {pl << [(a*b):[a,b]]}
    }
    a--
    }

    println pl.sort{ it.key }
  2. jonatasemidio revised this gist Jun 27, 2016. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions palindromesBrutalForce.groovy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    def isPl(n){s=n.length(); return s%2==0 ? n[0..(s/2-1)] == n[-1..s/2] : n[0..(s/2-1)] == n[-1..s/2+1] }
    a=99
    pl = [:]
    acc = []
    while(a>=10){
    for(b=99 ; b>=10; b--){
    acc << [a, b, a*b, isPl( (a*b).toString() )]
    if(isPl( (a*b).toString() )) pl << [(a*b):[a,b]]
    }
    a--
    }

    println pl.sort{ it.key }
  3. jonatasemidio revised this gist Jun 27, 2016. 1 changed file with 20 additions and 0 deletions.
    20 changes: 20 additions & 0 deletions palindromesDecrementadosDezPorDez.groovy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    def isPl(n){s=n.length(); return s%2==0 ? n[0..(s/2-1)] == n[-1..s/2] : n[0..(s/2-1)] == n[-1..s/2+1] }
    a = b = 999
    def calc(a,b){
    if(a<=9 || b<=9 || isPl((a*b).toString())){
    return [a, b, a*b]
    }
    println "a: $a and b: $b | a*b = ${a*b} isPl(${ (a*b).toString() } == ${isPl((a*b).toString())})"

    if(a%10!=0){
    calc(--a, b)

    }else if(b%10!=0){
    calc(a, --b)

    }else{
    a>=b ? calc(--a, b) : calc(a, --b)
    }
    }

    calc(a,b)
  4. jonatasemidio created this gist Jun 27, 2016.
    5 changes: 5 additions & 0 deletions checkIdeas.groovy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    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]
    13 changes: 13 additions & 0 deletions palindromesDecrementadosUmPorUm.groovy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    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)