Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Finding all divisors of an integer

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


in reply to Finding all divisors of an integer

For small numbers, this should be straightforward ...

@divisors = grep { $number % $_ == 0 } 1 .. $number;

I would not worry about optimizing this, unless you need it for very many or very big numbers ... but then it would be something like this:

# First, the lower half of the divisors: @divisors = grep { $number % $_ == 0 } 1 .. sqrt($number); # Then, the upper half: push @divisors, map {$number == $_*$_ ? () : $number/$_} reverse @divi +sors;

(Sorry, I missed the Math::BigInt stuff ... but I guess the algorithm above can be adapted to that case as well?)

Update: Most of my code could benefit from comments. I added comments to the optimized version. Never code as if the guy who ends up maintaining your code can always maintain a proper caffeine intake.

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

Replies are listed 'Best First'.
Re^2: Finding all divisors of an integer
by Limbic~Region (Chancellor) on Jul 03, 2004 at 14:10 UTC
    Sidhekin,
    I would not worry about optimizing this, unless you need it for very many or very big numbers
    Indeed, the node was updated after you (and I) already started posting. I would say that you only need to go to $number / 2 though.

    @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.

    Cheers - L~R

    People should not be allowed to touch keyboards early in the morning (weekend) without proper caffeine intake. I completely missed the push in the second line of code

      @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!

      A reply falls below the community's threshold of quality. You may see it by logging in.
    A reply falls below the community's threshold of quality. You may see it by logging in.
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://371578]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-20 01:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found