forked from trizen/perl-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmallest_number_with_n_divisors.pl
More file actions
41 lines (29 loc) · 990 Bytes
/
smallest_number_with_n_divisors.pl
File metadata and controls
41 lines (29 loc) · 990 Bytes
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
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# Date: 15 May 2021
# https://github.com/trizen
# Generate the smallest number that has exactly n divisors.
# See also:
# https://oeis.org/A005179 -- Smallest number with exactly n divisors.
use 5.020;
use warnings;
use experimental qw(signatures);
use ntheory qw(nth_prime);
use Math::AnyNum qw(:overload);
sub smallest_number_with_n_divisors ($threshold, $least_solution = Inf, $k = 1, $max_a = Inf, $sigma0 = 1, $n = 1) {
if ($sigma0 == $threshold) {
return $n;
}
if ($sigma0 > $threshold) {
return $least_solution;
}
my $p = nth_prime($k);
for (my $a = 1 ; $a <= $max_a ; ++$a) {
$n *= $p;
last if ($n > $least_solution);
$least_solution = __SUB__->($threshold, $least_solution, $k + 1, $a, $sigma0 * ($a + 1), $n);
}
return $least_solution;
}
say smallest_number_with_n_divisors(60); #=> 5040
say smallest_number_with_n_divisors(1000); #=> 810810000