Skip to content

Commit

Permalink
Yay problem 4 in Go
Browse files Browse the repository at this point in the history
  • Loading branch information
toothrot committed Mar 18, 2015
1 parent 7f3cdd8 commit 4910045
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
38 changes: 38 additions & 0 deletions project-euler/go/4/problem4.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import "fmt"
import "strconv"

// https://github.com/daviskoh/go-code-challenges/tree/master/reverse_string
func ReverseString(s string) string {
if len(s) < 2 {
return s
}

return s[len(s)-1:len(s)] + ReverseString(s[0:len(s)-1])
}

func isPalendrome(n int) bool {
testString := strconv.Itoa(n)
return testString == ReverseString(testString)
}

func problem4() int {
max := 0

for i := 100; i <= 999; i++ {
for j := 100; j <= 999; j++ {
result := i * j
if isPalendrome(i*j) && result > max {
max = result
}
}
}

return max
}

func main() {
result := problem4()
fmt.Println("Answer:", result)
}
33 changes: 33 additions & 0 deletions project-euler/go/4/problem4_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import "testing"

func TestProblem4(t *testing.T) {
expected := 906609
got := problem4()
if got != expected {
t.Errorf("oh no! expected: %i got: %i", expected, got)
}
}

func TestIsPalendrome(t *testing.T) {
got := isPalendrome(101)
if got != true {
t.Errorf("oh no! expected: %i got: %i", true, got)
}

got = isPalendrome(1)
if got != true {
t.Errorf("oh no! expected: %i got: %i", true, got)
}

got = isPalendrome(11)
if got != true {
t.Errorf("oh no! expected: %i got: %i", true, got)
}

got = isPalendrome(12)
if got != false {
t.Errorf("oh no! expected: %i got: %i", true, got)
}
}

0 comments on commit 4910045

Please sign in to comment.