Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,3 @@
---
from: http://rosettacode.org/wiki/First_class_environments
note: Classic CS problems and programs

View file

@ -0,0 +1,26 @@
According to [[wp:First-class_object|Wikipedia]], "In computing, a first-class object ... is an entity that can be constructed at run-time, passed as a parameter, returned from a subroutine, or assigned into a variable".
Often this term is used in the context of "first class functions". In an analogous way, a programming language may support "first class environments".
The environment is minimally, the set of variables accessible to a statement being executed. Change the environments and the same statement could produce different results when executed.
Often an environment is captured in a [[wp:Closure_(computer_science)|closure]], which encapsulates a function together with an environment. That environment, however, is '''not''' first-class, as it cannot be created, passed etc. independently from the function's code.
Therefore, a first class environment is a set of variable bindings which can be constructed at run-time, passed as a parameter, returned from a subroutine, or assigned into a variable. It is like a closure without code. A statement must be able to be executed within a stored first class environment and act according to the environment variable values stored within.
;Task:
Build a dozen environments, and a single piece of code to be run repeatedly in each of these environments.
Each environment contains the bindings for two variables:
:*   a value in the [[Hailstone sequence]], and
:*   a count which is incremented until the value drops to 1.
The initial hailstone values are 1 through 12, and the count in each environment is zero.
When the code runs, it calculates the next hailstone step in the current environment (unless the value is already 1) and counts the steps. Then it prints the current value in a tabular form.
When all hailstone values dropped to 1, processing stops, and the total number of hailstone steps for each environment is printed.
<br><br>

View file

@ -0,0 +1,46 @@
DIM @environ$(12)
@% = 4 : REM Column width
REM Initialise:
FOR E% = 1 TO 12
PROCsetenvironment(@environ$(E%))
seq% = E%
cnt% = 0
@environ$(E%) = FNgetenvironment
NEXT
REM Run hailstone sequences:
REPEAT
T% = 0
FOR E% = 1 TO 12
PROCsetenvironment(@environ$(E%))
PRINT seq% ;
IF seq% <> 1 THEN
T% += 1
cnt% += 1
IF seq% AND 1 seq% = 3 * seq% + 1 ELSE seq% DIV= 2
ENDIF
@environ$(E%) = FNgetenvironment
NEXT
PRINT
UNTIL T% = 0
REM Print counts:
PRINT "Counts:"
FOR E% = 1 TO 12
PROCsetenvironment(@environ$(E%))
PRINT cnt% ;
@environ$(E%) = FNgetenvironment
NEXT
PRINT
END
DEF FNgetenvironment
LOCAL e$ : e$ = STRING$(216, CHR$0)
SYS "RtlMoveMemory", !^e$, ^@%+108, 216
= e$
DEF PROCsetenvironment(e$)
IF LEN(e$) < 216 e$ = STRING$(216, CHR$0)
SYS "RtlMoveMemory", ^@%+108, !^e$, 216
ENDPROC

View file

@ -0,0 +1,34 @@
( (environment=(cnt=0) (seq=))
& :?environments
& 13:?seq
& whl
' ( !seq+-1:>0:?seq
& new$environment:?env
& !seq:?(env..seq)
& !env !environments:?environments
)
& out$(Before !environments)
& whl
' ( !environments:? (=? (seq=>1) ?) ?
& !environments:?envs
& whl
' ( !envs:(=?env) ?envs
& (
' ( $env
(
=
. put$(!(its.seq) \t)
& !(its.seq):1
| 1+!(its.cnt):?(its.cnt)
& 1/2*!(its.seq):~/?(its.seq)
| 3*!(its.seq)+1:?(its.seq)
)
)
.
)
$
)
& out$
)
& out$(After !environments)
)

View file

@ -0,0 +1,36 @@
#include <stdio.h>
#define JOBS 12
#define jobs(a) for (switch_to(a = 0); a < JOBS || !printf("\n"); switch_to(++a))
typedef struct { int seq, cnt; } env_t;
env_t env[JOBS] = {{0, 0}};
int *seq, *cnt;
void hail()
{
printf("% 4d", *seq);
if (*seq == 1) return;
++*cnt;
*seq = (*seq & 1) ? 3 * *seq + 1 : *seq / 2;
}
void switch_to(int id)
{
seq = &env[id].seq;
cnt = &env[id].cnt;
}
int main()
{
int i;
jobs(i) { env[i].seq = i + 1; }
again: jobs(i) { hail(); }
jobs(i) { if (1 != *seq) goto again; }
printf("COUNTS:\n");
jobs(i) { printf("% 4d", *cnt); }
return 0;
}

