-
Notifications
You must be signed in to change notification settings - Fork 0
/
0004.go
38 lines (33 loc) · 1.01 KB
/
0004.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
36
37
38
/*
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.
Find the largest palindrome made from the product of two 3-digit numbers.
*/
package main
import "fmt"
import "strconv"
func isPalindrome(x int) bool {
firstHalf := strconv.Itoa(x / 1000)
secondHalf := strconv.Itoa(x - ((x / 1000) * 1000))
runes := []rune(secondHalf)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
secondHalf = string(runes)
if firstHalf == secondHalf {
return true
}
return false
}
func main() {
i := 998001
for ; i >= 111*111 ; i-- {
if isPalindrome(i) {
for j := 999; j >= 111; j-- {
if (i / j < 1000 && i % j == 0) {
fmt.Printf("The largest palindrome made from the product of two 3-digit numbers (%d x %d) is %d\n", j, i/j, i)
return
}
}
}
}
}