From b9164fb5b41136a63391f5675a848ec4a7711cb6 Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Fri, 25 Aug 2023 10:40:00 -0500 Subject: 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) --- factors/totative.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'factors/totative.go') 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) } -- cgit v1.2.3