Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Oh wise ones, please help :) If I had an equation such as:

x+y+z=17

and I was only permitted to use the numbers 1 through 20, and only 3 numbers at a time, to make this answer, can I possibly get perl to find the question combinations that made up 17?

example output using only nos. 1-20:
combination of using only 3 numbers:
------------------------------------
9+3+5=17
5+5+7=17
8+8+1=17
12+3+2=17

etc...

I've racked my brain for days and I can't clear the cobwebs :-\ Would anyone have any suggestions or pointers on how to do this?

Your wisdom is soooo greatly appreciated, Thank-you

Replies are listed 'Best First'.
Re: Math with Perl, equation combinations:
by Zaxo (Archbishop) on May 10, 2002 at 23:08 UTC

    Here's the straight-ahead dumb way to do it:

    #!/usr/bin/perl -w use strict; my $target=shift; X: for my $x (1 .. 20) { for my $y (1..20) { my $z = $target - $x - $y; next X if $z<1 || $z>20; print "($x,$y,$z)$/"; } }
    Uhhh, this wasn't homework, was it? ;-)

    Update: Remove a bogosity from code. U 2: samtregar, there is Steal This Code.

    After Compline,
    Zaxo

      Uhhh, this wasn't homework, was it? ;-)

      Oh, no, I bet he was just wondering. Desperately wondering.

      We should trace his IP and find his professor...

      -sam

Re: Math with Perl, equation combinations:
by ariels (Curate) on May 11, 2002 at 08:25 UTC
    The straightforward, structured way of doing this in Perl would have to be
    perl -le'for$-(1..15){$==17-$--$_,print"$_+$-+$==17"for($-..16-$-)}'
    This works because _+-==.

    The `<samp>15</samp>' is just there to confuse your professor; it would work equally well with `<samp>289</samp>'.

    Also note that by carefully arranging the columns so the second is increasing, the first going up and jumping down, and the last going down and jumping up, no duplicates are printed.

Re: Math with Perl, equation combinations:
by Anonymous Monk on May 12, 2002 at 07:47 UTC
    I would like to extend some warm thank-you's for answering my question. I know the word "dumb" was used in one of the comments, but I'd like to personally change that to "genius". I would never of come up with any of those solutions, as my perl skills aren't that superb!

    To answer your question, I'm not doing this for homework. It's been a long time since I saw a proffessor :-) I was just doing this personally for myself. What I was orginally *trying* to do was add 7 numbers together but only using one number once in each question string, so let's say for example 8 wouldn't show up twice in a string of 8+8+1 The code would just find all the possible combinations of questions. Ahhh... anyhow, I'm trying to study your codes and trying to figure out how everything is working, and how I can change 3 to 7 numbers to add together and omit the same numbers appearing more than once. It's all very interesting and I should say difficult :-)

    It's hard to express my gratitude via text, but thank-you once again, there are truly many wise ones here at PerlMonks.com