Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Learning math and efficient algorithms using perl

by artist (Parson)
on Sep 06, 2007 at 20:36 UTC ( #637531=note: print w/replies, xml ) Need Help??


in reply to Learning math and efficient algorithms using perl

You can improve the speed of your code with:
sub get_nof2 { return scalar @{ divisors(+shift) }; } { my $D; sub divisors { my $n = shift; foreach my $d ( 2 .. sqrt($n) ) { unless ( $n % $d ) { my $div = $n / $d; unless ( defined $D->{$div} ) { $D->{$div} = divisors($div); } return $D->{$n} = [ uniq( @{ $D->{$div} }, map { $d * $_ } @{ $D->{$div} } + ) ]; } } return $D->{$n} = [ 1, $n ]; }
--Artist

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2022-12-05 20:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?