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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
/* This script is part of radix_info.
Copyright (C) 2023 Adrien Hopkins
This program is free software: you can redistribute it and/or modify
it under the terms of version 3 of the GNU General Public License
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"aphopkins/radix_info/factors"
"fmt"
"io"
"math"
"sort"
)
// FactorInfo contains all of the information this program
// calculates about a radix.
type factorInfo struct {
// The radix this info is about
Radix uint
// A representation of this radix as a product of prime numbers.
// Most of the important info about radices is determined through this.
PrimeFactorization factors.PrimeFactorization
// The radix's factors, in sorted order.
Factors []uint
// An estimate of the utility of the radix's factors.
Score float64
// The number of digits that are totatives (numbers that share no
// factors with the radix - they are the worst kind of digits)
Totient uint
// The fraction of digits that are totatives (numbers that share no
// factors with the radix - they are the worst kind of digits)
TotativeRatio float64
// The totative digits of this radix. May be nil if this was not requested.
TotativeDigits []uint32
// A rank measuring how well the radix works with the most elementary
// numbers and ratios
BasicRank string
// Whether or not this radix is part of any special factor-related classes.
// This is not calculated if the radix is too large - in this case
// this field will be nil.
Type factors.CompositenessType
// An estimate of the complexity of the radix's multiplication table.
// This is not calculated if the radix is too large - in this case
// this field will be nil.
MTC *uint64
// The radix's natural logarithm. This determines the length of numbers
// in this radix - higher Ln means numbers take up fewer digits.
// If c = log(a)/log(b), then numbers in radix b will be around
// c times longer than numbers in radix a.
Ln float64
// Information about each digit's compatibility with the radix.
// This determines what kind of decimal expansion the digit's
// reciprocoal has and what patterns are in its row of the multiplication
// table.
DigitMap []factors.DigitType
}
const (
// The maximum radix that will always be treated as normal
// (i.e. radix type and exact MTC will be calculated)
maxNormal = 1 << 16
// The max radix that will be treated as normal with -l
// above this, MTC can exceed the size of a uint64
maxExtended = 1 << 32
)
func getFactorInfo(a args) *factorInfo {
radix := a.Radix
r_factors := factors.Factors(radix)
sort.Slice(r_factors, func(i, j int) bool {
return r_factors[i] < r_factors[j]
})
var totativeDigits []uint32 = nil
if a.TotativeDigits && radix < maxExtended {
totativeDigits = factors.TotativeDigits(uint32(radix))
}
var mtc_ptr *uint64 = nil
if radix < maxNormal || (a.ExactMTCLarge && radix < maxExtended) {
mtc := factors.MTC(uint32(radix))
mtc_ptr = &mtc
}
var digitMap []factors.DigitType
if a.FullMap {
digitMap = make([]factors.DigitType, maxSmallRadix)
for d := 0; d < maxSmallRadix; d++ {
digitMap[d] = factors.GetDigitType(uint(d), radix)
}
} else if radix <= maxSmallRadix {
digitMap = factors.DigitMap(radix)
} else {
digitMap = []factors.DigitType{}
}
totativeCount := factors.Totient(radix)
totativeRatio := float64(totativeCount) / float64(radix)
return &factorInfo{radix, factors.PrimeFactorize(radix),
r_factors, factors.Score(radix), totativeCount, totativeRatio,
totativeDigits, factors.BasicRank(radix), factors.Type(radix),
mtc_ptr, math.Log(float64(radix)), digitMap}
}
func (fi *factorInfo) writeTo(w io.Writer) {
fmt.Fprintln(w, fi.Radix, "=", fi.PrimeFactorization)
fmt.Fprintln(w, "Factors:", fi.Factors)
fmt.Fprintf(w, "Factor Utility Score: %.4f\n", fi.Score)
fmt.Fprintf(w, "Regular Utility Score: %.4f\n",
float64(fi.Radix)/float64(fi.Totient))
fmt.Fprintln(w, "2345 Rank:", fi.BasicRank)
if fi.TotativeDigits != nil {
fmt.Fprintf(w, "Totative Digits: %v (%.3f%%)\n",
fi.TotativeDigits, fi.TotativeRatio*100.0)
} else {
fmt.Fprintf(w, "Totative Digit Count: %d (%.3f%%)\n",
fi.Totient, fi.TotativeRatio*100.0)
}
writeTypeMessage(w, fi.Type)
if fi.MTC != nil {
fmt.Fprintln(w, "Multiplication Table Complexity:", *fi.MTC)
} else {
low_mtc_est := float64(fi.Radix) * float64(fi.Totient-2)
high_mtc_est := float64(fi.Radix) * float64(fi.Radix-2)
fmt.Fprintf(w,
"Multiplication Table Complexity is between %.6g and %.6g.\n",
low_mtc_est, high_mtc_est)
}
fmt.Fprintf(w, "Natural Logarithm: %.3f\n", fi.Ln)
if len(fi.DigitMap) > 0 {
writeDigitMap(w, fi.DigitMap)
}
}
func (fi *factorInfo) writeToCompact(w io.Writer) {
fmt.Fprintf(w, "%d = %s | σ(r)/r: %.2f | r/φ(r): %.2f\n",
fi.Radix, fi.PrimeFactorization, fi.Score, 1/fi.TotativeRatio)
fmt.Fprintf(w, "%s | ", typeAbbrev(fi.Type))
if fi.MTC != nil {
fmt.Fprintf(w, "MTC: %d | ", *fi.MTC)
} else {
low_mtc_est := float32(fi.Radix) * float32(fi.Totient-2)
high_mtc_est := float32(fi.Radix) * float32(fi.Radix-2)
fmt.Fprintf(w, "%.4g ≤ MTC ≤ %.4g | ", low_mtc_est, high_mtc_est)
}
fmt.Fprintf(w, "ln: %.2f", fi.Ln)
fmt.Fprintln(w)
if len(fi.DigitMap) > 0 {
writeDigitMapCompact(w, fi.DigitMap)
}
}
func writeTypeMessage(w io.Writer, t factors.CompositenessType) {
switch t {
case factors.ColossallyAbundant:
fmt.Fprintln(w, "This radix is colossally abundant!")
case factors.Superabundant:
fmt.Fprintln(w, "This radix is superabundant.")
case factors.OrderedExponent:
fmt.Fprintln(w, "This radix has ordered exponents.")
case factors.Practical:
fmt.Fprintln(w, "This radix is practical.")
}
}
func typeAbbrev(t factors.CompositenessType) string {
switch t {
case factors.ColossallyAbundant:
return "Colossally Abundant"
case factors.Superabundant:
return "Superabundant"
case factors.OrderedExponent:
return "Ordered Exponents"
case factors.Practical:
return "Practical"
case factors.None:
return "Not Practical"
default:
panic("Should not be possible to get here.")
}
}
|