diff options
| author | Adrien Hopkins <adrien.p.hopkins@gmail.com> | 2023-08-07 16:34:47 -0500 |
|---|---|---|
| committer | Adrien Hopkins <adrien.p.hopkins@gmail.com> | 2023-08-21 09:56:04 -0500 |
| commit | 564e53cdd4d6fc8b611d59c2c19af42864e6ece4 (patch) | |
| tree | e48090eb1bc0162c346f7f63ed10305eec778fa8 /factors/score.go | |
| parent | b4bdd6146962d8dde391f09b2cdda9553cb44bde (diff) | |
Add totative ratio and factor score to program
Diffstat (limited to 'factors/score.go')
| -rw-r--r-- | factors/score.go | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/factors/score.go b/factors/score.go new file mode 100644 index 0000000..707120d --- /dev/null +++ b/factors/score.go @@ -0,0 +1,28 @@ +package factors + +// Score returns a "factor score" equal to the sum of the reciprocoals +// of the number n's factors. +// Rationale: +// A number's factors are one of the most important things that determines +// how well it functions as a number base. An easy way to determine how +// good these factors are is to simply count them, but that comes with +// the problem that all factors are considered equal when they really aren't +// - divisibility by 2 (which ensures 1/2 is a terminating fraction and +// you can tell whether a number is even or odd by looking at the last digit) +// is more important than divisibility by 23. The most obvious way of +// accounting for this is by giving each factor a score and adding those +// scores to get the overall score. I chose score(f) = 1/f because it is +// the simplest function that captures the intuition that smaller factors +// are more valuable than larger ones. It also gives the score a meaning: +// a factor's score is the probability a random number is divisible by it. +func Score(n uint) float64 { + if n == 0 { + panic("Cannot get factor score of 0.") + } + + factorSum := uint(0) + for _, f := range Factors(n) { + factorSum += f + } + return float64(factorSum) / float64(n) +} |
