-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem4.go
35 lines (27 loc) · 808 Bytes
/
problem4.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package main
import "fmt"
func solveProblem4() {
answer := maxPalindromicProductOfIntegersBetween(100, 999)
fmt.Printf("Largest palindromic product of 3-digit numbers is %d\n", answer)
}
func maxPalindromicProductOfIntegersBetween(min, max int) int {
maxPalindromicProduct := 0
for i := min; i <= max; i++ {
for j := min; j <= max; j++ {
product := i * j
productString := fmt.Sprintf("%d", product)
if productString == reverse(productString) && product > maxPalindromicProduct {
maxPalindromicProduct = product
}
}
}
return maxPalindromicProduct
}
// https://github.com/golang/example/blob/master/stringutil/reverse.go
func reverse(s string) string {
r := []rune(s)
for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
r[i], r[j] = r[j], r[i]
}
return string(r)
}