diff options
| author | Adrien Hopkins <adrien.p.hopkins@gmail.com> | 2023-08-30 17:54:17 -0500 |
|---|---|---|
| committer | Adrien Hopkins <adrien.p.hopkins@gmail.com> | 2023-08-30 20:02:26 -0500 |
| commit | aa73f47a5e2535224aa772f163aed2b6802bd4ad (patch) | |
| tree | e516fc505540eaa27abeb78214984d582c58c29c | |
| parent | 0f698d42907bc06f469ae3311433bcc741b8bd9b (diff) | |
Add compact display
| -rw-r--r-- | args.go | 16 | ||||
| -rw-r--r-- | factor_info.go | 33 | ||||
| -rw-r--r-- | print_digit_map.go | 16 | ||||
| -rw-r--r-- | radix_info.go | 6 |
4 files changed, 65 insertions, 6 deletions
@@ -3,20 +3,26 @@ package main import ( "errors" "fmt" - "os" + "flag" "strconv" ) // The arguments to this program type args struct { Radix uint + Compact bool } func parseArgs() (args, error) { - if len(os.Args) == 2 { - if radix, err := strconv.ParseUint(os.Args[1], 0, 0); err == nil { + var a args + flag.BoolVar(&a.Compact, "c", false, "Compact the output display") + flag.Parse() + + if flag.NArg() == 1 { + if radix, err := strconv.ParseUint(flag.Arg(0), 0, 0); err == nil { if radix > 1 { - return args{uint(radix)}, nil + a.Radix = uint(radix) + return a, nil } else { return args{}, errors.New("Radix must be an integer above 1.") } @@ -24,7 +30,7 @@ func parseArgs() (args, error) { return args{}, fmt.Errorf( "Argument must be an integer above 1 [%w].", err) } - } else if len(os.Args) < 2 { + } else if flag.NArg() < 1 { return args{}, errors.New("Please provide an argument (radix to study).") } else { return args{}, errors.New("Too many arguments provided.") diff --git a/factor_info.go b/factor_info.go index ee04e32..a6b91a9 100644 --- a/factor_info.go +++ b/factor_info.go @@ -95,6 +95,22 @@ func (fi *FactorInfo) WriteTo(w io.Writer) { } } +func (fi *FactorInfo) WriteToCompact(w io.Writer) { + fmt.Fprintf(w, "%d = %s | σ(n)/n: %.2f | τ(n)/n: %.3f | 2345: %s\n", + fi.Radix, fi.PrimeFactorization, fi.Score, fi.TotativeRatio, fi.BasicRank) + fmt.Fprintf(w, "Ln: %.2f", fi.Ln) + if fi.MTC != nil { + fmt.Fprintf(w, " | MTC: %d", *fi.MTC) + } + if fi.Type != nil { + fmt.Fprintf(w, " | %s", typeAbbrev(*fi.Type)) + } + fmt.Fprintln(w) + if len(fi.DigitMap) > 0 { + writeDigitMapCompact(w, fi.DigitMap) + } +} + func writeTypeMessage(w io.Writer, t factors.NumberType) { switch t { case factors.ColossallyAbundant: @@ -107,3 +123,20 @@ func writeTypeMessage(w io.Writer, t factors.NumberType) { fmt.Fprintln(w, "This radix is practical.") } } + +func typeAbbrev(t factors.NumberType) 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.NotPractical: + return "Not Practical" + default: + panic("Should not be possible to get here.") + } +} diff --git a/print_digit_map.go b/print_digit_map.go index 4def177..b878a51 100644 --- a/print_digit_map.go +++ b/print_digit_map.go @@ -8,6 +8,9 @@ import ( "strings" ) +// The maximum radix that is supported by writeDigitMapSmall +const maxSmallRadix = 36 + func writeDigitMap(w io.Writer, digitMap []factors.DigitType) { if len(digitMap) < 2 { panic("Radices cannot be less than 2!") @@ -36,6 +39,19 @@ func writeDigitMapSmall(w io.Writer, digitMap []factors.DigitType) { fmt.Fprintln(w, typesString.String()) } +// Prints a compactified digit map, used in the compact display. +func writeDigitMapCompact(w io.Writer, digitMap []factors.DigitType) { + radix := uint(len(digitMap)) + if radix <= maxSmallRadix { + fmt.Fprint(w, "Digits: ") + for digit, digitType := range digitMap { + fmt.Fprint(w, colourString(strings.ToUpper( + strconv.FormatUint(uint64(digit), int(radix))), digitType)) + } + fmt.Fprintln(w) + } +} + func colourString(s string, digitType factors.DigitType) string { var colourBegin string switch digitType.TotativeType() { diff --git a/radix_info.go b/radix_info.go index 386c050..04a8228 100644 --- a/radix_info.go +++ b/radix_info.go @@ -9,7 +9,11 @@ func main() { args, err := parseArgs() if err == nil { factorInfo := GetFactorInfo(args.Radix) - factorInfo.WriteTo(os.Stdout) + if args.Compact { + factorInfo.WriteToCompact(os.Stdout) + } else { + factorInfo.WriteTo(os.Stdout) + } } else { fmt.Fprintln(os.Stderr, err) } |