View file

@ -0,0 +1,21 @@
(def hailstone-src
"(defn hailstone-step [env]
(let [{:keys[n cnt]} env]
(cond
(= n 1) {:n 1 :cnt cnt}
(even? n) {:n (/ n 2) :cnt (inc cnt)}
:else {:n (inc (* n 3)) :cnt (inc cnt)})))")
(defn create-hailstone-table [f-src]
(let [done? (fn [e] (= (:n e) 1))
print-step (fn [envs] (println (map #(format "%4d" (:n %)) envs)))
print-counts (fn [envs] (println "Counts:\n"
(map #(format "%4d" (:cnt %)) envs)))]
(loop [f (eval (read-string f-src))
envs (for [n (range 12)]
{:n (inc n) :cnt 0})]
(if (every? done? envs)
(print-counts envs)
(do
(print-step envs)
(recur f (map f envs)))))))

View file

@ -0,0 +1,28 @@
import std.stdio, std.algorithm, std.range, std.array;
struct Prop {
int[string] data;
ref opDispatch(string s)() pure nothrow {
return data[s];
}
}
immutable code = `
writef("% 4d", e.seq);
if (e.seq != 1) {
e.cnt++;
e.seq = (e.seq & 1) ? 3 * e.seq + 1 : e.seq / 2;
}`;
void main() {
auto envs = 12.iota.map!(i => Prop(["cnt": 0, "seq": i+1])).array;
while (envs.any!(env => env.seq > 1)) {
foreach (e; envs) {
mixin(code);
}
writeln;
}
writefln("Counts:\n%(% 4d%)", envs.map!(env => env.cnt));
}

View file

@ -0,0 +1,18 @@
(define (bump-value)
(when (> value 1)
(set! count (1+ count))
(set! value (if (even? value) (/ value 2) (1+ (* 3 value))))))
(define (env-show name envs )
(write name)
(for ((env envs)) (write (format "%4a" (eval name env))))
(writeln))
(define (task (envnum 12))
(define envs (for/list ((i envnum)) (environment-new `((value ,(1+ i)) (count 0)))))
(env-show 'value envs)
(while
(any (curry (lambda ( n env) (!= 1 (eval n env))) 'value) envs)
(for/list ((env envs)) (eval '(bump-value) env))
(env-show 'value envs))
(env-show 'count envs))

View file

@ -0,0 +1,59 @@
-module( first_class_environments ).
-export( [task/0] ).
task() ->
Print_pid = erlang:spawn( fun() -> print_loop() end ),
Environments = lists:seq( 1, 12 ),
Print_pid ! "Environment: Sequence",
Pids = [erlang:spawn(fun() -> hailstone_in_environment(Print_pid, X) end) || X <- Environments],
Counts = counts( Pids ),
Print_pid ! "{Environment, Step count}",
Print_pid ! lists:flatten( io_lib:format("~p", [Counts]) ),
ok.
counts( Pids ) ->
My_pid = erlang:self(),
[X ! {count, My_pid} || X <- Pids],
counts( Pids, [] ).
counts( [], Acc ) -> Acc;
counts( Pids, Acc ) ->
receive
{count, N, Count, Pid} -> counts( lists:delete(Pid, Pids), [{N, Count} | Acc] )
end.
hailstone_in_environment( Print_pid, N ) ->
erlang:put( hailstone_value, N ),
erlang:put( count, 0 ),
hailstone_loop( hailstone_loop_done(N), Print_pid, N, [N] ).
hailstone_loop( stop, Print_pid, N, Acc ) ->
Environment = lists:flatten( io_lib:format("~11B:", [N]) ),
Sequence = lists:flatten( [io_lib:format("~4B", [X]) || X <- lists:reverse(Acc)] ),
Print_pid ! Environment ++ Sequence,
Count= erlang:get( count ),
receive
{count, Pid} -> Pid ! {count, N, Count, erlang:self()}
end;
hailstone_loop( keep_going, Print_pid, N, Acc ) ->
Next = hailstone_next( erlang:get(hailstone_value) ),
erlang:put( hailstone_value, Next ),
Count = erlang:get( count ),
erlang:put( count, Count + 1 ),
hailstone_loop( hailstone_loop_done(Next), Print_pid, N, [Next | Acc] ).
hailstone_loop_done( 1 ) -> stop;
hailstone_loop_done( _N ) -> keep_going.
hailstone_next( 1 ) -> 1;
hailstone_next( Even ) when (Even rem 2) =:= 0 -> Even div 2;
hailstone_next( Odd ) -> (3 * Odd) + 1.
print_loop() ->
receive
String -> io:fwrite("~s~n", [String] )
end,
print_loop().

View file

@ -0,0 +1,21 @@
USING: assocs continuations formatting io kernel math
math.ranges sequences ;
: (next-hailstone) ( count value -- count' value' )
[ 1 + ] [ dup even? [ 2/ ] [ 3 * 1 + ] if ] bi* ;
: next-hailstone ( count value -- count' value' )
dup 1 = [ (next-hailstone) ] unless ;
: make-environments ( -- seq ) 12 [ 0 ] replicate 12 [1,b] zip ;
: step ( seq -- new-seq )
[ [ dup "%4d " printf next-hailstone ] with-datastack ] map
nl ;
: done? ( seq -- ? ) [ second 1 = ] all? ;
make-environments
[ dup done? ] [ step ] until nl
"Counts:" print
[ [ drop "%4d " printf ] with-datastack drop ] each nl

View file

@ -0,0 +1,59 @@
package main
import "fmt"
const jobs = 12
type environment struct{ seq, cnt int }
var (
env [jobs]environment
seq, cnt *int
)
func hail() {
fmt.Printf("% 4d", *seq)
if *seq == 1 {
return
}
(*cnt)++
if *seq&1 != 0 {
*seq = 3*(*seq) + 1
} else {
*seq /= 2
}
}
func switchTo(id int) {
seq = &env[id].seq
cnt = &env[id].cnt
}
func main() {
for i := 0; i < jobs; i++ {
switchTo(i)
env[i].seq = i + 1
}
again:
for i := 0; i < jobs; i++ {
switchTo(i)
hail()
}
fmt.Println()
for j := 0; j < jobs; j++ {
switchTo(j)
if *seq != 1 {
goto again
}
}
fmt.Println()
fmt.Println("COUNTS:")
for i := 0; i < jobs; i++ {
switchTo(i)
fmt.Printf("% 4d", *cnt)
}
fmt.Println()
}

View file

@ -0,0 +1,4 @@
hailstone n
| n == 1 = 1
| even n = n `div` 2
| odd n = 3*n + 1

View file

@ -0,0 +1,2 @@
data Environment = Environment { count :: Int, value :: Int }
deriving Eq

View file

@ -0,0 +1 @@
environments = [ Environment 0 n | n <- [1..12] ]

View file

@ -0,0 +1,2 @@
process (Environment c 1) = Environment c 1
process (Environment c n) = Environment (c+1) (hailstone n)

View file

@ -0,0 +1,5 @@
process = execState $ do
n <- gets value
c <- gets count
when (n > 1) $ modify $ \env -> env { count = c + 1 }
modify $ \env -> env { value = hailstone n }

View file

@ -0,0 +1,13 @@
fixedPoint f x
| fx == x = [x]
| otherwise = x : fixedPoint f fx
where fx = f x
prettyPrint field = putStrLn . foldMap (format.field)
where format n = (if n < 10 then " " else "") ++ show n ++ " "
main = do
let result = fixedPoint (map process) environments
mapM_ (prettyPrint value) result
putStrLn (replicate 36 '-')
prettyPrint count (last result)

View file

@ -0,0 +1,6 @@
main = do
let result = map (fixedPoint process) environments
mapM_ (prettyPrint value) result
putStrLn (replicate 36 '-')
putStrLn "Counts: "
prettyPrint (count . last) result

View file

@ -0,0 +1,24 @@
link printf
procedure main()
every put(environment := [], hailenv(1 to 12,0)) # setup environments
printf("Sequences:\n")
while (e := !environment).sequence > 1 do {
every hailstep(!environment)
printf("\n")
}
printf("\nCounts:\n")
every printf("%4d ",(!environment).count)
printf("\n")
end
record hailenv(sequence,count)
procedure hailstep(env)
printf("%4d ",env.sequence)
if env.sequence ~= 1 then {
env.count +:= 1
if env.sequence % 2 = 0 then env.sequence /:= 2
else env.sequence := 3 * env.sequence + 1
}
end

View file

@ -0,0 +1,35 @@
coclass 'hailstone'
step=:3 :0
NB. and determine next element in hailstone sequence
if.1=N do. N return.end.
NB. count how many times this has run when N was not 1
STEP=:STEP+1
if.0=2|N do.
N=: N%2
else.
N=: 1 + 3*N
end.
)
create=:3 :0
STEP=: 0
N=: y
)
current=:3 :0
N__y
)
run1=:3 :0
step__y''
STEP__y
)
run=:3 :0
old=: ''
while. -. old -: state=: run1"0 y do.
smoutput 4j0 ": current"0 y
old=: state
end.
)

View file

@ -0,0 +1,22 @@
environments=: conew&'hailstone'"0 (1+i.12)
run_hailstone_ environments
1 1 10 2 16 3 22 4 28 5 34 6
1 1 5 1 8 10 11 2 14 16 17 3
1 1 16 1 4 5 34 1 7 8 52 10
1 1 8 1 2 16 17 1 22 4 26 5
1 1 4 1 1 8 52 1 11 2 13 16
1 1 2 1 1 4 26 1 34 1 40 8
1 1 1 1 1 2 13 1 17 1 20 4
1 1 1 1 1 1 40 1 52 1 10 2
1 1 1 1 1 1 20 1 26 1 5 1
1 1 1 1 1 1 10 1 13 1 16 1
1 1 1 1 1 1 5 1 40 1 8 1
1 1 1 1 1 1 16 1 20 1 4 1
1 1 1 1 1 1 8 1 10 1 2 1
1 1 1 1 1 1 4 1 5 1 1 1
1 1 1 1 1 1 2 1 16 1 1 1
1 1 1 1 1 1 1 1 8 1 1 1
1 1 1 1 1 1 1 1 4 1 1 1
1 1 1 1 1 1 1 1 2 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1
0 1 7 2 5 8 16 3 19 6 14 9

View file

@ -0,0 +1,5 @@
def code:
# Given an integer as input, compute the corresponding hailstone value:
def hail: if . % 2 == 0 then ./2|floor else 3*. + 1 end;
if .value > 1 then (.value |= hail) | .count += 1 else . end;

View file

@ -0,0 +1,3 @@
filter_and_last( generate;
map(.value) | @tsv;
"", "Counts:", (map(.count) | @tsv ))

View file

@ -0,0 +1,55 @@
const jobs = 12
mutable struct Environment
seq::Int
cnt::Int
Environment() = new(0, 0)
end
const env = [Environment() for i in 1:jobs]
const currentjob = [1]
seq() = env[currentjob[1]].seq
cnt() = env[currentjob[1]].cnt
seq(n) = (env[currentjob[1]].seq = n)
cnt(n) = (env[currentjob[1]].cnt = n)
function hail()
print(lpad(seq(), 4))
if seq() == 1
return
end
cnt(cnt() + 1)
seq(isodd(seq()) ? 3 * seq() + 1 : div(seq(), 2))
end
function runtest()
for i in 1:jobs
currentjob[1] = i
env[i].seq = i
end
computing = true
while computing
for i in 1:jobs
currentjob[1] = i
hail()
end
println()
for j in 1:jobs
currentjob[1] = j
if seq() != 1
break
elseif j == jobs
computing = false
end
end
end
println("\nCOUNTS:")
for i in 1:jobs
currentjob[1] = i
print(lpad(cnt(), 4))
end
println()
end
runtest()

View file

@ -0,0 +1,56 @@
// version 1.1.3
class Environment(var seq: Int, var count: Int)
const val JOBS = 12
val envs = List(JOBS) { Environment(it + 1, 0) }
var seq = 0 // 'seq' for current environment
var count = 0 // 'count' for current environment
var currId = 0 // index of current environment
fun switchTo(id: Int) {
if (id != currId) {
envs[currId].seq = seq
envs[currId].count = count
currId = id
}
seq = envs[id].seq
count = envs[id].count
}
fun hailstone() {
print("%4d".format(seq))
if (seq == 1) return
count++
seq = if (seq % 2 == 1) 3 * seq + 1 else seq / 2
}
val allDone get(): Boolean {
for (a in 0 until JOBS) {
switchTo(a)
if (seq != 1) return false
}
return true
}
fun code() {
do {
for (a in 0 until JOBS) {
switchTo(a)
hailstone()
}
println()
}
while (!allDone)
println("\nCOUNTS:")
for (a in 0 until JOBS) {
switchTo(a)
print("%4d".format(count))
}
println()
}
fun main(args: Array<String>) {
code()
}

View file

@ -0,0 +1,33 @@
local envs = { }
for i = 1, 12 do
-- fallback to the global environment for io and math
envs[i] = setmetatable({ count = 0, n = i }, { __index = _G })
end
local code = [[
io.write(("% 4d"):format(n))
if n ~= 1 then
count = count + 1
n = (n % 2 == 1) and 3 * n + 1 or math.floor(n / 2)
end
]]
while true do
local finished = 0
for _, env in ipairs(envs) do
if env.n == 1 then finished = finished + 1 end
end
if finished == #envs then break end
for _, env in ipairs(envs) do
-- 5.1; in 5.2, use load(code, nil, nil, env)() instead
setfenv(loadstring(code), env)()
end
io.write "\n"
end
print "counts:"
for _, env in ipairs(envs) do
io.write(("% 4d"):format(env.count))
end

View file

@ -0,0 +1,58 @@
import strformat
const Jobs = 12
type Environment = object
sequence: int
count: int
var
env: array[Jobs, Environment]
sequence, count: ptr int
#---------------------------------------------------------------------------------------------------
proc hail() =
stdout.write fmt"{sequence[]: 4d}"
if sequence[] == 1: return
inc count[]
sequence[] = if (sequence[] and 1) != 0: 3 * sequence[] + 1
else: sequence[] div 2
#---------------------------------------------------------------------------------------------------
proc switchTo(id: int) =
sequence = addr(env[id].sequence)
count = addr(env[id].count)
#---------------------------------------------------------------------------------------------------
template forAllJobs(statements: untyped): untyped =
for i in 0..<Jobs:
switchTo(i)
statements
#———————————————————————————————————————————————————————————————————————————————————————————————————
for i in 0..<Jobs:
switchTo(i)
env[i].sequence = i + 1
var terminated = false
while not terminated:
forAllJobs:
hail()
echo ""
terminated = true
forAllJobs:
if sequence[] != 1:
terminated = false
break
echo ""
echo "Counts:"
forAllJobs:
stdout.write fmt"{count[]: 4d}"
echo ""

View file

@ -0,0 +1,44 @@
#include <order/interpreter.h>
#define ORDER_PP_DEF_8hail ORDER_PP_FN( \
8fn(8N, 8cond((8equal(8N, 1), 1) \
(8is_0(8remainder(8N, 2)), 8quotient(8N, 2)) \
(8else, 8inc(8times(8N, 3))))) )
#define ORDER_PP_DEF_8h_loop ORDER_PP_FN( \
8fn(8S, \
8let((8F, 8fn(8E, 8env_ref(8(8H), 8E))), \
8do( \
8print(8seq_to_tuple(8seq_map(8F, 8S)) 8space), \
8let((8S, 8h_once(8S)), \
8if(8equal(1, \
8seq_fold(8times, 1, 8seq_map(8F, 8S))), \
8print_counts(8S), \
8h_loop(8S)))))) )
#define ORDER_PP_DEF_8h_once ORDER_PP_FN( \
8fn(8S, \
8seq_map( \
8fn(8E, \
8eval(8E, \
8quote( \
8env_bind(8(8C), \
8env_bind(8(8H), \
8env_bind(8(8E), 8E, 8E), \
8hail(8H)), \
8if(8equal(8H, 1), 8C, 8inc(8C))) ))), \
8S)) )
#define ORDER_PP_DEF_8print_counts ORDER_PP_FN( \
8fn(8S, \
8print(8space 8(Counts:) \
8seq_to_tuple(8seq_map(8fn(8E, 8env_ref(8(8C), 8E)), 8S)))) )
ORDER_PP(
8let((8S, // Build a list of environments
8seq_map(8fn(8N, 8seq_of_pairs_to_env(
8seq(8pair(8(8H), 8N), 8pair(8(8C), 0),
8pair(8(8E), 8env_nil)))),
8seq_iota(1, 13))),
8h_loop(8S))
)

View file

@ -0,0 +1 @@
(1,2,3,4,5,6,7,8,9,10,11,12) (1,1,10,2,16,3,22,4,28,5,34,6) (1,1,5,1,8,10,11,2,14,16,17,3) (1,1,16,1,4,5,34,1,7,8,52,10) (1,1,8,1,2,16,17,1,22,4,26,5) (1,1,4,1,1,8,52,1,11,2,13,16) (1,1,2,1,1,4,26,1,34,1,40,8) (1,1,1,1,1,2,13,1,17,1,20,4) (1,1,1,1,1,1,40,1,52,1,10,2) (1,1,1,1,1,1,20,1,26,1,5,1) (1,1,1,1,1,1,10,1,13,1,16,1) (1,1,1,1,1,1,5,1,40,1,8,1) (1,1,1,1,1,1,16,1,20,1,4,1) (1,1,1,1,1,1,8,1,10,1,2,1) (1,1,1,1,1,1,4,1,5,1,1,1) (1,1,1,1,1,1,2,1,16,1,1,1) (1,1,1,1,1,1,1,1,8,1,1,1) (1,1,1,1,1,1,1,1,4,1,1,1) (1,1,1,1,1,1,1,1,2,1,1,1) Counts:(0,1,7,2,5,8,16,3,19,6,14,9)

View file

@ -0,0 +1,40 @@
use strict;
use warnings;
use Safe;
sub hail_next {
my $n = shift;
return 1 if $n == 1;
return $n * 3 + 1 if $n % 2;
$n / 2;
};
my @enviornments;
for my $initial ( 1..12 ) {
my $env = Safe->new;
${ $env->varglob('value') } = $initial;
${ $env->varglob('count') } = 0;
$env->share('&hail_next');
$env->reval(q{
sub task {
return if $value == 1;
$value = hail_next( $value );
++$count;
}
});
push @enviornments, $env;
}
my @value_refs = map $_->varglob('value'), @enviornments;
my @tasks = map $_->varglob('task'), @enviornments;
while( grep { $$_ != 1 } @value_refs ) {
printf "%4s", $$_ for @value_refs;
print "\n";
$_->() for @tasks;
}
print "Counts\n";
printf "%4s", ${$_->varglob('count')} for @enviornments;
print "\n";

View file

@ -0,0 +1,51 @@
(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">hail</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">/=</span> <span style="color: #000000;">2</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">*</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">n</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">hails</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">12</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">counts</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">results</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">columnize</span><span style="color: #0000FF;">({</span><span style="color: #000000;">hails</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">step</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">edx</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">hails</span><span style="color: #0000FF;">[</span><span style="color: #000000;">edx</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">hail</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">hails</span><span style="color: #0000FF;">[</span><span style="color: #000000;">edx</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span>
<span style="color: #000000;">counts</span><span style="color: #0000FF;">[</span><span style="color: #000000;">edx</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #000000;">results</span><span style="color: #0000FF;">[</span><span style="color: #000000;">edx</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">results</span><span style="color: #0000FF;">[</span><span style="color: #000000;">edx</span><span style="color: #0000FF;">])</span> <span style="color: #0000FF;">&</span> <span style="color: #000000;">n</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<span style="color: #004080;">bool</span> <span style="color: #000000;">done</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
<span style="color: #008080;">while</span> <span style="color: #008080;">not</span> <span style="color: #000000;">done</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">done</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">12</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">step</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">done</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #000000;">counts</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">12</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;"><=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">results</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">])?</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%4d"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">results</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">][</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]}):</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"==="</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12</span><span style="color: #0000FF;">))})</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">12</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%4d"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">counts</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--

View file

@ -0,0 +1,64 @@
(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">hail</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">/=</span> <span style="color: #000000;">2</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">*</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">n</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">step</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">edx</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">getd</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"hail"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">edx</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">hail</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">setd</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"hail"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">edx</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">setd</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"count"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">getd</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"count"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">edx</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">edx</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">setd</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"results"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">getd</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"results"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">edx</span><span style="color: #0000FF;">))&</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">edx</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">dicts</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">12</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">new_dict</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">setd</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"hail"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">setd</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"count"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">setd</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"results"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">},</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">dicts</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">d</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #004080;">bool</span> <span style="color: #000000;">done</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
<span style="color: #008080;">while</span> <span style="color: #008080;">not</span> <span style="color: #000000;">done</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">done</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">12</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">step</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dicts</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">done</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #000000;">done</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">while</span> <span style="color: #008080;">not</span> <span style="color: #000000;">done</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">done</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">12</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">getd</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"results"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dicts</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;"><</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #000000;">done</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;"><=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)?</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%4d"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]}):</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">i</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"==="</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12</span><span style="color: #0000FF;">))})</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">12</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">getd</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"count"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dicts</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">])</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%4d"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">count</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--

View file

@ -0,0 +1,20 @@
(let Envs
(mapcar
'((N) (list (cons 'N N) (cons 'Cnt 0))) # Build environments
(range 1 12) )
(while (find '((E) (job E (> N 1))) Envs) # Until all values are 1:
(for E Envs
(job E # Use environment 'E'
(prin (align 4 N))
(unless (= 1 N)
(inc 'Cnt) # Increment step count
(setq N
(if (bit? 1 N) # Calculate next hailstone value
(inc (* N 3))
(/ N 2) ) ) ) ) )
(prinl) )
(prinl (need 48 '=))
(for E Envs # For each environment 'E'
(job E
(prin (align 4 Cnt)) ) ) # print the step count
(prinl) )

View file

@ -0,0 +1,18 @@
environments = [{'cnt':0, 'seq':i+1} for i in range(12)]
code = '''
print('% 4d' % seq, end='')
if seq != 1:
cnt += 1
seq = 3 * seq + 1 if seq & 1 else seq // 2
'''
while any(env['seq'] > 1 for env in environments):
for env in environments:
exec(code, globals(), env)
print()
print('Counts')
for env in environments:
print('% 4d' % env['cnt'], end='')
print()

View file

@ -0,0 +1,16 @@
code <- quote(
if (n == 1) n else {
count <- count + 1;
n <- if (n %% 2 == 1) 3 * n + 1 else n/2
})
eprint <- function(envs, var="n")
cat(paste(sprintf("%4d", sapply(envs, `[[`, var)), collapse=" "), "\n")
envs <- mapply(function(...) list2env(list(...)), n=1:12, count=0)
while (any(sapply(envs, eval, expr=code) > 1)) {eprint(envs)}
eprint(envs)
cat("\nCounts:\n")
eprint(envs, "count")

View file

@ -0,0 +1,34 @@
/*REXX pgm illustrates N 1st─class environments (using numbers from a hailstone seq).*/
parse arg n . /*obtain optional argument from the CL.*/
if n=='' | n=="," then n= 12 /*Was N defined? No, then use default.*/
@.= /*initialize the array @. to nulls.*/
do i=1 for n; @.i= i /* " environments to an index. */
end /*i*/
w= length(n) /*width (so far) for columnar output.*/
do forever until @.0; @.0= 1 /* ◄─── process all the environments. */
do k=1 for n; x= hailstone(k) /*obtain next hailstone number in seq. */
w= max(w, length(x)) /*determine the maximum width needed. */
@.k= @.k x /* ◄─── where the rubber meets the road*/
end /*k*/
end /*forever*/
#= 0 /* [↓] display the tabular results. */
do lines=-1 until _=''; _= /*process a line for each environment. */
do j=1 for n /*process each of the environments. */
select /*determine how to process the line. */
when #== 1 then _= _ right(words(@.j) - 1, w) /*environment count.*/
when lines==-1 then _= _ right(j, w) /*the title (header)*/
when lines== 0 then _= _ right('', w, "") /*the separator line*/
otherwise _= _ right(word(@.j, lines), w)
end /*select*/
end /*j*/
if #==1 then #= 2 /*separator line? */
if _='' then #= # + 1 /*Null? Bump the #.*/
if #==1 then _= copies(" "left('', w, ""), N) /*the foot separator*/
if _\='' then say strip( substr(_, 2), "T") /*display the counts*/
end /*lines*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
hailstone: procedure expose @.; parse arg y; _= word(@.y, words(@.y) )
if _==1 then return ''; @.0= 0; if _//2 then return _*3 + 1; return _%2

View file

@ -0,0 +1,28 @@
#lang racket
(define namespaces
(for/list ([i (in-range 1 13)])
(define ns (make-base-namespace))
(eval `(begin (define N ,i) (define count 0)) ns)
ns))
(define (get-var-values name)
(map (curry namespace-variable-value name #t #f) namespaces))
(define code
'(when (> N 1)
(set! N (if (even? N) (/ N 2) (+ 1 (* N 3))))
(set! count (add1 count))))
(define (show-nums nums)
(for ([n nums]) (display (~a n #:width 4 #:align 'right)))
(newline))
(let loop ()
(define Ns (get-var-values 'N))
(show-nums Ns)
(unless (andmap (λ(n) (= n 1)) Ns)
(for ([ns namespaces]) (eval code ns))
(loop)))
(displayln (make-string (* 4 12) #\=))
(show-nums (get-var-values 'count))

View file

@ -0,0 +1,19 @@
my $calculator = sub ($n is rw) {
$n == 1 ?? 1 !! $n %% 2 ?? $n div 2 !! $n * 3 + 1
}
sub next (%this, &get_next) {
return %this if %this.<value> == 1;
%this.<value> .= &get_next;
%this.<count>++;
%this;
}
my @hailstones = map { %(value => $_, count => 0) }, 1 .. 12;
while not all( map { $_.<value> }, @hailstones ) == 1 {
say [~] map { $_.<value>.fmt: '%4s' }, @hailstones;
@hailstones[$_] .= &next($calculator) for ^@hailstones;
}
say "\nCounts\n" ~ [~] map { $_.<count>.fmt: '%4s' }, @hailstones;

View file

@ -0,0 +1,29 @@
# Build environments
envs = (1..12).map do |n|
Object.new.instance_eval {@n = n; @cnt = 0; self}
end
# Until all values are 1:
until envs.all? {|e| e.instance_eval{@n} == 1}
envs.each do |e|
e.instance_eval do # Use environment _e_
printf "%4s", @n
if @n > 1
@cnt += 1 # Increment step count
@n = if @n.odd? # Calculate next hailstone value
@n * 3 + 1
else
@n / 2
end
end
end
end
puts
end
puts '=' * 48
envs.each do |e| # For each environment _e_
e.instance_eval do
printf "%4s", @cnt # print the step count
end
end
puts

View file

@ -0,0 +1,36 @@
# Build environments
envs = (1..12).map do |n|
e = class Object
# This is a new lexical scope with no local variables.
# Create a new binding here.
binding
end
eval(<<-EOS, e).call(n)
n, cnt = nil, 0
proc {|arg| n = arg}
EOS
e
end
# Until all values are 1:
until envs.all? {|e| eval('n == 1', e)}
envs.each do |e|
eval(<<-EOS, e) # Use environment _e_
printf "%4s", n
if n > 1
cnt += 1 # Increment step count
n = if n.odd? # Calculate next hailstone value
n * 3 + 1
else
n / 2
end
end
EOS
end
puts
end
puts '=' * 48
envs.each do |e| # For each environment _e_
eval('printf "%4s", cnt', e) # print the step count
end
puts

View file

@ -0,0 +1,25 @@
func calculator({.is_one} ) { 1 }
func calculator(n {.is_even}) { n / 2 }
func calculator(n ) { 3*n + 1 }
func succ(this {_{:value}.is_one}, _) {
return this
}
func succ(this, get_next) {
this{:value} = get_next(this{:value})
this{:count}++
return this
}
var enviornments = (1..12 -> map {|i| Hash(value => i, count => 0) });
while (!enviornments.map{ _{:value} }.all { .is_one }) {
say enviornments.map {|h| "%4s" % h{:value} }.join;
enviornments.range.each { |i|
enviornments[i] = succ(enviornments[i], calculator);
}
}
say 'Counts';
say enviornments.map{ |h| "%4s" % h{:count} }.join;

View file

@ -0,0 +1,31 @@
package require Tcl 8.5
for {set i 1} {$i <= 12} {incr i} {
dict set hailenv hail$i [dict create num $i steps 0]
}
while 1 {
set loopagain false
foreach k [dict keys $hailenv] {
dict with hailenv $k {
puts -nonewline [format %4d $num]
if {$num == 1} {
continue
} elseif {$num & 1} {
set num [expr {3*$num + 1}]
} else {
set num [expr {$num / 2}]
}
set loopagain true
incr steps
}
}
puts ""
if {!$loopagain} break
}
puts "Counts..."
foreach k [dict keys $hailenv] {
dict with hailenv $k {
puts -nonewline [format %4d $steps]
}
}
puts ""

View file

@ -0,0 +1,42 @@
import "/fmt" for Fmt
var environment = Fn.new {
class E {
construct new(value, count) {
_value = value
_count = count
}
value { _value }
count { _count }
hailstone() {
Fmt.write("$4d", _value)
if (_value == 1) return
_count = _count + 1
_value = (_value%2 == 0) ? _value/2 : 3*_value + 1
}
}
return E
}
// create and initialize the environments
var jobs = 12
var envs = List.filled(jobs, null)
for (i in 0...jobs) envs[i] = environment.call().new(i+1, 0)
System.print("Sequences:")
var done = false
while (!done) {
for (env in envs) env.hailstone()
System.print()
done = true
for (env in envs) {
if (env.value != 1) {
done = false
break
}
}
}
System.print("Counts:")
for (env in envs) Fmt.write("$4d", env.count)
System.print()

View file

@ -0,0 +1,11 @@
class Env{
var n,cnt=0;
fcn init(_n){n=_n; returnClass(self.f)}
fcn f{
if(n!=1){
cnt += 1;
if(n.isEven) n=n/2; else n=n*3+1;
}
n
}
}

View file

@ -0,0 +1,8 @@
var es=(1).pump(12,List,Env);
while(1){
ns:=es.run(True);
ns.pump(String,"%4d".fmt).println();
if (not ns.filter('!=(1))) break;
}
println("Counts:");
es.pump(String,fcn(e){"%4d".fmt(e.container.cnt)}).println();