-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathpseudo_square_root.pl
More file actions
executable file
·49 lines (34 loc) · 942 Bytes
/
pseudo_square_root.pl
File metadata and controls
executable file
·49 lines (34 loc) · 942 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
42
43
44
45
46
47
48
49
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# Date: 18 August 2017
# https://github.com/trizen
# Find the greatest divisor of `n` that does not exceed the square root of `n`.
# See also:
# https://projecteuler.net/problem=266
use 5.010;
use strict;
use warnings;
use ntheory qw(factor_exp sqrtint vecmax);
sub pseudo_square_root {
my ($n) = @_;
my $limit = sqrtint($n);
my @d = (1);
my @pp = grep { $_->[0] <= $limit } factor_exp($n);
foreach my $pp (@pp) {
my $p = $pp->[0];
my $e = $pp->[1];
my @t;
my $r = 1;
for my $i (1 .. $e) {
$r *= $p;
foreach my $u (@d) {
push(@t, $u * $r) if ($u * $r <= $limit);
}
}
push @d, @t;
}
return vecmax(@d);
}
say pseudo_square_root(479001600); #=> 21600
say pseudo_square_root(6469693230); #=> 79534
say pseudo_square_root(12398712476); #=> 68