Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Finding divisors from factors

by roboticus (Chancellor)
on Oct 09, 2014 at 00:22 UTC ( [id://1103246]=note: print w/replies, xml ) Need Help??


in reply to Finding divisors from factors

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1103246]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-19 07:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found