in reply to Solving math equations

This sounds distinctly like homework, which you will not get a good response to here if you are asking us to do it for you.

If, as you say, you have been at this for hours, then you must have some code showing what you have tried so far. If you post that, and an explanation of what problems you are having with your code, people may be prepared to point you in the right direction.


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: Solving math equations
by FireBird34 (Pilgrim) on Nov 10, 2002 at 05:00 UTC
    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).

      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.";
        

      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