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
|
#!/usr/bin/ruby
#
# See ../README.md
#
#
# Run as: ruby ch-1.rb [plain | compute]
#
PLAIN = 0
COMPUTE = 1
COUNT = 20
def divisor_sum (n)
sum = 0
for i in 2 .. n / 2
if n % i == 0
then sum += i
end
end
return sum
end
type = PLAIN
if ARGV . length > 0 && ARGV[0] == "compute"
then type = COMPUTE
end
if type == PLAIN
then puts ("0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21");
end
if type == COMPUTE
then for n in 1 .. COUNT
if n > 1
then print (", ")
end
print divisor_sum (n)
end
puts ("")
end
|