diff options
| author | Adrien Hopkins <adrien.p.hopkins@gmail.com> | 2023-08-21 09:35:27 -0500 |
|---|---|---|
| committer | Adrien Hopkins <adrien.p.hopkins@gmail.com> | 2023-08-21 09:56:06 -0500 |
| commit | 547c63fbf0c6dd673e8caf83ea7f9eeb679b5f5c (patch) | |
| tree | 31c07bcab4ffb2c18d287f50d471660cd65bac39 | |
| parent | 30e92c33303535a86e56824b668f46ad0c6261a8 (diff) | |
Add MTC to output
(MTC = Multiplication Table Complexity)
| -rw-r--r-- | factors/factors_test.go | 30 | ||||
| -rw-r--r-- | factors/mtc.go | 19 | ||||
| -rw-r--r-- | radix_info.go | 5 |
3 files changed, 52 insertions, 2 deletions
diff --git a/factors/factors_test.go b/factors/factors_test.go index 77c5f4a..db6e098 100644 --- a/factors/factors_test.go +++ b/factors/factors_test.go @@ -100,6 +100,36 @@ func TestBasicRank(t *testing.T) { tableTest(t, BasicRank, basicRankCases, stdEquals[string], "BasicRank") } +var gcdCases = map[struct{ a, b uint }]uint{ + {0, 0}: 0, {1, 1}: 1, + {6, 10}: 2, {10, 6}: 2, + {12, 20}: 4, {20, 12}: 4, + {20, 55}: 5, {55, 20}: 5, + {60, 120}: 60, {120, 60}: 60, +} + +// a version of gcd() for testing purposes +func gcdTest(c struct{ a, b uint }) uint { + return gcd(c.a, c.b) +} + +func TestGCD(t *testing.T) { + tableTest(t, gcdTest, gcdCases, stdEquals[uint], "gcd") +} + +var mtcCases = map[uint]uint{ + 2: 0, 3: 0, 4: 2, 5: 10, 6: 8, + 7: 28, 8: 26, 9: 42, 10: 42, 11: 88, 12: 52, + 13: 130, 14: 100, 15: 116, 16: 138, 18: 146, + 20: 190, 24: 252, 28: 416, 30: 380, 32: 618, 36: 598, + 48: 1100, 60: 1496, 64: 2602, 90: 3662, 120: 6080, + 210: 18542, 360: 54362, 840: 270122, 2520: 2363528, 5040: 9409112, +} + +func TestMTC(t *testing.T) { + tableTest(t, MTC, mtcCases, stdEquals[uint], "MTC") +} + // to be used as the equal paramater for tableTest func stdEquals[T comparable](a, b T) bool { return a == b } diff --git a/factors/mtc.go b/factors/mtc.go new file mode 100644 index 0000000..d008ca3 --- /dev/null +++ b/factors/mtc.go @@ -0,0 +1,19 @@ +package factors + +// MTC returns the multiplication table complexity of a radix n. +// This is an estimate of how difficult it is to learn a radix's +// multiplication table. +func MTC(n uint) uint { + mtc := uint(0) + for i := uint(2); i <= n - 2; i++ { + mtc += n / gcd(i, n) + } + return mtc +} + +func gcd(a, b uint) uint { + for b > 0 { + a, b = b, a % b + } + return a +} diff --git a/radix_info.go b/radix_info.go index a107b73..263909d 100644 --- a/radix_info.go +++ b/radix_info.go @@ -13,14 +13,15 @@ func main() { if n, err := strconv.ParseUint(os.Args[1], 0, 0); err == nil { if n > 1 { n := uint(n) - fmt.Printf("%d = %s\n", n, factors.PrimeFactorize(n)) + fmt.Println(n, "=", factors.PrimeFactorize(n)) n_factors := factors.Factors(n) slices.Sort(n_factors) factorScore := factors.Score(n) fmt.Printf("Factors: %v (Score: %.2f)\n", n_factors, factorScore) fmt.Printf("Totative Ratio: %03.1f%%\n", factors.TotativeRatio(n)*100.0) - fmt.Printf("2345 Rank: %s\n", factors.BasicRank(n)) + fmt.Println("2345 Rank:", factors.BasicRank(n)) + fmt.Println("Multiplication Table Complexity:", factors.MTC(n)) } else { fmt.Println("Argument must be an integer above 1.") } |
