-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdivisors_of_factorial_in_range_iterator.sf
More file actions
50 lines (36 loc) · 1 KB
/
Copy pathdivisors_of_factorial_in_range_iterator.sf
File metadata and controls
50 lines (36 loc) · 1 KB
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
#!/usr/bin/ruby
# Daniel "Trizen" Șuteu
# Date: 18 December 2018
# https://github.com/trizen
# Generate the divisors of n! in a given range, using a closure iterator.
# See also:
# https://en.wikipedia.org/wiki/Smooth_number
func divisors_of_factorial_iterator (f, low, high) {
var P = f.primes.map {|p| [p, f.factorial_power(p)] }
var s = P.len.of { [1] }
func {
var n = 0
while (n < low) {
n = s.map{ _[0] }.min
for k in (^P) {
s[k].shift if (s[k][0] == n)
var q = P[k][0]
break if (n.valuation(q) >= P[k][1])
s[k] << n*q
}
}
return nil if (n > high)
return n
}
}
var n = 12
var low = 10**4
var high = 10**6
var iter = divisors_of_factorial_iterator(n, low, high)
var sum = 0
while (iter()) {|v|
sum += v
}
say "Sum of divisors of #{n}! between #{low} and #{high} = #{sum}"
__END__
Sum of divisors of 12! between 10000 and 1000000 = 64006347