Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,4 +1,5 @@
Produce an ASCII representation of a [[wp:Sierpinski triangle|Sierpinski triangle]] of order N. For example, the Sierpinski triangle of order 4 should look like this:
Produce an ASCII representation of a [[wp:Sierpinski triangle|Sierpinski triangle]] of order N.
For example, the Sierpinski triangle of order 4 should look like this:
<pre>
*
* *
@ -18,4 +19,5 @@ Produce an ASCII representation of a [[wp:Sierpinski triangle|Sierpinski triangl
* * * * * * * * * * * * * * * *
</pre>
See [[Sierpinski triangle/Graphical]] for graphics images of this pattern. See also [[Sierpinski carpet]]
See [[Sierpinski triangle/Graphical]] for graphics images of this pattern.
See also [[Sierpinski carpet]]

View file

@ -0,0 +1,42 @@
(* ****** ****** *)
//
// How to compile:
//
// patscc -DATS_MEMALLOC_LIBC -o sierpinski sierpinski.dats
//
(* ****** ****** *)
//
#include
"share/atspre_staload.hats"
//
(* ****** ****** *)
#define SIZE 16
implement
main0 () =
{
//
var x: int
//
val () =
for (x := SIZE-1; x >= 0; x := x-1)
{
var i: int
val () =
for (i := 0; i < x; i := i+1)
{
val () = print_char(' ')
}
var y: int
val () =
for (y := 0; y + x < SIZE; y := y+1)
{
val y = g0int2uint_int_uint(y)
val x = g0int2uint_int_uint(x)
val () = print_string(if (x land y) != 0 then " " else "* ")
}
val ((*flushed*)) = print_newline()
}
//
} (* end of [main0] *)

View file

@ -1,6 +1,6 @@
import std.stdio, std.algorithm, std.string, std.array;
void main() /*@safe*/ {
import std.stdio, std.algorithm, std.string, std.array;
void main() {
enum level = 4;
auto d = ["*"];
foreach (immutable n; 0 .. level) {
@ -8,5 +8,5 @@ void main() {
d = d.map!(a => sp ~ a ~ sp).array ~
d.map!(a => a ~ " " ~ a).array;
}
d.join("\n").writeln;
d.join('\n').writeln;
}

View file

@ -1,19 +1,14 @@
import std.string;
import std.string, std.range, std.algorithm;
string sierpinski(int n) {
auto parts = ["*"];
auto space = " ";
foreach (i; 0 .. n) {
string[] parts2;
foreach (x; parts)
parts2 ~= space ~ x ~ space;
foreach (x; parts)
parts2 ~= x ~ " " ~ x;
parts = parts2;
space ~= space;
string sierpinski(int level) pure nothrow /*@safe*/ {
auto d = ["*"];
foreach (immutable i; 0 .. level) {
immutable sp = " ".replicate(2 ^^ i);
d = d.map!(a => sp ~ a ~ sp).array ~
d.map!(a => a ~ " " ~ a).array;
}
return parts.join("\n");
return d.join('\n');
}
pragma(msg, sierpinski(4));
pragma(msg, 4.sierpinski);
void main() {}

View file

@ -0,0 +1,17 @@
void showSierpinskiTriangle(in uint order) nothrow @safe @nogc {
import core.stdc.stdio: putchar;
foreach_reverse (immutable y; 0 .. 2 ^^ order) {
foreach (immutable _; 0 .. y)
' '.putchar;
foreach (immutable x; 0 .. 2 ^^ order - y) {
putchar((x & y) ? ' ' : '*');
' '.putchar;
}
'\n'.putchar;
}
}
void main() nothrow @safe @nogc {
4.showSierpinskiTriangle;
}

View file

@ -0,0 +1,42 @@
import core.stdc.stdio: putchar;
import std.algorithm: swap;
void showSierpinskiTriangle(in uint nLevels) nothrow @safe
in {
assert(nLevels > 0);
} body {
alias Row = bool[];
static void applyRules(in Row r1, Row r2) pure nothrow @safe @nogc {
r2[0] = r1[0] || r1[1];
r2[$ - 1] = r1[$ - 2] || r1[$ - 1];
foreach (immutable i; 1 .. r2.length - 1)
r2[i] = r1[i - 1] != r1[i] || r1[i] != r1[i + 1];
}
static void showRow(in Row r) nothrow @safe @nogc {
foreach (immutable b; r)
putchar(b ? '#' : ' ');
'\n'.putchar;
}
immutable width = 2 ^^ (nLevels + 1) - 1;
auto row1 = new Row(width);
auto row2 = new Row(width);
row1[width / 2] = true;
foreach (immutable _; 0 .. 2 ^^ nLevels) {
showRow(row1);
applyRules(row1, row2);
row1.swap(row2);
}
}
void main() @safe nothrow {
foreach (immutable i; 1 .. 6) {
i.showSierpinskiTriangle;
'\n'.putchar;
}
}

View file

@ -0,0 +1,4 @@
x = 1
while True:
print(bin(x)[2:].replace('0', ' '))
x ^= x<<1

View file

@ -2,10 +2,10 @@ def sierpinski_triangle(n)
triangle = ["*"]
n.times do |i|
sp = " " * (2**i)
triangle = triangle.collect {|x| sp + x + sp} + \
triangle = triangle.collect {|x| sp + x + sp} +
triangle.collect {|x| x + " " + x}
end
triangle.join("\n")
triangle
end
puts sierpinski_triangle(4)

View file

@ -1,9 +1,8 @@
def sierpinski_triangle(n)
(0..(n-1)).inject(["*"]) {|triangle, i|
space = " " * (2**i)
triangle.map {|x| space + x + space} + triangle.map {|x| x + " " + x}
}
(0...n).inject(["*"]) {|triangle, i|
space = " " * (2**i)
triangle.map {|x| space + x + space} + triangle.map {|x| x + " " + x}
}
end
puts sierpinski_triangle(4)