The Sierpinski triangle activity illustrates the fundamental principles of fractals – how a pattern can repeat again and again at different scales and how this complex shape can be formed by simple repetition.
This leaves us with three triangles, each of which has dimensions exactly one-half the dimensions of the original triangle, and area exactly one-fourth of the original area. Also, each remaining triangle is similar to the original.
Connect the midpoints and shade the three triangles that are pointing downward. Iteration 3: Repeat the first and second iterations with a new triangle. Now mark the midpoints of the three sides of each of the nine unshaded triangles. Connect the midpoints and shade/color the nine triangles that are pointing downward.
46 characters, reading from stdin.
(,.~,~[,.~' '$~#,#)^:(<:".1!:1]3)' /\',:'/__\'
\n
always delimits sentences, which made it impossible to fit inside S3 (only 54 characters to play with). S4 is a bit big at 162, so I padded it to fit. Serendipitously, /\
is a legal adverb. ☺
/\ i=:3 /\ /\ %r=:1!:1 /\ /\ t=:] [r+i /\ /\ /\ /\ b=:' /\',:'/__\' /\ /\ i=:1 -".t /\ /\ /\ /\ h=:(' '$ ~#,#),.] /\ /\ /\ /\ s=:( h^:1 ,d=: ,.~) /\ /\ /\ /\ /\ /\ /\ /\ (,,&(10{a.)"1[s^:(-i)b)(1!:2)(4)
Sorry I'm late. This is based on A. Rex's Perl solution:
&I
;for
$x (2
..<>){$E
.= $E
;my$ y;3*
33 +3 ** 3;
s".+"$y.=$n.$&x2
,$ E.
$&.$ E"ge
;; $_ .= $y
}print;; sub I{($
E, $n ,$ F,
$B,$ U)=( $",$ /,qw
(/ \ _ )) ;$ _= $E .$
F.$B.$E.$n.$F.$U.$U.$B};33333333
' /\ /__\ '4/{).+: ;.{ \ ++}%\{.+}%+~ ]}@~(*n*
Golfscript - 47
' /\ /__\ '4/): ;{ +: ;.{ \ ++}%\{.+}%+}@~(*n*
Golfscript - 48
' ': '/\ /__\\'+4/{2 *: ;.{ \ ++}%\{.+}%+}@~(*n*
Golfscript - 51
~' ': '/\ /__\\'+4/\(,{;2 *: ;.{ \ ++}%\{.+}%+}%;n*
Same algorithm as my shorter python ( and ruby ) answer
Golfscript - 78
2\~(?,{-1*}$1: ;{" ":$*. 2base.{[$$+' /\ ']=}%n+@@{[$$+"/__\\"]=}%n .2*^: ;}%
Same algorithm as my longer python solution
This one has significant newlines
2\~(?,{-1*}$1: ;{" ":
*. 2base.{[
2*' /\ ']=}%n+@@{[
2*"/__\\"]=}%n .2*^: ;}%
package main
import(f"fmt";"os";s"strconv";)func main(){var
t=[2]string{" /\\ ","/__\\"};
n,_:=s.Atoi(os.Args[1]);a:=1;N:=a<<uint(n);for
N>0{N-=2;for
k:=0;k<2;k++{for
j:=0;j<N;j++{f.Print(" ")}b:=a;for
b>0{o:=t[k];if
b&1==0{o=" "}f.Print(o);b>>=1}f.Print("\n")}a^=a*2}}
Whitespace is all significant.
Unminized with gofmt sierpinski-3.go | perl -p -e's/\t/ /g'
:
package main
import (
"fmt";
"os";
"strconv";
)
func main() {
var t = [2]string{" /\\ ", "/__\\"};
n, _ := strconv.Atoi(os.Args[1]);
a := 1;
N := a << uint(n);
for N > 0 {
N -= 2;
for k := 0; k < 2; k++ {
for j := 0; j < N; j++ {
fmt.Print(" ")
}
b := a;
for b > 0 {
o := t[k];
if b&1 == 0 {
o = " "
}
fmt.Print(o);
b >>= 1;
}
fmt.Print("\n");
}
a ^= a * 2;
}
}
I got a good hint for Go golf here.
a=" /\ ","/__\\"
j=' '
for n in~-input()*j:j+=j;a=[j+x+j for x in a]+[x*2for x in a]
print"\n".join(a)
Python - 105
a=" /\ ","/__\\"
j=' '
for n in(input()-1)*j:j+=j;a=[j+x+j for x in a]+[x+x for x in a]
print"\n".join(a)
Python - 109
a=" /\ ","/__\\"
for n in range(1,input()):j=' '*2**n;a=[j+x+j for x in a]+[x+x for x in a]
print"\n".join(a)
N=1<<input()
a=1
while N:
N-=2
for s in" /\ ","/__\\":print' '*N+bin(a)[2:].replace('0',' '*4).replace('1',s)
a=a^a*2
This version no longer prints a trailing newline. Only the first newline is necessary:
$_=' /\
/__\\';
for$x(2..<>){
my$y;
$".=$";
s#.+#$y.=$/.$&x2,$".$&.$"#ge;
$_.=$y
}
print
If command-line switches are allowed, then by traditional Perl golf scoring, this is 77+3 strokes (the first newline is literal):
#!perl -p
$\=' /\
/__\\';
$y="",
$".=$",
$\=~s#.+#$y.=$/.$&x2,$".$&.$"#ge,
$\.=$y
for 2..$_
Please feel free to edit my answer if you find an improvement.
Using tail recursion:
(%)=zipWith(++)
p=" ":p
g t _ 1=t
g t s(n+1)=g(s%t%s++t%t)(s%s)n
main=interact$unlines.g[" /\\ ","/__\\"]p.read
earlier version, @118 characters:
(%)=zipWith(++)
f 1=[" /\\ ","/__\\"]
f(n+1)=s%t%s++t%t where t=f n;s=replicate(2^n)' ':s
main=interact$unlines.f.read
Using the (justly deprecated!) n+k pattern saved 4 characters.
I like how it comes out halfway readable even in compressed form.
edit:old main
main=do{n<-getLine;putStr$unlines$f$read n}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With