-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
55 lines (40 loc) · 1.07 KB
/
main.go
File metadata and controls
55 lines (40 loc) · 1.07 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"strconv"
)
func main() {
p := palindrom()
println("Answer is ", p)
}
/* 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. */
func palindrom() int {
var pali int
var good int
for i := 1; i <= 999; i++ {
for j := 1; j <= 999; j++ {
x := i * j
//Let's find out how many digits x have!
s := strconv.Itoa(x)
length := len(s)
//Is number of digits even? Otherwise we don't care!
if length%2 == 0 {
count := length / 2
//Loop through numbers and for each match, count + 1 to good
for z := 0; z < count; z++ {
if s[z] == s[length-z-1] {
good++
}
}
//We need to check just half of digits, good equal count means that it is palindrom!
if good == count && x > pali {
pali = x
println(pali, " - ", i, " - ", j)
}
//Reset count - honestly don't know how to reset it in more noble way
good = 0
}
}
}
return pali
}