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

Hi Monks,
Can anyone reduce this to a suitable length for a sig for me?

$a=1; while($a>=-1){ $b=-2; while($b<1){ $c='X'; $d=$b; $e=$a; for(1..50){ $g=$d; $d=$d**2-$e**2+$b; $e=2*$g*$e+$a; $c=' 'if$d**2+$e**2>4; } print$c; $b+=0.05; } print"\n"; $a-=0.1; }

(with line-break and padding removed)

$a=1;while($a>=-1){$b=-2;while($b<1){$c='X';$d=$b;$e=$a; for(1..50){$g=$d;$d=$d**2-$e**2+$b;$e=2*$g*$e+$a;$c=' ' if$d**2+$e**2>4;}print$c;$b+=0.05;}print"\n";$a-=0.1;}
Tom Melly, tom@tomandlu.co.uk

Replies are listed 'Best First'.
Re: Fractal Golf anyone?
by ikegami (Patriarch) on Nov 28, 2006 at 17:46 UTC

    138 (Melly: 165, liverpole: 156)

    map{$a=1-$_/10;map{$d=$a;$e=$b=$_/20-2;$c=X;map{($d,$e)=(2*$d*$e+$a,$ e**2-$d**2+$b);$c=$"if$d**2+$e**2>4}1..50;print$c}0..59;print$/}0..20
    • 'X'X
    • ' '$"
    • "\n"$/
    • {...;}{...}
    • $a=1;while($a>=-1){...;$a-=0.1}
      for(0..20){$a=1-$_/10;...}
      map{$a=1-$_/10;...}0..20

    • $b=-2;while($b<1){...;$b+=0.05}
      for(1..50){$b=$_/20-2;...}
      map{$b=$_/20-2;...}1..50

    • $g=$d;$d=$d**2-$e**2+$b;$e=2*$g*$e+$a;
      $d=($g=$d)**2-$e**2+$b;$e=2*$g*$e+$a;
      ($d,$e)=($d**2-$e**2+$b,2*$d*$e+$a);

    Notes:

    • My loops counters are even more accurate than yours, since they don't accumulate floating point errors. This causes a slight visual difference which can be eliminated by removing the top and bottom rows (map{$a=.9-$_/10;...}0..18).
    • I renamed $e and $d to $d and $e to create a parallel with $a and $b.

    Spaced out:

    map { $a = 1-$_/10; map{ $d = $a; $e = $b = $_/20-2; $c = X; map { ($d, $e) = (2*$d*$e+$a, $e**2-$d**2+$b); $c = $" if $d**2+$e**2 > 4 } 1..50; print $c } 0..59; print $/ } 0..20

      Mmm, nice - I thought at first the list assignment for $d and $e was a false economy, but on closer inspection, it somehow avoids the need for a temporary value for $d - care to explain to an idiot? (I assume that the new value for $d is not used in the evaluation of $e)

      ($d, $e) = (2*$d*$e+$a, $e**2-$d**2+$b);
      Tom Melly, tom@tomandlu.co.uk
        The RHS of the assignment is evaluated before either either $d or $e is changed. The intermediate value are saved on Perl's stack.

      Woo-hoo! - 135! (and 134 if I would settle for '_' instead of $")

      Basically, lose the initial assignment to $c, and handle $c via a conditional operator

      map{ $a=1-$_/10; map{ $d=$a; $e=$b=$_/20-2; map{ ($d,$e)=(2*$d*$e+$a,$e**2-$d**2+$b); $c=$d**2+$e**2>4?$":0 }1..99; print$c }0..59; print$/ }0..20;
      Tom Melly, tom@tomandlu.co.uk

        Well, my last version failed to work properly on linux due to v large FP numbers (due to lack of last when the >4 condition is met).

        Here's a slightly longer version (136) that is linux-safe (and still avoids a last).

        map{$a=1-$_/10;map{$d=$a;$e=$b=$_/20-2;map{($d,$e)=(2*$d*$e+$a,$e**2 -$d**2+$b);$c=$d**2+$e**2>4?$d=8:_}1..50;print$c}0..59;print$/}0..20
        Tom Melly, pm@tomandlu.co.uk
Re: Fractal Golf anyone?
by liverpole (Monsignor) on Nov 28, 2006 at 17:19 UTC
    Hi Melly,

    I can only see small improvements, but of course, in golf, everything counts!

    Change your while loops and for loops to redo blocks and maps, for starters.  Use special variables wherever possible, like $" instead of " ", and $/ instead of "\n".  Get rid of all ; characters at the end of blocks.

    Here's what I came up with so far:

    $a=1;{ $b=-2; { $c='X'; $d=$b; $e=$a; map{ $g=$d; $d=$d**2-$e**2+$b; $e=2*$g*$e+$a; $c=$"if$d**2+$e**2>4 }1..50; print$c; $b+=.05; $b<1&&redo } print$/; $a-=0.1; $a>-1&&redo }

    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

      Another quickie tip: Embed assignments into other expressions to remove redundancy and drop line seperators.

      $b+=.05;$b<1&&redo ($b+=.05)<1&&redo $a-=0.1;$a>-1&&redo ($a-=0.1)>-1&&redo

      Hmm, good tips (I'm new to perl-golf - this particular script was from the good old days when I played pov-golf...)

      Tom Melly, tom@tomandlu.co.uk
Re: Fractal Golf anyone?
by eyepopslikeamosquito (Archbishop) on Nov 28, 2006 at 20:37 UTC
Re: Fractal Golf anyone?
by DrHyde (Prior) on Nov 29, 2006 at 10:36 UTC
    At three lines and well under 70 columns, it's already suitable for a signature.