langs a-z

This commit is contained in:
Ingy döt Net 2013-04-10 22:43:41 -07:00
parent db842d013d
commit d066446780
11389 changed files with 98361 additions and 1020 deletions

View file

@ -0,0 +1,9 @@
module Hailstone {
our sub hailstone($n) is export {
$n, { $_ %% 2 ?? $_ div 2 !! $_ * 3 + 1 } ... 1
}
}
sub MAIN {
say "hailstone(27) = {.[^4]} [...] {.[*-4 .. *-1]}" given Hailstone::hailstone 27;
}

View file

@ -0,0 +1 @@
$ perl6 Hailstone.pm

View file

@ -0,0 +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;

View file

@ -0,0 +1,39 @@
#!/usr/bin/env pike
int next(int n)
{
if (n==1)
return 0;
if (n%2)
return 3*n+1;
else
return n/2;
}
array(int) hailstone(int n)
{
array seq = ({ n });
while (n=next(n))
seq += ({ n });
return seq;
}
void main()
{
array(int) two = hailstone(27);
if (equal(two[0..3], ({ 27, 82, 41, 124 })) && equal(two[<3..], ({ 8,4,2,1 })))
write("sizeof(({ %{%d, %}, ... %{%d, %} }) == %d\n", two[0..3], two[<3..], sizeof(two));
mapping longest = ([ "length":0, "start":0 ]);
foreach(allocate(100000); int start; )
{
int length = sizeof(hailstone(start));
if (length > longest->length)
{
longest->length = length;
longest->start = start;
}
}
write("longest sequence starting at %d has %d elements\n", longest->start, longest->length);
}

View file

@ -0,0 +1,25 @@
void main()
{
.HailStone HailStone = .HailStone();
mapping long = ([]);
foreach (allocate(100000); int start; )
long[sizeof(HailStone->hailstone(start))]++;
analyze(long);
}
void analyze(mapping long)
{
mapping max = ([ "count":0, "length":0 ]);
foreach (long; int length; int count)
{
if (count > max->count)
{
max->length = length;
max->count = count;
}
}
write("most common length %d appears %d times\n", max->length, max->count);
}

View file

@ -0,0 +1,23 @@
void main()
{
mapping long = ([]);
foreach (allocate(100000); int start; )
long[sizeof(.Hailstone.hailstone(start))]++;
analyze(long);
}
void analyze(mapping long)
{
mapping max = ([ "count":0, "length":0 ]);
foreach (long; int length; int count)
{
if (count > max->count)
{
max->length = length;
max->count = count;
}
}
write("most common length %d appears %d times\n", max->length, max->count);
}