-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunitary_powerfree_divisors.sf
51 lines (42 loc) · 1.65 KB
/
unitary_powerfree_divisors.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
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/ruby
# A simple algorithm for generating the unitary k-powerfree divisors of a given number n.
# See also:
# https://oeis.org/A092261
# https://mathworld.wolfram.com/Powerfree.html
# https://en.wikipedia.org/wiki/Unitary_divisor
func unitary_powerfree_divisors(n, k=2) {
var d = [1]
for p,e in (n.factor_exp) {
e < k || next
var r = p**e
d << d.map {|t| t*r }...
}
return d.sort
}
for n in (1..20) {
say "unitary squarefree divisors of #{n} = #{unitary_powerfree_divisors(n, 2)}"
assert_eq(unitary_powerfree_divisors(n, 2), n.udivisors.grep { .is_powerfree(2) })
assert_eq(unitary_powerfree_divisors(n, 3), n.udivisors.grep { .is_powerfree(3) })
assert_eq(unitary_powerfree_divisors(n, 4), n.udivisors.grep { .is_powerfree(4) })
}
__END__
unitary squarefree divisors of 1 = [1]
unitary squarefree divisors of 2 = [1, 2]
unitary squarefree divisors of 3 = [1, 3]
unitary squarefree divisors of 4 = [1]
unitary squarefree divisors of 5 = [1, 5]
unitary squarefree divisors of 6 = [1, 2, 3, 6]
unitary squarefree divisors of 7 = [1, 7]
unitary squarefree divisors of 8 = [1]
unitary squarefree divisors of 9 = [1]
unitary squarefree divisors of 10 = [1, 2, 5, 10]
unitary squarefree divisors of 11 = [1, 11]
unitary squarefree divisors of 12 = [1, 3]
unitary squarefree divisors of 13 = [1, 13]
unitary squarefree divisors of 14 = [1, 2, 7, 14]
unitary squarefree divisors of 15 = [1, 3, 5, 15]
unitary squarefree divisors of 16 = [1]
unitary squarefree divisors of 17 = [1, 17]
unitary squarefree divisors of 18 = [1, 2]
unitary squarefree divisors of 19 = [1, 19]
unitary squarefree divisors of 20 = [1, 5]