in reply to help with simplifying program

From your description I take you want (w, x, y, z) such that 0 <= z < y < x < w <= 100 and not w-x == x-y == y-z. In order to achieve this I would parametrize the loops using the differences dwx = w-x, dxy = x-y, dyz = y-z.

use strict; use warnings; my $n = 5; # should be 100 my $c1 = my $c2 = 0; for my $w (3..$n) { for my $dwx (1..$w-2) { my $x = $w - $dwx; for my $dxy (1..$x-1) { my $y = $x - $dxy; for my $dyz (1..$y) { my $z = $y - $dyz; $c1++; next unless $dwx != $dxy or $dxy != $dyz; $c2++; print "$w $x $y $z\n"; } } } } print "Found $c2 out of $c1 combinations\n";
UPDATE: Changes next unless... for better readibility.

Replies are listed 'Best First'.
Re^2: help with simplifying program
by Anonymous Monk on May 24, 2013 at 12:41 UTC
    The unless should read
    next unless $dwx != $dxy and $dxy != $dyz;
    otherwise you will not get all of the valid combinations

      Your proposal gets less combinations! I admit that using unless and != is rather confusing but it is correct. It translates into

      next if $dwx == $dxy and $dxy == $dyz;

      which I should have used in the first place.

        you are correct. I didn't pay enough attention to the unless. That will teach me to be more careful.