diff options
| author | Adrien Hopkins <adrien.p.hopkins@gmail.com> | 2023-09-05 11:15:28 -0500 |
|---|---|---|
| committer | Adrien Hopkins <adrien.p.hopkins@gmail.com> | 2023-09-05 11:15:28 -0500 |
| commit | ebdc5107b8b2bd69a65a34a6276fc64b84d210ec (patch) | |
| tree | 5cf307a129bfa52d362f648d99857209a158d230 | |
| parent | d034179fde42c4fc633b98fef6e84c2e0d1c2834 (diff) | |
Remove totative ratio from factors API
This value can easily be calculated as φ(r)/r. There is no need to have
this now that I have a function φ(r) (renamed to its mathematical name,
Totient).
I removed totative ratio instead of totient because, while it is more
important, totient is an integer while totative ratio is a float. This
means that the totative ratio can be calculated exactly from the
totient, but not the other way round.
| -rw-r--r-- | factor_info.go | 15 | ||||
| -rw-r--r-- | factors/factors_test.go | 16 | ||||
| -rw-r--r-- | factors/totative.go | 14 |
3 files changed, 23 insertions, 22 deletions
diff --git a/factor_info.go b/factor_info.go index f908b9b..c243048 100644 --- a/factor_info.go +++ b/factor_info.go @@ -22,7 +22,7 @@ type FactorInfo struct { Score float64 // The number of digits that are totatives (numbers that share no // factors with the radix - they are the worst kind of digits) - TotativeCount uint + 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 @@ -77,10 +77,13 @@ func GetFactorInfo(radix uint, fullMap bool) *FactorInfo { digitMap = []factors.DigitType{} } + totativeCount := factors.Totient(radix) + totativeRatio := float64(totativeCount) / float64(radix) + return &FactorInfo{radix, factors.PrimeFactorize(radix), - r_factors, factors.Score(radix), factors.TotativeCount(radix), - factors.TotativeRatio(radix), factors.BasicRank(radix), - r_type_ptr, mtc_ptr, math.Log(float64(radix)), digitMap} + r_factors, factors.Score(radix), totativeCount, totativeRatio, + factors.BasicRank(radix), r_type_ptr, mtc_ptr, + math.Log(float64(radix)), digitMap} } func (fi *FactorInfo) WriteTo(w io.Writer) { @@ -88,7 +91,7 @@ func (fi *FactorInfo) WriteTo(w io.Writer) { fmt.Fprintf(w, "Factors: %v (Score: %.4f)\n", fi.Factors, fi.Score) fmt.Fprintln(w, "2345 Rank:", fi.BasicRank) fmt.Fprintf(w, "Totative Digit Count: %d (%.3f%%)\n", - fi.TotativeCount, fi.TotativeRatio * 100.0) + fi.Totient, fi.TotativeRatio * 100.0) if fi.Type != nil { writeTypeMessage(w, *fi.Type) } @@ -105,7 +108,7 @@ 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\n", + fmt.Fprintf(w, "%d = %s | σ(n)/n: %.2f | φ(n)/n: %.3f\n", fi.Radix, fi.PrimeFactorization, fi.Score, fi.TotativeRatio) if fi.Type != nil { fmt.Fprintf(w, "%s | ", typeAbbrev(*fi.Type)) diff --git a/factors/factors_test.go b/factors/factors_test.go index af91e70..745fd2c 100644 --- a/factors/factors_test.go +++ b/factors/factors_test.go @@ -67,17 +67,25 @@ var totativeRatioCases = map[uint]float64{ 30: 4.0 / 15.0, 60: 4.0 / 15.0, 120: 4.0 / 15.0, } +func totativeRatio(n uint) float64 { + if n == 0 { + panic("0 has no totative ratio!") + } + + return float64(Totient(n)) / float64(n) +} + func TestTotativeRatio(t *testing.T) { - tableTest(t, TotativeRatio, totativeRatioCases, stdEquals, "TotativeRatio") + tableTest(t, totativeRatio, totativeRatioCases, stdEquals, "TotativeRatio") } -var totativeCountCases = map[uint]uint{ - 1: 1, 2: 1, 3: 2, 4: 2, 6: 2, 7: 6, 8: 4, 12: 4, +var totientCases = map[uint]uint{ + 0: 0, 1: 1, 2: 1, 3: 2, 4: 2, 6: 2, 7: 6, 8: 4, 12: 4, 14: 6, 15: 8, 30: 8, 60: 16, 120: 32, } func TestTotativeCount(t *testing.T) { - tableTest(t, TotativeCount, totativeCountCases, stdEquals, "TotativeCount") + tableTest(t, Totient, totientCases, stdEquals, "Totient") } var factorScoreCases = map[uint]float64{ diff --git a/factors/totative.go b/factors/totative.go index 7a4bebe..680cc26 100644 --- a/factors/totative.go +++ b/factors/totative.go @@ -1,18 +1,8 @@ package factors -// TotativeRatio calculates the fraction of numbers that +// Totient calculates the number of numbers less than n that // are totatives of n (share no factors with n) -func TotativeRatio(n uint) float64 { - if n == 0 { - panic("0 has no totative ratio!") - } - - return float64(TotativeCount(n)) / float64(n) -} - -// TotativeCount calculates the number of numbers less than n that -// are totatives of n (share no factors with n) -func TotativeCount(n uint) uint { +func Totient(n uint) uint { if n == 0 { return 0 } |
