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

Re^3: Finding all divisors of an integer

by Sidhekin (Priest)
on Jul 03, 2004 at 14:16 UTC ( [id://371586]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Finding all divisors of an integer
in thread Finding all divisors of an integer

@divisors = grep { $number % $_ == 0 } 1 .. sqrt($number);
I think you are thinking of prime factorization here. You can stop there to determine primeness but not to determine all factors.

You somehow miss the second line of my optimized solution. It adds the other half of the divisors. :-)

push @divisors, map {$number == $_*$_ ? () : $number/$_} reverse @divi +sors;

For every divisor above the square root, there is a corresponding divisor below the square root. Symmetrical optimization. :-)

print "Just another Perl ${\(trickster and hacker)},"
The Sidhekin proves Sidhe did it!

Replies are listed 'Best First'.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-26 00:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found