in reply to Re: Re: Solving math equations
in thread Solving math equations
Okay. The process you describe is called factorisation. There is probably at least one module to do this on CPAN.
However, if you need to do it yourself, then my tip would be to start with the big number an work backwards using modulos rather than with 1 and multiplying.
Tips:
No factor of a number can be bigger than the square root of that number (excluding the number itself)
Update: As rightly pointed out by sauoq, the previous statement is a bunch of dingoes kidney's. The point I was trying to make in my clumsy way was.
In any pair of factors (f1, f2) of N, one of the pair must be less than or equal to the square root of N. It therefore follows that by limiting the iteration of your search to 2..squareroot N, you are guarenteed to find all the smaller values in each pair of factors, and the larger may, by definition, be trivially found by divison of N by the smaller.
The square root can be found using
$sqrt=$number**0.5;
sauoq also pointed out that the line above can also be done using $sqrt = sqrt $number;
and
if ($number % $n == 0) { ## $n is a factor of $number; }
Put that together with a loop and it finds the factors in a blink of an eye.
Have fun:)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Re: Solving math equations
by sauoq (Abbot) on Nov 10, 2002 at 21:25 UTC | |
by BrowserUk (Patriarch) on Nov 10, 2002 at 21:28 UTC | |
by Louis_Wu (Chaplain) on Nov 10, 2002 at 21:32 UTC |