Here is a simple version of the code. It just enumerates the divisors and binary-searches them:
#! /usr/bin/perl
use strict;
use Math::Pari qw(:int factorint sqrtint divisors);
for (@ARGV) {
my $N = $_;
my $a = factorint($N);
my $sqrt = sqrtint($N);
my $div = divisors($a);
my $len = Math::Pari::matsize($div)->[1];
my $low = 0;
my $high = $len - 1;
my $mid;
while (1) {
$mid = ($low + $high) >> 1;
last unless $low < $high;
if ($div->[$mid] > $sqrt) {
$high = $mid - 1;
} else {
last if $low == $mid;
$low = $mid;
}
}
print "Result for $N is ",$div->[$mid],"\n";
}