Sierpinski triangle: Difference between revisions

(13 intermediate revisions by 7 users not shown)
Line 905:
 
{{trans|BASH (feat. sed & tr)}}
{{works with|Bash|3.2.57}}
{{works with|Bash|5.2.9}}
 
<syntaxhighlight lang="bash">
Line 2,579 ⟶ 2,581:
: sierpinski ( n -- )
[ { "*" } " " ] dip (sierpinski) print ;</syntaxhighlight>
 
A more idiomatic version taking advantage of the '''''with''''', '''''each-integer''''', and '''''?''''' combinator as well as leveraging the looping combinator '''''each-integer'''''.
<syntaxhighlight lang="factor">USING: command-line io io.streams.string kernel math math.parser
namespaces sequences ;
IN: sierpinski
 
: plot ( i j -- )
bitand zero? "* " " " ? write ;
 
: pad ( n -- )
1 - [ " " write ] times ;
 
: plot-row ( n -- )
dup 1 + [ tuck - plot ] with each-integer ;
 
: sierpinski ( n -- )
dup '[ _ over - pad plot-row nl ] each-integer ;</syntaxhighlight>
 
=={{header|FALSE}}==
{{incorrect|FALSE|DifferentRuns resultscorrectly with different interpreters (like http://morphett.info/false/false.html andin http://www.quirkster.com/iano/js/false-js.html}}.
Requires the pick character to be substituted with 'O' in the portable interpreter linked-to from https://strlen.com/false-language/.
<syntaxhighlight lang="false">[[$][$1&["*"]?$~1&[" "]?2/]#%"
<syntaxhighlight lang="false">{ print spaces; in:n }
"]s: { stars }
[[$0>][" " 1-]#%]w:
[$@$@|@@&~&]x: { xor }
 
[1\[$][1-\2*\]#%]e: { 2^n }
{ left shift; in:x,y out:x<<y }
[e;!1\[$][\$s;!$2*x;!\1-]#%%]t:
[[$0>][\2*\ 1-]#%]l:
4t;!</syntaxhighlight>
 
1 4 l;! { SIZE = 1<<4 }
 
$ { y = SIZE }
[$0>] { y > 0 }
[1-
$w;!
1ø { x = SIZE }
[$0>]
[1-
1ø$2ø\-&0= { !((x - y) & y) }
$ ["* "]?
~ [" "]?
]#%
10,
]#%%</syntaxhighlight>
 
=={{header|FOCAL}}==
Line 3,701 ⟶ 3,736:
* * * * * * * * * * * * * * * *
</pre>
 
=={{header|Lambdatalk}}==
===1) define===
<syntaxhighlight lang="scheme">
{def sierp
{def sierp.r
{lambda {:order :length :angle}
{if {= :order 0}
then M:length // move :length
else {sierp.r {- :order 1} // recurse
{/ :length 2}
{- :angle}}
T:angle // turn :angle
{sierp.r {- :order 1} // recurse
{/ :length 2}
{+ :angle}}
T:angle // turn :angle
{sierp.r {- :order 1} // recurse
{/ :length 2}
{- :angle}}
}}}
{lambda {:order :length}
{if {= {% :order 2} 0} // if :order is even
then {sierp.r :order :length 60} // recurse with 60°
else T60 // else turn 60°
{sierp.r :order :length -60} // recurse with -60°
}}}
-> sierp
</syntaxhighlight>
===2) draw===
Four curves drawn in 50ms on a PowerBookPro. using the turtle primitive.
<syntaxhighlight lang="scheme">
 
{svg {@ width="580" height="580" style="box-shadow:0 0 8px #000;"}
{polyline {@ points="{turtle 50 5 0 {sierp 1 570}}"
stroke="#ccc" fill="transparent" stroke-width="7"}}
{polyline {@ points="{turtle 50 5 0 {sierp 3 570}}"
stroke="#8ff" fill="transparent" stroke-width="5"}}
{polyline {@ points="{turtle 50 5 0 {sierp 5 570}}"
stroke="#f88" fill="transparent" stroke-width="3"}}
{polyline {@ points="{turtle 50 5 0 {sierp 7 570}}"
stroke="#000" fill="transparent" stroke-width="1"}}
</syntaxhighlight>
===3) output===
See http://lambdaway.free.fr/lambdawalks/?view=sierpinsky
 
 
=={{header|Liberty BASIC}}==
Line 5,339 ⟶ 5,420:
writeln(join(sierpinski(4), "\n"));
end func;</syntaxhighlight>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program sierpinski;
const size = 4;
 
loop for i in [0..size*4-1] do
putchar(' ' * (size*4-1-i));
c := 1;
loop for j in [0..i] do
putchar(if c mod 2=0 then " " else " *" end);
c := c*(i-j) div (j+1);
end loop;
print;
end loop;
end program;</syntaxhighlight>
{{out}}
<pre> *
* *
* *
* * * *
* *
* * * *
* * * *
* * * * * * * *
* *
* * * *
* * * *
* * * * * * * *
* * * *
* * * * * * * *
* * * * * * * *
* * * * * * * * * * * * * * * *</pre>
 
=={{header|Sidef}}==
Line 5,574 ⟶ 5,687:
* * * * * * * * * * * * * * * *
</pre>
 
=={{header|Uxntal}}==
<syntaxhighlight lang="Uxntal">( uxncli sierpinski.rom )
 
|100 @on-reset ( -> )
 
#10 STHk #01 SUB
&ver ( -- )
DUP
#00 EQUk ?{
&pad ( -- )
#2018 DEO
INC GTHk ?&pad
} POP
#00
&fill
ANDk #202a ROT ?{ SWP } POP #18 DEO
#2018 DEO
INC ADDk STHkr LTH ?&fill
POP2
#0a18 DEO
#01 SUB DUP #ff NEQ ?&ver
POP POPr
 
BRK</syntaxhighlight>
 
The triangle size is given by the first instruction <code>#10</code>, representing the number of rows to print.
 
=={{header|VBA}}==
Line 5,808 ⟶ 5,948:
=={{header|Wren}}==
{{trans|C}}
<syntaxhighlight lang="ecmascriptwren">var size = 1 << 4
for (y in size-1..0) {
System.write(" " * y)
Line 5,949 ⟶ 6,089:
next
</syntaxhighlight>
 
=={{header|Zig}}==
{{trans|C}}
<syntaxhighlight lang="zig">const std = @import("std");
 
pub fn main() !void {
const stdout = std.io.getStdOut().writer();
const size: u16 = 1 << 4;
var y = size;
while (y > 0) {
y -= 1;
for (0..y) |_| try stdout.writeByte(' ');
for (0..size - y) |x| try stdout.writeAll(if (x & y != 0) " " else "* ");
try stdout.writeByte('\n');
}
}</syntaxhighlight>
 
===Automaton===
{{trans|C}}
{{works with|Zig|0.11.0dev}}
<syntaxhighlight lang="zig">const std = @import("std");
const Allocator = std.mem.Allocator;
 
pub fn main() !void {
const stdout = std.io.getStdOut().writer();
 
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = arena.allocator();
 
try sierpinski_triangle(allocator, stdout, 4);
}</syntaxhighlight><syntaxhighlight lang="zig">inline fn truth(x: u8) bool {
return x == '*';
}</syntaxhighlight><syntaxhighlight lang="zig">fn rule_90(allocator: Allocator, evstr: []u8) !void {
var cp = try allocator.dupe(u8, evstr);
defer allocator.free(cp); // free does "free" for last node in arena
 
for (evstr, 0..) |*evptr, i| {
var s = [2]bool{
if (i == 0) false else truth(cp[i - 1]),
if (i + 1 == evstr.len) false else truth(cp[i + 1]),
};
evptr.* = if ((s[0] and !s[1]) or (!s[0] and s[1])) '*' else ' ';
}
}</syntaxhighlight><syntaxhighlight lang="zig">fn sierpinski_triangle(allocator: Allocator, writer: anytype, n: u8) !void {
const len = std.math.shl(usize, 1, n + 1);
 
var b = try allocator.alloc(u8, len);
defer allocator.free(b);
for (b) |*ptr| ptr.* = ' ';
 
b[len >> 1] = '*';
 
try writer.print("{s}\n", .{b});
 
for (0..len / 2 - 1) |_| {
try rule_90(allocator, b);
try writer.print("{s}\n", .{b});
}
}</syntaxhighlight>
 
=={{header|zkl}}==
6

edits