-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path4.go
81 lines (69 loc) · 1.79 KB
/
4.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
/*
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.
*/
import (
"encoding/json"
"math"
"net/http"
"strconv"
)
func EulerFourHandler(rw http.ResponseWriter, req *http.Request) {
responseData := map[string]interface{}{
"result": "OK",
"answer": getLargestPalindromeByDigits(3),
}
jsonEncoder := json.NewEncoder(rw)
jsonEncoder.Encode(responseData)
}
func getLargestPalindromeByDigits(digits int) int {
productA := getLargestNumberForDigits(digits)
productB := productA
limit := getLargestNumberForDigits(digits - 1)
palindromes := []int{}
for i := productA; i > limit; i-- {
for n := productB; n > limit; n-- {
palindromes = palindromeCollector(n, i, palindromes)
}
palindromes = palindromeCollector(i, productB, palindromes)
}
return findGreatestNumberInSlice(palindromes)
}
func getLargestNumberForDigits(digits int) int {
products := math.Pow(10, (float64(digits))) - 1
return int(products)
}
func palindromeCollector(a int, b int, list []int) []int {
if isPalindrome(a * b) {
list = append(list, a*b)
}
return list
}
func isPalindrome(num int) bool {
numSlice := convertIntToSliceOfString(num)
for i := 0; i < len(numSlice); i++ {
if numSlice[i] != numSlice[len(numSlice)-(i+1)] {
return false
}
}
return true
}
func convertIntToSliceOfString(num int) []string {
numString := strconv.Itoa(num)
var numList []string
for i := 0; i < len(numString); i++ {
numList = append(numList, string(numString[i]))
}
return numList
}
func findGreatestNumberInSlice(slice []int) int {
highest := 0
for i := 0; i < len(slice); i++ {
if slice[i] > highest {
highest = slice[i]
}
}
return highest
}