Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Comment on

( #3333=superdoc: 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); }

In reply to Pascal-Sierpinski for golfers by brx

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (12)
    As of 2014-10-02 17:54 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      What is your favourite meta-syntactic variable name?














      Results (68 votes), past polls