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:)


Nah! You're thinking of Simon Templar, originally played (on UKTV) by Roger Moore and later by Ian Ogilvy

Replies are listed 'Best First'.
Re: Re: Re: Re: Solving math equations
by sauoq (Abbot) on Nov 10, 2002 at 21:25 UTC
    No factor of a number can be bigger than the square root of that number (excluding the number itself).

    That's not correct. For example, three is a factor of six but is greater than its square root.

    It is true, however, that you only need to look for factors less than or equal to the square root because, once you have them, the larger factors are easily found by division. For example, once you find that two is a factor of six, simply dividing six by two reveals that three is also a factor.

    Edit: That's "six" not "siz"... Thanks BrowserUk!

    -sauoq
    "My two cents aren't worth a dime.";
    

      Good point. I knew what I was trying to say, but I sure did say it the wrong way.


      Nah! You're thinking of Simon Templar, originally played (on UKTV) by Roger Moore and later by Ian Ogilvy
      Thanks for saying that. When I read the square root node, I twisted that to be what you said. Note to self, actually read the node next time.