blob: ff3a4abf35d434a3dd0d594e484fcaa9971add4b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
/* This script is part of radix_info.
Copyright (C) 2023 Adrien Hopkins
This program is free software: you can redistribute it and/or modify
it under the terms of version 3 of the GNU General Public License
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package factors
// Totient calculates the number of numbers between 1 and n inclusive
// that are totatives of n (share no factors with n).
func Totient(n uint) uint {
if n == 0 {
return 0
}
primeFactorization := PrimeFactorize(n)
num, denom := uint(1), uint(1)
for p := range primeFactorization.exponents {
num *= p - 1
denom *= p
}
return num * (n / denom)
}
// TotativeDigits returns a slice containing every number between 1 and r
// inclusive that is a totative of r (shares no factors with r).
//
// The returned slice's length is [Totient](r).
func TotativeDigits(r uint32) []uint32 {
digits := make([]uint32, Totient(uint(r)))
totativesFound := 0
for d := uint32(1); d <= r; d++ {
if gcd(d, r) == 1 {
digits[totativesFound] = d
totativesFound++
}
}
return digits
}
|