-
Notifications
You must be signed in to change notification settings - Fork 34
/
alexandrian_integers.pl
executable file
·74 lines (58 loc) · 1.24 KB
/
alexandrian_integers.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/perl
# Author: Daniel "Trizen" Șuteu
# Date: 18 August 2016
# License: GPLv3
# Website: https://github.com/trizen
# Get the nth Alexandrian integer.
# See also: https://oeis.org/A147811
# https://projecteuler.net/problem=221
use 5.010;
use strict;
use warnings;
use ntheory qw(divisors);
sub nth_alexandrian {
my ($nth) = @_;
return 120 if $nth == 3; # hmm...
my %nums;
my $count = 0;
my $prev = 6;
OUT: foreach my $n (1 .. $nth) {
foreach my $d (divisors($n * $n + 1)) {
my $q = $n + $d;
my $r = ($n + ($n * $n + 1) / $d);
last if $q > $r;
my $A = $n * $q * $r;
--$count if ($A < $prev);
if (not exists $nums{$A}) {
undef $nums{$A};
$prev = $A;
last OUT if (++$count == $nth);
}
}
}
+(sort { $a <=> $b } keys %nums)[$nth - 1];
}
foreach my $n (1 .. 20) {
say "A($n) = ", nth_alexandrian($n);
}
__END__
A(1) = 6
A(2) = 42
A(3) = 120
A(4) = 156
A(5) = 420
A(6) = 630
A(7) = 930
A(8) = 1428
A(9) = 1806
A(10) = 2016
A(11) = 2184
A(12) = 3192
A(13) = 4950
A(14) = 5256
A(15) = 8190
A(16) = 8364
A(17) = 8970
A(18) = 10296
A(19) = 10998
A(20) = 12210