-
Notifications
You must be signed in to change notification settings - Fork 319
/
ch-1.pl
executable file
·49 lines (32 loc) · 936 Bytes
/
ch-1.pl
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
#!/usr/bin/perl -s
use v5.16;
use Test2::V0;
use Math::Prime::Util 'divisor_sum';
use experimental 'signatures';
our $examples;
run_tests() if $examples; # does not return
die <<EOS unless @ARGV == 1;
usage: $0 [-examples] [n]
-examples
run the examples from the challenge
n
print the first n Chowla numbers
EOS
### Input and Output
say join ', ', map chowla($_), 1 .. $ARGV[0];
### Implementation
# Return the n-th Chowla number as the sum of n's nontrivial divisors.
# The trivial divisors are 1 and n, which need to be subtracted from the
# sum of all divisors. For n = 1 these two numbers coincide and thus
# only one subtrahend shall be applied.
sub chowla ($n) {
divisor_sum($n) - $n - ($n > 1);
}
### Examples and tests
sub run_tests {
my $n;
is chowla(++$n), $_, "chowla($n) = $_" for
(0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21);
done_testing;
exit;
}