in reply to Re: Solving math equations
in thread Solving math equations

Didn't think of it sounding like that, sorry.
#!perl use strict; my $x = 1; my $y = 1; my $total; while ($x*$y ne 133745531) { $x++; $y++ if $x == 137745532; $x = 1 if $x == 137745532; $total = $x*$y; print $total."\n"; } print "$x+$y"; exit;
This is all I have right now. Also, no. This is no homework -- don't know how else I can prove that to you though, except by that above code. That code that I have isn't very effective, and will take quite along time to actually come up with the (any) proper results. I'm currently working on another variation which will be alot faster, but I still have no guarantee it will work (although once it is done, I will post it here).

Replies are listed 'Best First'.
Re: Re: Re: Solving math equations
by BrowserUk (Patriarch) on Nov 10, 2002 at 05:18 UTC

    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
      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.
Re^3: Solving math equations
by kelan (Deacon) on Nov 10, 2002 at 05:34 UTC

    Just a few pointers. You should be using != rather than ne in your while condition. ne is for string comparsion, and != is number comparison. Also, you don't need to increment both numbers. Just increment one, and divide into the total. If there's no remainder, you have one factor, which makes it easy to get the other one.

    On a side note, to everyone else who's reading, I can attest that this isn't homework. It's one of the Geek Challenges; the second one, actually. A couple of the higher ones are quite fun.

    kelan


    Perl6 Grammar Student