From 32320351ec98bbdf526d1587d073e4f3a382f2d5 Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Wed, 30 Aug 2023 16:20:50 -0500 Subject: Extract factor info into separate struct This achieves two things: - Decouples my code by putting the printing code into its own file - Makes it easier to make alternate ways of printing (e.g. a compact mode) --- factor_info.go | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++ print_digit_map.go | 2 +- radix_info.go | 38 +------------------ 3 files changed, 112 insertions(+), 37 deletions(-) create mode 100644 factor_info.go diff --git a/factor_info.go b/factor_info.go new file mode 100644 index 0000000..ee04e32 --- /dev/null +++ b/factor_info.go @@ -0,0 +1,109 @@ +package main + +import ( + "aphopkins/radix_info/factors" + "fmt" + "io" + "math" + "slices" +) + +// 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 fraction of digits that are totatives (numbers that share no + // factors with the radix - they are the worst kind of digits) + TotativeRatio float64 + // 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.NumberType + // 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 +} + +func GetFactorInfo(radix uint) *FactorInfo { + r_factors := factors.Factors(radix) + slices.Sort(r_factors) + + var r_type_ptr *factors.NumberType + var mtc_ptr *uint64 + if radix < 1<<32 { + r_type := factors.Type(uint32(radix)) + r_type_ptr = &r_type + mtc := factors.MTC(uint64(radix)) + mtc_ptr = &mtc + } else { + r_type_ptr = nil + mtc_ptr = nil + } + + var digitMap []factors.DigitType + if radix <= maxSmallRadix { + digitMap = factors.DigitMap(radix) + } else { + digitMap = []factors.DigitType{} + } + + return &FactorInfo{radix, factors.PrimeFactorize(radix), + r_factors, factors.Score(radix), factors.TotativeRatio(radix), + factors.BasicRank(radix), r_type_ptr, mtc_ptr, + math.Log(float64(radix)), digitMap} +} + +func (fi *FactorInfo) WriteTo(w io.Writer) { + fmt.Fprintln(w, fi.Radix, "=", fi.PrimeFactorization) + fmt.Fprintf(w, "Factors: %v (Score: %.2f)\n", fi.Factors, fi.Score) + fmt.Fprintf(w, "Totative Ratio: %.1f%%\n", fi.TotativeRatio * 100.0) + fmt.Fprintln(w, "2345 Rank:", fi.BasicRank) + if fi.Type != nil { + writeTypeMessage(w, *fi.Type) + } + if fi.MTC != nil { + fmt.Fprintln(w, "Multiplication Table Complexity:", *fi.MTC) + } else { + fmt.Fprintf(w, "Multiplication Table Complexity ≤ %.4g\n", + float32(fi.Radix)*float32(fi.Radix-2)) + } + fmt.Fprintf(w, "Natural Logarithm: %.2f\n", fi.Ln) + if len(fi.DigitMap) > 0 { + writeDigitMap(w, fi.DigitMap) + } +} + +func writeTypeMessage(w io.Writer, t factors.NumberType) { + 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.") + } +} diff --git a/print_digit_map.go b/print_digit_map.go index d4f7f3d..4def177 100644 --- a/print_digit_map.go +++ b/print_digit_map.go @@ -9,7 +9,7 @@ import ( ) func writeDigitMap(w io.Writer, digitMap []factors.DigitType) { - if len(digitMap) <= 2 { + if len(digitMap) < 2 { panic("Radices cannot be less than 2!") } else if len(digitMap) <= 36 { writeDigitMapSmall(w, digitMap) diff --git a/radix_info.go b/radix_info.go index 20cbc20..32e1e40 100644 --- a/radix_info.go +++ b/radix_info.go @@ -1,11 +1,8 @@ package main import ( - "aphopkins/radix_info/factors" "fmt" - "math" "os" - "slices" "strconv" ) @@ -13,26 +10,8 @@ func main() { if len(os.Args) > 1 { if n, err := strconv.ParseUint(os.Args[1], 0, 0); err == nil { if n > 1 { - n := uint(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.Println("2345 Rank:", factors.BasicRank(n)) - if n < 1<<32 { - printTypeMessage(factors.Type(uint32(n))) - // MTC(n) < n^2, so n < 2^32 ensures it fits in a uint64 - fmt.Println("Multiplication Table Complexity:", - factors.MTC(uint64(n))) - } else { - fmt.Printf("Multiplication Table Complexity ≤ %.4g\n", - float32(n)*float32(n-2)) - } - fmt.Printf("Natural Logarithm: %.2f\n", math.Log(float64(n))) - writeDigitMap(os.Stdout, factors.DigitMap(n)) + factorInfo := GetFactorInfo(uint(n)) + factorInfo.WriteTo(os.Stdout) } else { fmt.Println("Argument must be an integer above 1.") } @@ -43,16 +22,3 @@ func main() { fmt.Println("Please provide an argument (radix to study).") } } - -func printTypeMessage(t factors.NumberType) { - switch t { - case factors.ColossallyAbundant: - fmt.Println("This radix is colossally abundant!") - case factors.Superabundant: - fmt.Println("This radix is superabundant.") - case factors.OrderedExponent: - fmt.Println("This radix has ordered exponents.") - case factors.Practical: - fmt.Println("This radix is practical.") - } -} -- cgit v1.2.3