summaryrefslogtreecommitdiff
path: root/factors/totative.go
diff options
context:
space:
mode:
authorAdrien Hopkins <adrien.p.hopkins@gmail.com>2023-08-07 16:34:47 -0500
committerAdrien Hopkins <adrien.p.hopkins@gmail.com>2023-08-21 09:56:04 -0500
commit564e53cdd4d6fc8b611d59c2c19af42864e6ece4 (patch)
treee48090eb1bc0162c346f7f63ed10305eec778fa8 /factors/totative.go
parentb4bdd6146962d8dde391f09b2cdda9553cb44bde (diff)
Add totative ratio and factor score to program
Diffstat (limited to 'factors/totative.go')
-rw-r--r--factors/totative.go17
1 files changed, 17 insertions, 0 deletions
diff --git a/factors/totative.go b/factors/totative.go
new file mode 100644
index 0000000..558f0f0
--- /dev/null
+++ b/factors/totative.go
@@ -0,0 +1,17 @@
+package factors
+
+// TotativeRatio calculates the fraction 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!")
+ }
+
+ primeFactorization := PrimeFactorize(n)
+
+ totativeRatio := 1.0
+ for p := range primeFactorization.exponents {
+ totativeRatio *= float64(p - 1) / float64(p)
+ }
+ return totativeRatio
+}