danaj:
For my version, I took the powerset version, used tail recursion to turn it into a loop, and used a hash to remove duplicates continuously. I don't expect that it's all that fast with the extra hash operations and a sort at the end, but it's simple enough:
#!/usr/bin/perl
use strict;
use warnings;
my @all_factors = (1, 2, 2, 3, 5, 11, 277412413);
my @divisors = build_factors(@all_factors);
print join(", ", @divisors),"\n";
sub build_factors {
my @orig = @_;
my %new = (1=>0);
while (my $factor = shift @orig) {
@new{map{$factor*$_} keys %new}=0;
}
return sort {$a<=>$b} keys %new;
}
...roboticus
When your only tool is a hammer, all problems look like your thumb.