-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
118 lines (95 loc) · 2.29 KB
/
main.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"encoding/csv"
"errors"
"log"
"math"
"os"
"strconv"
)
const defaultStartBase = 2
const defaultEndBase = 20
const defaultEndNum = 1000
// Input is the number being checked for a palindrome in the smallest base
type Input int
func main() {
var num Input
count := 1 // skip first index for header
records := make([][]string, defaultEndNum)
for num = 0; num <= defaultEndNum; num++ {
res, _ := num.smallesPalindBase(defaultEndBase)
if res > 0 {
records[count] = append(records[count], strconv.Itoa(int(num)), strconv.Itoa(res))
count++
}
}
if count > 1 {
writeOutput(records)
}
}
func (number Input) smallesPalindBase(maxNumBase int) (int, error) {
if maxNumBase < 2 {
return -1, errors.New("Base less than 2 not evaluated")
}
for base := defaultStartBase; base <= maxNumBase; base++ {
size := number.representationSize(base)
baseRepresentation := make([]int, size)
number.decompose(baseRepresentation, base)
if isPalindrome(baseRepresentation) {
return base, nil
}
}
return 0, nil
}
func (number Input) representationSize(base int) int {
if number > 0 {
x := logB(float64(number), base)
size := int(math.Ceil(x))
if int(x) == size {
size++
}
return size
}
return 1
}
func (number Input) decompose(baseRepresentation []int, base int) {
current := int(number)
for i := range baseRepresentation {
if current < base {
baseRepresentation[i] = current
break
}
baseRepresentation[i] = current % base
current = current / base
}
}
func isPalindrome(baseRepresentation []int) bool {
size := len(baseRepresentation)
if size == 1 {
return true
}
midPoint := int(math.Floor(float64(size) / float64(2.0)))
for i := 0; i < midPoint; i++ {
if baseRepresentation[i] != baseRepresentation[size-i-1] {
return false
}
}
return true
}
func logB(x float64, base int) float64 {
return math.Log(x) / math.Log(float64(base))
}
func writeOutput(records [][]string) {
output, err := os.Create("output.csv")
if err != nil {
log.Fatalln("error creating csv file:", err)
return
}
defer output.Close()
records[0] = append(records[0], "decimal", "smallest base in which the number is a palindrome")
w := csv.NewWriter(output)
w.WriteAll(records)
if err := w.Error(); err != nil {
log.Fatalln("error writing csv:", err)
}
}