-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindromeproducts.go
72 lines (63 loc) · 1.61 KB
/
palindromeproducts.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
package main
import (
"fmt"
"math"
"sort"
"strconv"
)
func main() {
const lower = 10
const upper = 99
var products []int
var palindromes []int
var lowerfactors []int
var upperfactors []int
var producthold string
var productreverse string
for i := lower; i <= upper; i++ {
for j := lower; j <= upper; j++ {
products = append(products, i*j)
}
}
products = removeDuplicates(products)
for x := range products {
producthold = strconv.Itoa(products[x])
data := []rune(producthold)
result := []rune{}
for z := len(data) - 1; z >= 0; z-- {
result = append(result, data[z])
}
productreverse = string(result)
if producthold == productreverse {
pal, _ := strconv.Atoi(producthold)
palindromes = append(palindromes, pal)
}
}
sort.Slice(palindromes, func(i, j int) bool {
return palindromes[i] < palindromes[j]
})
for y := 1; y <= palindromes[0]; y++ {
if math.Mod(float64(palindromes[0]), float64(y)) == 0 {
lowerfactors = append(lowerfactors, y)
}
}
for y := 1; y <= palindromes[len(palindromes)-1]; y++ {
if math.Mod(float64(palindromes[len(palindromes)-1]), float64(y)) == 0 {
upperfactors = append(upperfactors, y)
}
}
fmt.Printf("Lowest = %v, Highest = %v\n", palindromes[0], palindromes[len(palindromes)-1])
fmt.Printf("Factors of %v = %v\n", palindromes[0], lowerfactors)
fmt.Printf("Factors of %v = %v\n", palindromes[len(palindromes)-1], upperfactors)
}
func removeDuplicates(a []int) []int {
result := []int{}
seen := map[int]int{}
for _, val := range a {
if _, ok := seen[val]; !ok {
result = append(result, val)
seen[val] = val
}
}
return result
}