Sierpinski carpet: Difference between revisions
Content deleted Content added
No edit summary |
→{{header|UNIX Shell}}: Add alternate implementation. |
||
Line 5,980:
=={{header|UNIX Shell}}==
===Bash + paste(1)===
{{works with|Bash}}
Doesn't pretend to be efficient.
Line 6,027 ⟶ 6,028:
# # # # # # # # # # # # # # # # # # # # # # # # # # #
</pre>
===Bash/Ksh/Zsh + dc(1)===
Alternate version using the 'corresponding 1s in base 3' rule, with help from dc(1):
{{works with|Bourne-Again SHell}}
{{works with|Korn Shell}}
{{works with|Zsh}}
<lang sh>sierpinski_carpet() {
typeset -i n=${1:-3}
if (( n < 1 )); then
return 1
fi
typeset -i size x y
typeset x1 y1
(( size = 3 ** n ))
for (( y=0; y<size; ++y )); do
y1=$(dc <<<"$y 3op" | tr 2 0)
for (( x=0; x<size; ++x )); do
x1=$(dc <<<"$x 3op" | tr 2 0)
if (( 2#$x1 & 2#$y1 )); then
printf ' '
else
printf '#'
fi
done
printf '\n'
done
}
sierpinski_carpet 3</lang>
{{Out}}
<pre>###########################
# ## ## ## ## ## ## ## ## #
###########################
### ###### ###### ###
# # # ## # # ## # # #
### ###### ###### ###
###########################
# ## ## ## ## ## ## ## ## #
###########################
######### #########
# ## ## # # ## ## #
######### #########
### ### ### ###
# # # # # # # #
### ### ### ###
######### #########
# ## ## # # ## ## #
######### #########
###########################
# ## ## ## ## ## ## ## ## #
###########################
### ###### ###### ###
# # # ## # # ## # # #
### ###### ###### ###
###########################
# ## ## ## ## ## ## ## ## #
###########################</pre>
=={{header|Ursala}}==
|