Executable library: Difference between revisions

m
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Minor tidy)
 
(6 intermediate revisions by 3 users not shown)
Line 756:
This is the executable library:
 
<syntaxhighlight lang="j">hailseq=: -:`(1 3&p.)@.(2&|) ^:(1 ~: ]) ^:a:"0#!/usr/bin/ijconsole
hailseq=: -:`(1 3&p.)@.(2&|) ^:(1 ~: ]) ^:a:"0
9!:29]1
9!:27'main 0'
Line 1,357 ⟶ 1,358:
Execute main program: ''LD_LIBRARY_PATH=. ./main'' :
<pre>27: 82, 41, 124, 62, 31, 94, 47, 142, 71, 214, 107, 322, 161, 484, 242, 121, 364, 182, 91, 274, 137, 412, 206, 103, 310, 155, 466, 233, 700, 350, 175, 526, 263, 790, 395, 1186, 593, 1780, 890, 445, 1336, 668, 334, 167, 502, 251, 754, 377, 1132, 566, 283, 850, 425, 1276, 638, 319, 958, 479, 1438, 719, 2158, 1079, 3238, 1619, 4858, 2429, 7288, 3644, 1822, 911, 2734, 1367, 4102, 2051, 6154, 3077, 9232, 4616, 2308, 1154, 577, 1732, 866, 433, 1300, 650, 325, 976, 488, 244, 122, 61, 184, 92, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1, (112)</pre>
 
=={{header|Pascal}}==
{{works with|FPC}}
Library:
<syntaxhighlight lang="pascal">
uses
DynLibs;
type
THailSeq = record
Data: PCardinal;
Count: Longint;
end;
var
Buffer: array[0..511] of Cardinal;
 
function Hailstone(aValue: Cardinal): THailSeq;
var
I: Longint;
begin
Hailstone.Count := 0;
Hailstone.Data := nil;
if (aValue <> 0) and (aValue <= 200000) then begin
Buffer[0] := aValue;
I := 1;
repeat
if Odd(aValue) then
aValue := Succ((3 * aValue))
else
aValue := aValue div 2;
Buffer[I] := aValue;
Inc(I);
until aValue = 1;
Hailstone.Count := I;
Hailstone.Data := @Buffer;
end;
end;
 
procedure PrintArray(const Prefix: string; const a: array of Cardinal);
var
I: Longint;
begin
Write(Prefix, '[');
for I := 0 to High(a) - 1 do Write(a[I], ', ');
WriteLn(a[High(a)], ']');
end;
 
exports
Hailstone;
var
hs: THailSeq;
I, Value: Cardinal;
MaxLen: Longint;
begin
hs := Hailstone(27);
WriteLn('Length of Hailstone(27) is ', hs.Count, ',');
PrintArray('it starts with ', hs.Data[0..3]);
PrintArray('and ends with ', hs.Data[hs.Count-4..hs.Count-1]);
Value := 0;
MaxLen := 0;
for I := 1 to 100000 do begin
hs := Hailstone(I);
if hs.Count > MaxLen then begin
MaxLen := hs.Count;
Value := I;
end;
end;
WriteLn('Maximum length ', MaxLen, ' was found for Hailstone(', Value, ')');
end.
</syntaxhighlight>
build
<syntaxhighlight lang="shell">
Linux64: fpc <source file name> -Cg -k-pie -oexec_lib
Win64: fpc <source file name> -Cg -oexec_lib.exe
</syntaxhighlight>
when run as executable:
{{out}}
<pre>
Length of Hailstone(27) is 112,
it starts with [27, 82, 41, 124]
and ends with [8, 4, 2, 1]
Maximum length 351 was found for Hailstone(77031)
</pre>
Client:
<syntaxhighlight lang="pascal">
{$h+}
uses
SysUtils, DynLibs;
const
LIB_NAME = {$ifdef windows}'exec_lib.exe'{$else}'exec_lib'{$endif};
type
THailSeq = record
Data: PCardinal;
Count: Longint;
end;
THailstone = function(aValue: Cardinal): THailSeq;
TCounts = array[0..511] of Longint;
var
LibName: string;
hLib: TLibHandle;
Fun: THailstone;
I, Len, Count, c: Longint;
Counts: TCounts;
begin
LibName := ExtractFilePath(ParamStr(0))+LIB_NAME;
hLib := LoadLibrary(LibName);
if hLib = NilHandle then begin
WriteLn('Can not load library ', LibName); Halt(1);
end;
Pointer(Fun) := GetProcAddress(hLib, 'Hailstone');
if Pointer(Fun) = nil then begin
WriteLn('Can not find Hailstone() function'); Halt(2);
end;
Counts := Default(TCounts);
Count := 0;
Len := 0;
for I := 1 to 100000 do begin
c := Fun(I).Count;
Inc(Counts[c]);
if Counts[c] > Count then begin
Count := Counts[c];
Len := c;
end;
end;
UnloadLibrary(hLib);
WriteLn('The most common Hailstone sequence length in the specified range is ',
Len, ', it occurs ', Count, ' times.');
end.
</syntaxhighlight>
Both executables must be placed in the same folder.
{{out}}
<pre>
The most common Hailstone sequence length in the specified range is 72, it occurs 1467 times.
</pre>
 
=={{header|Perl}}==
Line 2,004 ⟶ 2,138:
while (n > 1) {
take(n)
n = (n.is_even ? (n/2) : (take(3*n + 1)/2))
}
take(1)
}
}
 
 
if (__FILE__ == __MAIN__) { # true when not imported
var seq = hailstone(27)
say "hailstone(27) - #{seq.len} elements: #{seq.ftfirst(0, 34)} [...] #{seq.ftlast(-4)}"
 
 
var n = 0
var max = 0
Line 2,023 ⟶ 2,157:
}
}
 
 
say "Longest sequence is for #{n}: #{max}"
}</syntaxhighlight>
Line 2,102 ⟶ 2,236:
The strategy here is check whether the main module is the same as the library module and to treat is as executable if it is but as a library otherwise.
 
<syntaxhighlight lang="ecmascriptwren">/* hailstoneExecutable_library.wren */
 
var Hailstone = Fn.new { |n|
Line 2,138 ⟶ 2,272:
// Check if it's being used as a library or not.
import "os" for Process
if (Process.allArguments[1] == "hailstoneExecutable_library.wren") { // if true, not a library
libMain_.call()
}</syntaxhighlight>
Line 2,145 ⟶ 2,279:
If we run this directly, we get the expected output:
<pre>
$ wren hailstoneExecutable_library.wren
 
For the Hailstone sequence starting with n = 27:
Line 2,158 ⟶ 2,292:
 
If we now create a second module which imports the above and calls its Hailstone function:
<syntaxhighlight lang="ecmascriptwren">/* hailstone2Executable_library_2.wren */
 
import "./hailstoneExecutable_library" for Hailstone
 
var freq = {}
Line 2,182 ⟶ 2,316:
We can now run this to check that the executable code in hailstone.wren has been suppressed, giving us:
<pre>
$ wren hailstone2Executable_library_2.wren
 
The Hailstone length returned most is 72, which occurs 1467 times.
9,487

edits