diff --git a/04_largest_palindrome_product/main.go b/04_largest_palindrome_product/main.go index 2491f3d..8d26c5a 100644 --- a/04_largest_palindrome_product/main.go +++ b/04_largest_palindrome_product/main.go @@ -7,11 +7,15 @@ import ( ) func main() { + findMaxPalindrome(100, 999) +} + +func findMaxPalindrome(start, end int) int { var n int var x int var y int - for i := 100; i < 1000; i++ { - for j := 100; j < 1000; j++ { + for i := start; i < end+1; i++ { + for j := start; j < end+1; j++ { tmp := i * j if tmp > n && IsPalindrome(tmp) { x = i @@ -21,6 +25,7 @@ func main() { } } fmt.Println(x, y, n) + return n } func IsPalindrome(n int) bool { diff --git a/04_largest_palindrome_product/palindrome_test.go b/04_largest_palindrome_product/palindrome_test.go index e276089..74d231e 100644 --- a/04_largest_palindrome_product/palindrome_test.go +++ b/04_largest_palindrome_product/palindrome_test.go @@ -46,3 +46,28 @@ func TestIsPalindrome(t *testing.T) { }) } } + +func Test_findMaxPalindrome(t *testing.T) { + type args struct { + start int + end int + } + tests := []struct { + name string + args args + want int + }{ + { + "1", + args{10, 99}, + 9009, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := findMaxPalindrome(tt.args.start, tt.args.end); got != tt.want { + t.Errorf("findMaxPalindrome() = %v, want %v", got, tt.want) + } + }) + } +}