Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Finding all divisors of an integer

by Limbic~Region (Chancellor)
on Jul 03, 2004 at 14:05 UTC ( [id://371579]=note: print w/replies, xml ) Need Help??


in reply to Finding all divisors of an integer

chiburashka,
My solution will likely not win the "fastest way" solution. There are likely CPAN modules that do this as well. Usually when someone wants the fastest way to do something - they are taking the wrong approach or using the wrong language. Perhaps if you don't get the answers you like you should expand on the overall picture.
#!/usr/bin/perl use strict; use warnings; my @factors = Factor_It( 20 ); print "$_\n" for @factors; sub Factor_It { my $number = shift; my @factors; for ( 2 .. int $number / 2 ) { push @factors, $_ unless $number % $_; } return (1, @factors, $number); }
Of course, you will also want to add some checks to ensure your input is a positive whole integer greater than 1.

Cheers - L~R

It looks like you updated your node after you posted it with the BigInt stuff - probably want a CPAN module with XS code. You can avoid checking even numbers if it is not evenly divisible by 2 on the first check

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-16 13:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found