-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconditional_euler_totient_function.sf
39 lines (31 loc) · 1.11 KB
/
conditional_euler_totient_function.sf
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
#!/usr/bin/ruby
# Daniel "Trizen" Șuteu
# Date: 04 July 2018
# https://github.com/trizen
# Find the number of k = 1..n for which GCD(n,k) satisfies a certain condition (e.g.:
# GCD(n,k) is a prime number), using the divisors of `n` and the Euler totient function.
# See also:
# https://oeis.org/A117494 -- Number of k = 1..n for which GCD(n, k) is a prime
# https://oeis.org/A116512 -- Number of k = 1..n for which GCD(n, k) is a power of a prime
# https://oeis.org/A206369 -- Number of k = 1..n for which GCD(n, k) is a square
# https://oeis.org/A078429 -- Number of k = 1..n for which GCD(n, k) is a cube
# https://oeis.org/A063658 -- Number of k = 1..n for which GCD(n, k) is divisible by a square greater than 1
func foo(n, condition) { # slow
1..n -> count {|k|
condition(gcd(n,k))
}
}
func bar(n, condition) { # fast
n.divisors.sum {|d|
condition(d) ? euler_phi(n/d) : 0
}
}
func condition(d) {
d.is_prime
}
say 25.of {|n| foo(n, condition) }
say 25.of {|n| bar(n, condition) }
assert_eq(
100.of {|n| foo(n, condition) },
100.of {|n| bar(n, condition) }
)