summaryrefslogtreecommitdiff
path: root/factors/totative.go
blob: 3a1f635a045aa2ed642f25f8e4f08f5577bba6c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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)
	num, denom := uint(1), uint(1)

	for p := range primeFactorization.exponents {
		num *= p - 1
		denom *= p
	}
	return float64(num) / float64(denom)
}