Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Pascal-Sierpinski for golfers

by brx (Pilgrim)
on Nov 29, 2011 at 09:07 UTC ( #940559=CUFP: print w/ replies, xml ) Need Help??

Pascal's triangle

First oneliner to write Pascal's triangle (see: Pascal's_triangle )
perl -le '$_=1;print,s/(\d+) ?/+$1 $1/g,s/\S+/$&/eeg until$a++>15'
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1
1 11 55 165 330 462 462 330 165 55 11 1
1 12 66 220 495 792 924 792 495 220 66 12 1
1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1
1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1
1 15 105 455 1365 3003 5005 6435 6435 5005 3003 1365 455 105 15 1

or with bigint
perl -Mbigint -le '$_=1;print,s/(\d+) ?/+$1 $1/g,s/\S+/$&/eeg until$a+ ++>256'

Sierpinski's triangle

Second oneliner to draw Sierpinski (approx. : "The pattern obtained by coloring only the odd numbers in Pascal's triangle closely resembles the fractal called the Sierpinski triangle." -- see: http://en.wikipedia.org/wiki/Pascal%27s_triangle#Overall_patterns_and_properties )
perl -le '$_=o;print,s/./$&$&/g,s/(?!^)(.)(.)/$1^$2^_/eg until$a++>80'
Same thing with any two ascii characters (params)
perl -le '$_=pop;print,s/./$&$&/g,s/(?!^)(.)(.)/$1^$2^$ARGV[0]/eg unti +l$b++>80' _ o
o
oo
o_o
oooo
o___o
oo__oo
o_o_o_o
oooooooo
o_______o
oo______oo
o_o_____o_o
oooo____oooo
o___o___o___o
oo__oo__oo__oo
o_o_o_o_o_o_o_o
oooooooooooooooo
o_______________o
oo______________oo
o_o_____________o_o
oooo____________oooo
o___o___________o___o
oo__oo__________oo__oo
o_o_o_o_________o_o_o_o
oooooooo________oooooooo
o_______o_______o_______o
oo______oo______oo______oo
o_o_____o_o_____o_o_____o_o
oooo____oooo____oooo____oooo
o___o___o___o___o___o___o___o
oo__oo__oo__oo__oo__oo__oo__oo
o_o_o_o_o_o_o_o_o_o_o_o_o_o_o_o
oooooooooooooooooooooooooooooooo
o_______________________________o
oo______________________________oo
o_o_____________________________o_o
oooo____________________________oooo
o___o___________________________o___o
oo__oo__________________________oo__oo
o_o_o_o_________________________o_o_o_o
oooooooo________________________oooooooo
o_______o_______________________o_______o
oo______oo______________________oo______oo
o_o_____o_o_____________________o_o_____o_o
oooo____oooo____________________oooo____oooo
o___o___o___o___________________o___o___o___o
oo__oo__oo__oo__________________oo__oo__oo__oo
o_o_o_o_o_o_o_o_________________o_o_o_o_o_o_o_o
oooooooooooooooo________________oooooooooooooooo
o_______________o_______________o_______________o
oo______________oo______________oo______________oo
o_o_____________o_o_____________o_o_____________o_o
oooo____________oooo____________oooo____________oooo
o___o___________o___o___________o___o___________o___o
oo__oo__________oo__oo__________oo__oo__________oo__oo
o_o_o_o_________o_o_o_o_________o_o_o_o_________o_o_o_o
oooooooo________oooooooo________oooooooo________oooooooo
o_______o_______o_______o_______o_______o_______o_______o
oo______oo______oo______oo______oo______oo______oo______oo
o_o_____o_o_____o_o_____o_o_____o_o_____o_o_____o_o_____o_o
oooo____oooo____oooo____oooo____oooo____oooo____oooo____oooo
o___o___o___o___o___o___o___o___o___o___o___o___o___o___o___o
oo__oo__oo__oo__oo__oo__oo__oo__oo__oo__oo__oo__oo__oo__oo__oo
o_o_o_o_o_o_o_o_o_o_o_o_o_o_o_o_o_o_o_o_o_o_o_o_o_o_o_o_o_o_o_o
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
o_______________________________________________________________o
oo______________________________________________________________oo
o_o_____________________________________________________________o_o
oooo____________________________________________________________oooo
o___o___________________________________________________________o___o
oo__oo__________________________________________________________oo__oo
o_o_o_o_________________________________________________________o_o_o_o
oooooooo________________________________________________________oooooooo
o_______o_______________________________________________________o_______o
oo______oo______________________________________________________oo______oo
o_o_____o_o_____________________________________________________o_o_____o_o
oooo____oooo____________________________________________________oooo____oooo
o___o___o___o___________________________________________________o___o___o___o
oo__oo__oo__oo__________________________________________________oo__oo__oo__oo
o_o_o_o_o_o_o_o_________________________________________________o_o_o_o_o_o_o_o
oooooooooooooooo________________________________________________oooooooooooooooo
o_______________o_______________________________________________o_______________o
If you don't like oneliners and terminal, here is the Sierpinski approx. with Image::Magick
use strict; use Image::Magick; my $size = 256; my $psci; my $rang=$size-1; my $image=Image::Magick->new(size=>2*$size.'x'.2*$size); $image->ReadImage('canvas:white'); $psci->[0][0] = 1; for my $i (1 .. $rang) { $psci->[$i][0]=1; for my $j (1 .. ($i-1)) { $psci->[$i][$j]= ($psci->[$i-1][$j-1]) ^ ($psci->[$i-1][$j]); } $psci->[$i][$i]=1; } for my $i (0 .. $rang) { for my $j (0 .. $i) { if ($psci->[$i][$j]==1) { my $color = [&col()]; # random color my $decalage = $rang-$i; #offset $image->SetPixel(x=>$decalage + 2*$j ,y=>2*$i,color=>$colo +r); $image->SetPixel(x=>$decalage + 2*$j,y=>2*$i+1,color=>$col +or); $image->SetPixel(x=>$decalage + 2*$j+1,y=>2*$i,color=>$co +lor); $image->SetPixel(x=>$decalage + 2*$j+1,y=>2*$i+1,color=>$ +color); }; } } my $err = $image->Write("Sierpinski${size}.png"); warn "$err" if "$err"; sub col { #random RGB color around (206,190,68) return ((256-100*rand)/256,(246-100*rand)/256,(118-100*rand)/256); }

Comment on Pascal-Sierpinski for golfers
Select or Download Code
Replies are listed 'Best First'.
Re: Pascal-Sierpinski for golfers
by eyepopslikeamosquito (Chancellor) on Nov 29, 2011 at 20:48 UTC

    A long time ago, I accidentally wrote this little Sierpinski triangle generator obfu, sier.pl, while playing golf IIRC:

    #!/usr/bin/perl -l s--@{[(gE^Ge)=~/[^g^e]/g]}[g^e]x((!!+~~g^e^g^e)<<pop).!gE-ge, s-[^ge^ge]-s,,,,s,@{[(g^';').(e^'?')]},(G^'/').(E^'|')^Ge,ge, print,s,(?<=/[^g^e])[^g^e][^g^e],$&^(G^'/').(E^'|')^gE,ge-ge
    Running, for example:
    perl sier.pl 5
    displays:
    /\ /\/\ /\ /\ /\/\/\/\ /\ /\ /\/\ /\/\ /\ /\ /\ /\ /\/\/\/\/\/\/\/\ /\ /\ /\/\ /\/\ /\ /\ /\ /\ /\/\/\/\ /\/\/\/\ /\ /\ /\ /\ /\/\ /\/\ /\/\ /\/\ /\ /\ /\ /\ /\ /\ /\ /\ /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ /\ /\ /\/\ /\/\ /\ /\ /\ /\ /\/\/\/\ /\/\/\/\ /\ /\ /\ /\ /\/\ /\/\ /\/\ /\/\ /\ /\ /\ /\ /\ /\ /\ /\ /\/\/\/\/\/\/\/\ /\/\/\/\/\/\/\/\ /\ /\ /\ /\ /\/\ /\/\ /\/\ /\/\ /\ /\ /\ /\ /\ /\ /\ /\ /\/\/\/\ /\/\/\/\ /\/\/\/\ /\/\/\/\ /\ /\ /\ /\ /\ /\ /\ /\ /\/\ /\/\ /\/\ /\/\ /\/\ /\/\ /\/\ /\/\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\

Re: Pascal-Sierpinski for golfers
by hbm (Hermit) on Nov 29, 2011 at 15:12 UTC

    Golf? Here's a first pass:

    perl -le'$_=1;print,s/(\d+) ?/+$1 $1/g,s/\S+/$&/eeg until$a++>15' perl -le'$_=1;print,s/(\d+) ?/+$1 $1/g,s/\S+/$&/eeguntil/560/'

      perl -le'$_=1;print,s/(\d+) ?/+$1 $1/g,s/\S+/$&/eeguntil/560/'
      Shaving 12 strokes (down to 41 bytes):
      s/^|\d+/$&+$'||1/eg,print,s// /for($/)x16
      (Can run this from a file or via perl -e).

      Update: if you are willing to allow a trailing space on each line that can be reduced to 38 bytes:

      s/^/1 /,print,s/\d+/$&+$'/egfor($/)x16

      Using perl -l, five more:

      #!perl -l s/^/1 /,print,s/\d+/$&+$'/eguntil/08/ s/^/1 /,print,s/\d+/$&+$'/eg^16&&do$0 1while s/^/1 /,print,s/\d+/$&+$'/eg^16 {s/^/1 /,print,s/\d+/$&+$'/eg^16&&redo} eval's/^/1 /;print;s/\d+/$&+$\'/eg;'x16

        I really appreciate two tricks:

        • s// /
          I understand this, and have used the empty expression before with m//, but never thought to try it with s///.
           
        • for($/)x16
          A slick way of getting the newline (vis-à-vis -l print until...)!
          Changing the loop to for($_)x16 is informative.
          And, since a bare print prints both $_ and $/, I found it rather unobvious which variable holds the strings.
        Nice, thank you ! With the same idea for Sierpinski :
        #!/usr/bin/perl -l print,s/^/_/,s/./$'& z^$&^Z/eg,y/5/o/for($a=o)x80

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://940559]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (10)
As of 2015-07-28 09:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (254 votes), past polls