sub divisors { my($n,@factors) = @_; my $sqrtn = int(sqrt($n)); my %all_factors; foreach my $f ( grep { $_ <= $sqrtn } @factors) { my @to_add = grep { $_ <= $sqrtn } map { $f * $_ } keys %all_factors; undef @all_factors{ $f, @to_add }; } undef $all_factors{1}; my @d = sort {$a<=>$b} keys %all_factors; @d, map { $_*$_ == $n ? () : int($n/$_) } reverse @d; }