Executable library: Difference between revisions

m
(added Pascal example)
m (→‎{{header|Wren}}: Minor tidy)
 
(5 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,376 ⟶ 1,377:
I: Longint;
begin
Buffer[0]Hailstone.Count := aValue0;
IHailstone.Data := 1nil;
if (aValue <> 0) and (aValue <= 200000) then begin
repeat
Buffer[0] := aValue;
if Odd(aValue) then
aValueI := Succ((3 * aValue))1;
elserepeat
aValue :=if Odd(aValue) div 2;then
Buffer[I] aValue := Succ((3 * aValue;))
Inc(I); else
until aValue := 1aValue div 2;
Hailstone.Count Buffer[I] := IaValue;
Inc(I);
Hailstone.Data := @Buffer;
ifuntil Odd(aValue) then= 1;
Hailstone.Count := I;
Hailstone.Data := @Buffer;
end;
end;
 
Line 1,402 ⟶ 1,407:
Hailstone;
var
ahs: THailSeq;
I, Value: Cardinal;
MaxLen: Longint;
begin
ahs := Hailstone(27);
WriteLn('Length of Hailstone(27) is ', ahs.Count, ',');
PrintArray('it starts with ', ahs.Data[0..3]);
PrintArray('and ends with ', ahs.Data[ahs.Count-4..ahs.Count-1]);
Value := 0;
MaxLen := 0;
for I := 1 to 100000 do begin
ahs := Hailstone(I);
if ahs.Count > MaxLen then begin
MaxLen := ahs.Count;
Value := I;
end;
end;
WriteLn('Maximum length ', MaxLen, ' was found for Hailstone(', Value, ')');
end.
Line 1,481 ⟶ 1,486:
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>
 
Line 2,132 ⟶ 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,151 ⟶ 2,157:
}
}
 
 
say "Longest sequence is for #{n}: #{max}"
}</syntaxhighlight>
Line 2,230 ⟶ 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,266 ⟶ 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,273 ⟶ 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,286 ⟶ 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,310 ⟶ 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,485

edits