2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -0,0 +1,30 @@
HailStone := Object clone
HailStone sequence := method(n,
if(n < 1, Exception raise("hailstone: expect n >= 1 not #{n}" interpolate))
n = n floor // make sure integer value
stones := list(n)
while (n != 1,
n = if(n isEven, n/2, 3*n + 1)
stones append(n)
)
stones
)
if( isLaunchScript,
out := HailStone sequence(27)
writeln("hailstone(27) has length ",out size,": ",
out slice(0,4) join(" ")," ... ",out slice(-4) join(" "))
maxSize := 0
maxN := 0
for(n, 1, 100000-1,
out = HailStone sequence(n)
if(out size > maxSize,
maxSize = out size
maxN = n
)
)
writeln("For numbers < 100,000, ", maxN,
" has the longest sequence of ", maxSize, " elements.")
)

View file

@ -0,0 +1,20 @@
counts := Map clone
for(n, 1, 100000-1,
out := HailStone sequence(n)
key := out size asCharacter
counts atPut(key, counts atIfAbsentPut(key, 0) + 1)
)
maxCount := counts values max
lengths := list()
counts foreach(k,v,
if(v == maxCount, lengths append(k at(0)))
)
if(lengths size == 1,
writeln("The most frequent sequence length for n < 100,000 is ",lengths at(0),
" occurring ",maxCount," times.")
,
writeln("The most frequent sequence lengths for n < 100,000 are:\n",
lengths join(",")," occurring ",maxCount," times each.")
)

View file

@ -0,0 +1,44 @@
#include <pari/pari.h>
#define HAILSTONE1 "n=1;print1(%d,\": \");apply(x->while(x!=1,if(x/2==x\\2,x/=2,x=x*3+1);n++;print1(x,\", \")),%d);print(\"(\",n,\")\n\")"
#define HAILSTONE2 "m=n=0;for(i=2,%d,h=1;apply(x->while(x!=1,if(x/2==x\\2,x/=2,x=x*3+1);h++),i);if(m<h,m=h;n=i));print(n,\": \",m)"
void hailstone1(int x)
{
char buf[1024];
snprintf(buf, sizeof(buf), HAILSTONE1, x, x);
pari_init(1000000, 2);
geval(strtoGENstr(buf));
pari_close();
}
void hailstone2(int range)
{
char buf[1024];
snprintf(buf, sizeof(buf), HAILSTONE2, range);
pari_init(1000000, 2);
geval(strtoGENstr(buf));
pari_close();
}
#if __i386__
const char _hail[] __attribute__((section(".interp"))) = "/lib/ld-linux.so.2";
#else // __x86_64__
const char _hail[] __attribute__((section(".interp"))) = "/lib64/ld-linux-x86-64.so.2";
#endif
int main(void)
{
hailstone1(27);
hailstone2(100000);
exit(0);
}

View file

@ -0,0 +1,8 @@
void hailstone1(int);
int main(void)
{
hailstone1(27);
return 0;
}

View file

@ -1,3 +1,3 @@
use Hailstone;
my %score; %score{hailstone($_).elems}++ for 1 .. 100_000;
say "Most common lengh is {.key}, occuring {.value} times." given max :by(*.value), %score;
say "Most common length is {.key}, occurring {.value} times." given max :by(*.value), %score;