CDE
This commit is contained in:
parent
518da4a923
commit
764da6cbbb
6144 changed files with 83610 additions and 11 deletions
18
Task/Executable-library/0DESCRIPTION
Normal file
18
Task/Executable-library/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
The general idea behind an executable library is to create a library that when used as a library does one thing; but has the ability to be run directly via command line. Thus the API comes with a CLI in the very same source code file.
|
||||
|
||||
'''Task detail'''
|
||||
|
||||
* Create a library/module/dll/shared object/... for a programming language that contains a function/method called hailstone that is a function taking a positive integer and returns the [[Hailstone sequence]] for that number.
|
||||
|
||||
* The library, when executed directly should satisfy the remaining requirements of the [[Hailstone sequence]] task:
|
||||
:: 2. Use the routine to show that the hailstone sequence for the number 27 has 112 elements starting with 27, 82, 41, 124 and ending with 8, 4, 2, 1
|
||||
:: 3. Show the number less than 100,000 which has the longest hailstone sequence together with that sequences length.
|
||||
|
||||
* Create a second executable to calculate the following:
|
||||
** Use the libraries hailstone function, in the standard manner, (or document how this use deviates from standard use of a library), together with extra code in this executable, to find the hailstone length returned most often for 1 ≤ n < 100,000.
|
||||
|
||||
* Explain any extra setup/run steps needed to complete the task.
|
||||
|
||||
'''Notes:'''
|
||||
* It is assumed that for a language that overwhelmingly ships in a compiled form, such as C, the library must also be an executable and the compiled user of that library is to do so without changing the compiled library. I.e. the compile tool-chain is assumed ''not'' to be present in the runtime environment.
|
||||
* Interpreters are present in the runtime environment.
|
||||
4
Task/Executable-library/Ada/executable-library-1.ada
Normal file
4
Task/Executable-library/Ada/executable-library-1.ada
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
package Parameter is
|
||||
X: Natural := 0;
|
||||
Y: Natural;
|
||||
end Parameter;
|
||||
49
Task/Executable-library/Ada/executable-library-2.ada
Normal file
49
Task/Executable-library/Ada/executable-library-2.ada
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
with Ada.Text_IO, Parameter, Hailstones;
|
||||
|
||||
procedure Hailstone is
|
||||
-- if Parameter.X > 0, the length of Hailstone(Parameter.X)
|
||||
-- is computed and written into Parameter.Y
|
||||
|
||||
-- if Parameter.X = 0, Hailstone(27) and N <= 100_000 with maximal
|
||||
-- Hailstone(N) are computed and printed.
|
||||
|
||||
procedure Show_Sequence(N: Natural) is
|
||||
Seq: Hailstones.Integer_Sequence := Hailstones.Create_Sequence(N);
|
||||
begin
|
||||
Ada.Text_IO.Put("Hailstone(" & Integer'Image(N) & " ) = (");
|
||||
if Seq'Length < 8 then
|
||||
for I in Seq'First .. Seq'Last-1 loop
|
||||
Ada.Text_IO.Put(Integer'Image(Seq(I)) & ",");
|
||||
end loop;
|
||||
else
|
||||
for I in Seq'First .. Seq'First+3 loop
|
||||
Ada.Text_IO.Put(Integer'Image(Seq(I)) & ",");
|
||||
end loop;
|
||||
Ada.Text_IO.Put(" ...,");
|
||||
for I in Seq'Last-3 .. Seq'Last-1 loop
|
||||
Ada.Text_IO.Put(Integer'Image(Seq(I)) &",");
|
||||
end loop;
|
||||
end if;
|
||||
Ada.Text_IO.Put_Line(Integer'Image(Seq(Seq'Last)) & " ); Length: " &
|
||||
Integer'Image(seq'Length));
|
||||
end Show_Sequence;
|
||||
begin
|
||||
if Parameter.X>0 then
|
||||
Parameter.Y := Hailstones.Create_Sequence(Parameter.X)'Length;
|
||||
else
|
||||
Show_Sequence(27);
|
||||
declare
|
||||
Longest: Natural := 0;
|
||||
Longest_Length: Natural := 0;
|
||||
begin
|
||||
for I in 2 .. 100_000 loop
|
||||
if Hailstones.Create_Sequence(I)'Length > Longest_Length then
|
||||
Longest := I;
|
||||
Longest_Length := Hailstones.Create_Sequence(I)'Length;
|
||||
end if;
|
||||
end loop;
|
||||
Ada.Text_IO.Put("Longest<=100_000: ");
|
||||
Show_Sequence(Longest);
|
||||
end;
|
||||
end if;
|
||||
end Hailstone;
|
||||
1
Task/Executable-library/Ada/executable-library-3.ada
Normal file
1
Task/Executable-library/Ada/executable-library-3.ada
Normal file
|
|
@ -0,0 +1 @@
|
|||
procedure Hailstone;
|
||||
35
Task/Executable-library/Ada/executable-library-4.ada
Normal file
35
Task/Executable-library/Ada/executable-library-4.ada
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
with Hailstone, Parameter, Ada.Text_IO;
|
||||
|
||||
procedure Hailstone_Test is
|
||||
Counts: array (1 .. 100_000) of Natural := (others => 0);
|
||||
Max_Count: Natural := 0;
|
||||
Most_Common: Positive := Counts'First;
|
||||
Length: Natural renames Parameter.Y;
|
||||
Sample: Natural := 0;
|
||||
begin
|
||||
for I in Counts'Range loop
|
||||
Parameter.X := I;
|
||||
Hailstone; -- compute the length of Hailstone(I)
|
||||
Counts(Length) := Counts(Length)+1;
|
||||
end loop;
|
||||
for I in Counts'Range loop
|
||||
if Counts(I) > Max_Count then
|
||||
Max_Count := Counts(I);
|
||||
Most_Common := I;
|
||||
end if;
|
||||
end loop;
|
||||
Ada.Text_IO.Put_Line("Most frequent length:"
|
||||
& Integer'Image(Most_Common)
|
||||
& ";" & Integer'Image(Max_Count)
|
||||
& " sequences of that length.");
|
||||
for I in Counts'Range loop
|
||||
Parameter.X := I;
|
||||
Hailstone; -- compute the length of Hailstone(I)
|
||||
if Length = Most_Common then
|
||||
Sample := I;
|
||||
exit;
|
||||
end if;
|
||||
end loop;
|
||||
Ada.Text_IO.Put_Line("The first such sequence: Hailstone("
|
||||
& Integer'Image(Sample) & " ).");
|
||||
end Hailstone_Test;
|
||||
28
Task/Executable-library/AutoHotkey/executable-library-1.ahk
Normal file
28
Task/Executable-library/AutoHotkey/executable-library-1.ahk
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
#NoEnv
|
||||
SetBatchLines, -1
|
||||
|
||||
; Check if we're executed directly:
|
||||
If (A_LineFile = A_ScriptFullPath){
|
||||
h27 := hailstone(27)
|
||||
MsgBox % "Length of hailstone 27: " (m := h27.MaxIndex()) "`nStarts with "
|
||||
. h27[1] ", " h27[2] ", " h27[3] ", " h27[4]
|
||||
. "`nEnds with "
|
||||
. h27[m-3] ", " h27[m-2] ", " h27[m-1] ", " h27[m]
|
||||
|
||||
Loop 100000
|
||||
{
|
||||
h := hailstone(A_Index)
|
||||
If (h.MaxIndex() > m)
|
||||
m := h.MaxIndex(), longest := A_Index
|
||||
}
|
||||
MsgBox % "Longest hailstone is that of " longest " with a length of " m "!"
|
||||
}
|
||||
|
||||
|
||||
hailstone(n){
|
||||
out := [n]
|
||||
Loop
|
||||
n := n & 1 ? n*3+1 : n//2, out.insert(n)
|
||||
until n=1
|
||||
return out
|
||||
}
|
||||
17
Task/Executable-library/AutoHotkey/executable-library-2.ahk
Normal file
17
Task/Executable-library/AutoHotkey/executable-library-2.ahk
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#NoEnv
|
||||
#Include %A_ScriptDir%\hailstone.ahk
|
||||
SetBatchLines -1
|
||||
|
||||
col := Object(), highestCount := 0
|
||||
|
||||
Loop 100000
|
||||
{
|
||||
length := hailstone(A_Index).MaxIndex()
|
||||
if not col[length]
|
||||
col[length] := 0
|
||||
col[length]++
|
||||
}
|
||||
for length, count in col
|
||||
if (count > highestCount)
|
||||
highestCount := count, highestN := length
|
||||
MsgBox % "the most common length was " highestN "; it occurred " highestCount " times."
|
||||
21
Task/Executable-library/BBC-BASIC/executable-library-1.bbc
Normal file
21
Task/Executable-library/BBC-BASIC/executable-library-1.bbc
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
seqlen% = FNhailstone(27)
|
||||
PRINT "Sequence length for 27 is "; seqlen%
|
||||
maxlen% = 0
|
||||
FOR number% = 2 TO 100000
|
||||
seqlen% = FNhailstone(number%)
|
||||
IF seqlen% > maxlen% THEN
|
||||
maxlen% = seqlen%
|
||||
maxnum% = number%
|
||||
ENDIF
|
||||
NEXT
|
||||
PRINT "The number with the longest hailstone sequence is " ; maxnum%
|
||||
PRINT "Its sequence length is " ; maxlen%
|
||||
END
|
||||
|
||||
DEF FNhailstone(N%)
|
||||
LOCAL L%
|
||||
WHILE N% <> 1
|
||||
IF N% AND 1 THEN N% = 3 * N% + 1 ELSE N% DIV= 2
|
||||
L% += 1
|
||||
ENDWHILE
|
||||
= L% + 1
|
||||
18
Task/Executable-library/BBC-BASIC/executable-library-2.bbc
Normal file
18
Task/Executable-library/BBC-BASIC/executable-library-2.bbc
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
INSTALL "HAILSTONE"
|
||||
|
||||
DIM freq%(351)
|
||||
FOR number% = 2 TO 100000
|
||||
seqlen% = FNhailstone(number%)
|
||||
freq%(seqlen%) += 1
|
||||
NEXT
|
||||
max% = 0
|
||||
FOR i% = 0 TO 351
|
||||
IF freq%(i%) > max% THEN
|
||||
max% = freq%(i%)
|
||||
mostcommon% = i%
|
||||
ENDIF
|
||||
NEXT
|
||||
|
||||
PRINT "The most common sequence length is " ; mostcommon%
|
||||
PRINT "It occurs " ; max% " times"
|
||||
END
|
||||
7
Task/Executable-library/C/executable-library-1.c
Normal file
7
Task/Executable-library/C/executable-library-1.c
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
#ifndef HAILSTONE
|
||||
#define HAILSTONE
|
||||
|
||||
long hailstone(long, long**);
|
||||
void free_sequence(long *);
|
||||
|
||||
#endif/*HAILSTONE*/
|
||||
44
Task/Executable-library/C/executable-library-2.c
Normal file
44
Task/Executable-library/C/executable-library-2.c
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
long hailstone(long n, long **seq)
|
||||
{
|
||||
long len = 0, buf_len = 4;
|
||||
if (seq)
|
||||
*seq = malloc(sizeof(long) * buf_len);
|
||||
|
||||
while (1) {
|
||||
if (seq) {
|
||||
if (len >= buf_len) {
|
||||
buf_len *= 2;
|
||||
*seq = realloc(*seq, sizeof(long) * buf_len);
|
||||
}
|
||||
(*seq)[len] = n;
|
||||
}
|
||||
len ++;
|
||||
if (n == 1) break;
|
||||
if (n & 1) n = 3 * n + 1;
|
||||
else n >>= 1;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
void free_sequence(long * s) { free(s); }
|
||||
|
||||
const char my_interp[] __attribute__((section(".interp"))) = "/lib/ld-linux.so.2";
|
||||
/* "ld-linux.so.2" should be whatever you use on your platform */
|
||||
|
||||
int hail_main() /* entry point when running along, see compiler command line */
|
||||
{
|
||||
long i, *seq;
|
||||
|
||||
long len = hailstone(27, &seq);
|
||||
printf("27 has %ld numbers in sequence:\n", len);
|
||||
for (i = 0; i < len; i++) {
|
||||
printf("%ld ", seq[i]);
|
||||
}
|
||||
printf("\n");
|
||||
free_sequence(seq);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
20
Task/Executable-library/C/executable-library-3.c
Normal file
20
Task/Executable-library/C/executable-library-3.c
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#include <stdio.h>
|
||||
#include "hailstone.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
long i, longest, longest_i, len;
|
||||
|
||||
longest = 0;
|
||||
for (i = 1; i < 100000; i++) {
|
||||
len = hailstone(i, 0);
|
||||
if (len > longest) {
|
||||
longest_i = i;
|
||||
longest = len;
|
||||
}
|
||||
}
|
||||
|
||||
printf("Longest sequence at %ld, length %ld\n", longest_i, longest);
|
||||
|
||||
return 0;
|
||||
}
|
||||
26
Task/Executable-library/Perl/executable-library-1.pl
Normal file
26
Task/Executable-library/Perl/executable-library-1.pl
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package Hailstone;
|
||||
|
||||
sub seq {
|
||||
my $x = shift;
|
||||
$x == 1 ? (1) : ($x & 1)? ($x, seq($x * 3 + 1))
|
||||
: ($x, seq($x / 2))
|
||||
}
|
||||
|
||||
my %cache = (1 => 1);
|
||||
sub len {
|
||||
my $x = shift;
|
||||
$cache{$x} //= 1 + (
|
||||
$x & 1 ? len($x * 3 + 1)
|
||||
: len($x / 2))
|
||||
}
|
||||
|
||||
unless (caller) {
|
||||
for (1 .. 100_000) {
|
||||
my $l = len($_);
|
||||
($m, $len) = ($_, $l) if $l > $len;
|
||||
}
|
||||
print "seq of 27 - $cache{27} elements: @{[seq(27)]}\n";
|
||||
print "Longest sequence is for $m: $len\n";
|
||||
}
|
||||
|
||||
1;
|
||||
11
Task/Executable-library/Perl/executable-library-2.pl
Normal file
11
Task/Executable-library/Perl/executable-library-2.pl
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
use Hailstone;
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
my %seqs;
|
||||
for (1 .. 100_000) {
|
||||
$seqs{Hailstone::len($_)}++;
|
||||
}
|
||||
|
||||
my ($most_frequent) = sort {$seqs{$b} <=> $seqs{$a}} keys %seqs;
|
||||
print "Most frequent length: $most_frequent ($seqs{$most_frequent} occurrences)\n";
|
||||
20
Task/Executable-library/PicoLisp/executable-library-1.l
Normal file
20
Task/Executable-library/PicoLisp/executable-library-1.l
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#!/usr/bin/picolisp /usr/lib/picolisp/lib.l
|
||||
|
||||
(de hailstone (N)
|
||||
(make
|
||||
(until (= 1 (link N))
|
||||
(setq N
|
||||
(if (bit? 1 N)
|
||||
(inc (* N 3))
|
||||
(/ N 2) ) ) ) ) )
|
||||
|
||||
(de hailtest ()
|
||||
(let L (hailstone 27)
|
||||
(test 112 (length L))
|
||||
(test (27 82 41 124) (head 4 L))
|
||||
(test (8 4 2 1) (tail 4 L)) )
|
||||
(let N (maxi '((N) (length (hailstone N))) (range 1 100000))
|
||||
(test 77031 N)
|
||||
(test 351 (length (hailstone N))) )
|
||||
(println 'OK)
|
||||
(bye) )
|
||||
11
Task/Executable-library/PicoLisp/executable-library-2.l
Normal file
11
Task/Executable-library/PicoLisp/executable-library-2.l
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#!/usr/bin/picolisp /usr/lib/picolisp/lib.l
|
||||
|
||||
(load "hailstone.l")
|
||||
|
||||
(let Len NIL
|
||||
(for N 100000
|
||||
(accu 'Len (length (hailstone N)) 1) )
|
||||
(let M (maxi cdr Len)
|
||||
(prinl "The hailstone length returned most often is " (car M))
|
||||
(prinl "It is returned " (cdr M) " times") ) )
|
||||
(bye)
|
||||
12
Task/Executable-library/Python/executable-library-1.py
Normal file
12
Task/Executable-library/Python/executable-library-1.py
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
def hailstone(n):
|
||||
seq = [n]
|
||||
while n>1:
|
||||
n = 3*n + 1 if n & 1 else n//2
|
||||
seq.append(n)
|
||||
return seq
|
||||
|
||||
if __name__ == '__main__':
|
||||
h = hailstone(27)
|
||||
assert len(h)==112 and h[:4]==[27, 82, 41, 124] and h[-4:]==[8, 4, 2, 1]
|
||||
print("Maximum length %i was found for hailstone(%i) for numbers <100,000" %
|
||||
max((len(hailstone(i)), i) for i in range(1,100000)))
|
||||
13
Task/Executable-library/Python/executable-library-2.py
Normal file
13
Task/Executable-library/Python/executable-library-2.py
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
from collections import Counter
|
||||
|
||||
def function_length_frequency(func, hrange):
|
||||
return Counter(len(func(n)) for n in hrange).most_common()
|
||||
|
||||
if __name__ == '__main__':
|
||||
from executable_hailstone_library import hailstone
|
||||
|
||||
upto = 100000
|
||||
hlen, freq = function_length_frequency(hailstone, range(1, upto))[0]
|
||||
print("The length of hailstone sequence that is most common for\n"
|
||||
"hailstone(n) where 1<=n<%i, is %i. It occurs %i times."
|
||||
% (upto, hlen, freq))
|
||||
9
Task/Executable-library/REXX/executable-library-1.rexx
Normal file
9
Task/Executable-library/REXX/executable-library-1.rexx
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
/*REXX program returns the hailstone (Collatz) sequence for any integer.*/
|
||||
numeric digits 20 /*ensure enough digits for mult. */
|
||||
parse arg n 1 s /*N & S assigned to the first arg*/
|
||||
do while n\==1 /*loop while N isn't unity. */
|
||||
if n//2 then n=n*3+1 /*if N is odd, calc: 3*n +1 */
|
||||
else n=n%2 /* " " " even, perform fast ÷ */
|
||||
s=s n /*build a sequence list (append).*/
|
||||
end /*while*/
|
||||
return s
|
||||
16
Task/Executable-library/REXX/executable-library-2.rexx
Normal file
16
Task/Executable-library/REXX/executable-library-2.rexx
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
/*REXX pgm tests a number and a range for hailstone (Collatz) sequences.*/
|
||||
parse arg x .; if x=='' then x=27 /*get the optional first argument*/
|
||||
|
||||
$=hailstone(x) /*═════════════task 2════════════*/
|
||||
#=words($) /*number of numbers in sequence. */
|
||||
say x 'has a hailstone sequence of' # 'and starts with: ' subword($,1,4),
|
||||
' and ends with:' subword($,#-3)
|
||||
say
|
||||
w=0; do j=1 for 99999 /*═════════════task 3════════════*/
|
||||
$=hailstone(j); #=words($) /*obtain the hailstone sequence. */
|
||||
if #<=w then iterate /*Not big 'nuff? Then keep going.*/
|
||||
bigJ=j; w=# /*remember what # has biggest HS.*/
|
||||
end /*j*/
|
||||
|
||||
say '(between 1──►99,999) ' bigJ 'has the longest hailstone sequence:' w
|
||||
/*stick a fork in it, we're done.*/
|
||||
15
Task/Executable-library/REXX/executable-library-3.rexx
Normal file
15
Task/Executable-library/REXX/executable-library-3.rexx
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
/*REXX pgm finds the most common (popular) hailstone sequence length. */
|
||||
parse arg z .; if z=='' then z=99999 /*get the optional first argument*/
|
||||
!.=0
|
||||
w=0; do j=1 for z /*═════════════task 4════════════*/
|
||||
#=words(hailstone(j)) /*obtain hailstone sequence count*/
|
||||
!.# = !.# + 1 /*add unity to popularity count. */
|
||||
end /*j*/
|
||||
occ=0; p=0
|
||||
do k=1 for z
|
||||
if !.k>occ then do; occ=!.k; p=k; end
|
||||
end /*p*/
|
||||
|
||||
say '(between 1──►'z") " p,
|
||||
' is the most common hailstone sequence length (with' occ "occurrences)."
|
||||
/*stick a fork in it, we're done.*/
|
||||
26
Task/Executable-library/Ruby/executable-library-1.rb
Normal file
26
Task/Executable-library/Ruby/executable-library-1.rb
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
# hailstone.rb
|
||||
module Hailstone
|
||||
module_function
|
||||
def hailstone n
|
||||
seq = [n]
|
||||
until n == 1
|
||||
n = (n.even?) ? (n / 2) : (3 * n + 1)
|
||||
seq << n
|
||||
end
|
||||
seq
|
||||
end
|
||||
end
|
||||
|
||||
if __FILE__ == $0
|
||||
include Hailstone
|
||||
|
||||
# for n = 27, show sequence length and first and last 4 elements
|
||||
hs27 = hailstone 27
|
||||
p [hs27.length, hs27[0..3], hs27[-4..-1]]
|
||||
|
||||
# find the longest sequence among n less than 100,000
|
||||
n, len = (1 ... 100_000) .collect {|n|
|
||||
[n, hailstone(n).length]} .max_by {|n, len| len}
|
||||
puts "#{n} has a hailstone sequence length of #{len}"
|
||||
puts "the largest number in that sequence is #{hailstone(n).max}"
|
||||
end
|
||||
11
Task/Executable-library/Ruby/executable-library-2.rb
Normal file
11
Task/Executable-library/Ruby/executable-library-2.rb
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
# hsfreq.rb
|
||||
require 'hailstone'
|
||||
|
||||
h = Hash.new(0)
|
||||
last = 99_999
|
||||
(1..last).each {|n| h[Hailstone.hailstone(n).length] += 1}
|
||||
length, count = h.max_by {|length, count| count}
|
||||
|
||||
puts "Given the hailstone sequences from 1 to #{last},"
|
||||
puts "the most common sequence length is #{length},"
|
||||
puts "with #{count} such sequences."
|
||||
25
Task/Executable-library/Tcl/executable-library-1.tcl
Normal file
25
Task/Executable-library/Tcl/executable-library-1.tcl
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
### In the file hailstone.tcl ###
|
||||
package provide hailstone 1.0
|
||||
|
||||
proc hailstone n {
|
||||
while 1 {
|
||||
lappend seq $n
|
||||
if {$n == 1} {return $seq}
|
||||
set n [expr {$n & 1 ? $n*3+1 : $n/2}]
|
||||
}
|
||||
}
|
||||
|
||||
# If directly executed, run demo code
|
||||
if {[info script] eq $::argv0} {
|
||||
set h27 [hailstone 27]
|
||||
puts "h27 len=[llength $h27]"
|
||||
puts "head4 = [lrange $h27 0 3]"
|
||||
puts "tail4 = [lrange $h27 end-3 end]"
|
||||
|
||||
set maxlen [set max 0]
|
||||
for {set i 1} {$i<100000} {incr i} {
|
||||
set l [llength [hailstone $i]]
|
||||
if {$l>$maxlen} {set maxlen $l;set max $i}
|
||||
}
|
||||
puts "max is $max, with length $maxlen"
|
||||
}
|
||||
1
Task/Executable-library/Tcl/executable-library-2.tcl
Normal file
1
Task/Executable-library/Tcl/executable-library-2.tcl
Normal file
|
|
@ -0,0 +1 @@
|
|||
pkg_mkIndex .
|
||||
16
Task/Executable-library/Tcl/executable-library-3.tcl
Normal file
16
Task/Executable-library/Tcl/executable-library-3.tcl
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#!/usr/bin/tclsh8.6
|
||||
package require Tcl 8.6 ;# For [lsort -stride] option
|
||||
lappend auto_path . ;# Or wherever it is located
|
||||
package require hailstone 1.0
|
||||
|
||||
# Construct a histogram of length frequencies
|
||||
set histogram {}
|
||||
for {set n 1} {$n < 100000} {incr n} {
|
||||
dict incr histogram [llength [hailstone $n]]
|
||||
}
|
||||
|
||||
# Identify the most common length by sorting...
|
||||
set sortedHist [lsort -decreasing -stride 2 -index 1 $histogram]
|
||||
lassign $sortedHist mostCommonLength freq
|
||||
|
||||
puts "most common length is $mostCommonLength, with frequency $freq"
|
||||
Loading…
Add table
Add a link
Reference in a new issue