summaryrefslogtreecommitdiff
path: root/factors/totative.go
diff options
context:
space:
mode:
authorAdrien Hopkins <adrien.p.hopkins@gmail.com>2023-08-25 10:40:00 -0500
committerAdrien Hopkins <adrien.p.hopkins@gmail.com>2023-08-25 10:46:59 -0500
commitb9164fb5b41136a63391f5675a848ec4a7711cb6 (patch)
tree6aa7f00fb2e53462f7cb64ff0a0e56bc8f8b6e59 /factors/totative.go
parent99c4b2d9980dd14e8c43ffbf00660047335ada4b (diff)
Make totative ratio exact
(or, as exact as possible within a float64 - I only do one float division, and everything else is a uint, so I think this means I will get the closest available float64 value every time)
Diffstat (limited to 'factors/totative.go')
-rw-r--r--factors/totative.go7
1 files changed, 4 insertions, 3 deletions
diff --git a/factors/totative.go b/factors/totative.go
index 558f0f0..3a1f635 100644
--- a/factors/totative.go
+++ b/factors/totative.go
@@ -8,10 +8,11 @@ func TotativeRatio(n uint) float64 {
}
primeFactorization := PrimeFactorize(n)
+ num, denom := uint(1), uint(1)
- totativeRatio := 1.0
for p := range primeFactorization.exponents {
- totativeRatio *= float64(p - 1) / float64(p)
+ num *= p - 1
+ denom *= p
}
- return totativeRatio
+ return float64(num) / float64(denom)
}