summaryrefslogtreecommitdiff
path: root/factors/totative.go
blob: 558f0f0ae0611f3e23aaa502be101f41e9f156bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
}