Time for an 2014 update…

This commit is contained in:
Ingy döt Net 2014-01-17 05:32:22 +00:00
parent 372c577f83
commit 09687c4926
2520 changed files with 34227 additions and 7318 deletions

View file

@ -0,0 +1,36 @@
local hailstone:
swap [ over ]
while < 1 dup:
if % over 2:
#odd
++ * 3
else:
#even
/ swap 2
swap push-through rot dup
drop
if = (name) :(main):
local :h27 hailstone 27
!. = 112 len h27
!. = 27 h27! 0
!. = 82 h27! 1
!. = 41 h27! 2
!. = 124 h27! 3
!. = 8 h27! 108
!. = 4 h27! 109
!. = 2 h27! 110
!. = 1 h27! 111
local :max 0
local :maxlen 0
for i range 1 99999:
dup len hailstone i
if < maxlen:
set :maxlen
set :max i
else:
drop
!print( "number: " to-str max ", length: " to-str maxlen )
else:
@hailstone

View file

@ -0,0 +1,12 @@
!import!hailstone
local :counts {}
set-default counts 0
for i range 1 99999:
set-to counts swap ++ counts! dup len hailstone i
local :maxlen 0
for k in keys counts:
if < maxlen counts! k:
set :maxlen counts! k
!print( "Maximum length: " to-str maxlen )

View file

@ -0,0 +1,53 @@
implement Execlib;
include "sys.m"; sys: Sys;
include "draw.m";
Execlib: module {
init: fn(ctxt: ref Draw->Context, args: list of string);
hailstone: fn(i: big): list of big;
};
init(nil: ref Draw->Context, nil: list of string)
{
sys = load Sys Sys->PATH;
seq := hailstone(big 27);
l := len seq;
sys->print("hailstone(27): ");
for(i := 0; i < 4; i++) {
sys->print("%bd, ", hd seq);
seq = tl seq;
}
sys->print("⋯");
while(len seq > 4)
seq = tl seq;
while(seq != nil) {
sys->print(", %bd", hd seq);
seq = tl seq;
}
sys->print(" (length %d)\n", l);
max := 1;
maxn := big 1;
for(n := big 2; n < big 100000; n++) {
cur := len hailstone(n);
if(cur > max) {
max = cur;
maxn = n;
}
}
sys->print("hailstone(%bd) has length %d\n", maxn, max);
}
hailstone(i: big): list of big
{
if(i == big 1)
return big 1 :: nil;
if(i % big 2 == big 0)
return i :: hailstone(i / big 2);
return i :: hailstone(big 3 * i + big 1);
}

View file

@ -0,0 +1,45 @@
implement ExecsExeclib;
include "sys.m"; sys: Sys;
include "draw.m";
ExecsExeclib: module {
init: fn(ctxt: ref Draw->Context, args: list of string);
};
# Usually, this would be placed into something like "execlib.m",
# but it's fine here.
Execlib: module {
hailstone: fn(i: big): list of big;
};
init(nil: ref Draw->Context, nil: list of string)
{
sys = load Sys Sys->PATH;
# This program expects that the result of compiling Execlib is execlib.dis,
# so you'll need to adjust this line if you used a different filename.
lib := load Execlib "execlib.dis";
if(lib == nil)
die("Couldn't load execlib.dis");
counts := array[352] of { * => 0 };
for(i := 1; i < 100000; i++) {
counts[len lib->hailstone(big i)]++;
}
max := 0;
maxi := 0;
for(i = 1; i < len counts; i++) {
if(counts[i] > max) {
max = counts[i];
maxi = i;
}
}
sys->print("The most common sequence length is %d (encountered %d times)\n", maxi, max);
}
die(s: string)
{
sys->fprint(sys->fildes(2), "runls: %s: %r", s);
raise "fail:errors";
}