langs a-z
This commit is contained in:
parent
db842d013d
commit
d066446780
11389 changed files with 98361 additions and 1020 deletions
9
Task/Executable-library/Perl-6/executable-library-1.pl6
Normal file
9
Task/Executable-library/Perl-6/executable-library-1.pl6
Normal 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;
|
||||
}
|
||||
1
Task/Executable-library/Perl-6/executable-library-2.pl6
Normal file
1
Task/Executable-library/Perl-6/executable-library-2.pl6
Normal file
|
|
@ -0,0 +1 @@
|
|||
$ perl6 Hailstone.pm
|
||||
3
Task/Executable-library/Perl-6/executable-library-3.pl6
Normal file
3
Task/Executable-library/Perl-6/executable-library-3.pl6
Normal 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;
|
||||
39
Task/Executable-library/Pike/executable-library-1.pike
Normal file
39
Task/Executable-library/Pike/executable-library-1.pike
Normal 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);
|
||||
}
|
||||
25
Task/Executable-library/Pike/executable-library-2.pike
Normal file
25
Task/Executable-library/Pike/executable-library-2.pike
Normal 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);
|
||||
}
|
||||
23
Task/Executable-library/Pike/executable-library-3.pike
Normal file
23
Task/Executable-library/Pike/executable-library-3.pike
Normal 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);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue