September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -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;
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
filter_and_last( generate;
|
||||
map(.value) | @tsv;
|
||||
"", "Counts:", (map(.count) | @tsv ))
|
||||
|
|
@ -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()
|
||||
}
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
function hail(integer n)
|
||||
if remainder(n,2)=0 then
|
||||
n /= 2
|
||||
else
|
||||
n = 3*n+1
|
||||
end if
|
||||
return n
|
||||
end function
|
||||
|
||||
sequence hails = tagset(12),
|
||||
counts = repeat(0,12),
|
||||
results = columnize({hails})
|
||||
|
||||
function step(integer edx)
|
||||
integer n = hails[edx]
|
||||
if n=1 then return 0 end if
|
||||
n = hail(n)
|
||||
hails[edx] = n
|
||||
counts[edx] += 1
|
||||
results[edx] &= n
|
||||
return 1
|
||||
end function
|
||||
|
||||
procedure main()
|
||||
bool done = false
|
||||
while not done do
|
||||
done = true
|
||||
for i=1 to 12 do
|
||||
if step(i) then
|
||||
done = false
|
||||
end if
|
||||
end for
|
||||
end while
|
||||
|
||||
for i=1 to max(counts)+1 do
|
||||
for j=1 to 12 do
|
||||
puts(1,iff(i<=length(results[j])?sprintf("%4d",{results[j][i]}):" "))
|
||||
end for
|
||||
puts(1,"\n")
|
||||
end for
|
||||
printf(1," %s\n",{join(repeat("===",12))})
|
||||
for j=1 to 12 do
|
||||
printf(1,"%4d",{counts[j]})
|
||||
end for
|
||||
puts(1,"\n")
|
||||
end procedure
|
||||
|
||||
main()
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
function hail(integer n)
|
||||
if remainder(n,2)=0 then
|
||||
n /= 2
|
||||
else
|
||||
n = 3*n+1
|
||||
end if
|
||||
return n
|
||||
end function
|
||||
|
||||
function step(integer edx)
|
||||
integer n = getd("hail",edx)
|
||||
if n=1 then return 0 end if
|
||||
n = hail(n)
|
||||
setd("hail",n,edx)
|
||||
setd("count",getd("count",edx)+1,edx)
|
||||
setd("results",getd("results",edx)&n,edx)
|
||||
return 1
|
||||
end function
|
||||
|
||||
sequence dicts = {}
|
||||
|
||||
procedure main()
|
||||
for i=1 to 12 do
|
||||
integer d = new_dict()
|
||||
setd("hail",i,d)
|
||||
setd("count",0,d)
|
||||
setd("results",{i},d)
|
||||
dicts &= d
|
||||
end for
|
||||
|
||||
bool done = false
|
||||
while not done do
|
||||
done = true
|
||||
for i=1 to 12 do
|
||||
if step(dicts[i]) then
|
||||
done = false
|
||||
end if
|
||||
end for
|
||||
end while
|
||||
|
||||
done = false
|
||||
integer i = 1
|
||||
while not done do
|
||||
done = true
|
||||
for j=1 to 12 do
|
||||
sequence res = getd("results",dicts[j])
|
||||
if i<length(res) then done = false end if
|
||||
puts(1,iff(i<=length(res)?sprintf("%4d",{res[i]}):" "))
|
||||
end for
|
||||
puts(1,"\n")
|
||||
i += 1
|
||||
end while
|
||||
printf(1," %s\n",{join(repeat("===",12))})
|
||||
for j=1 to 12 do
|
||||
integer count = getd("count",dicts[j])
|
||||
printf(1,"%4d",{count})
|
||||
end for
|
||||
puts(1,"\n")
|
||||
end procedure
|
||||
|
||||
main()
|
||||
|
|
@ -1,32 +1,32 @@
|
|||
/*REXX program illustrates first-class environments (using hailstone #s)*/
|
||||
parse arg N .; if N=='' then N=12 /*Was N defined? No, use default.*/
|
||||
w=length(N) /*the width (so far) for columns.*/
|
||||
/*REXX program illustrates 1st─class environments (using the numbers from hailstone seq)*/
|
||||
parse arg N . /*obtain optional argument from the CL.*/
|
||||
if N=='' | N=="," then N=12 /*Was N defined? No, then use default.*/
|
||||
w=length(N) /*width (so far) for columnar output.*/
|
||||
@.=
|
||||
do init=1 for N; @.init=init; end /*initialize all the environments*/
|
||||
do i=1 for N; @.i=i; end /*i*/ /*initialize all the environments. */
|
||||
|
||||
do forever until @.0; @.0=1 /* ◄─── process all environments.*/
|
||||
do k=1 for N; x=hailstone(k); w=max(w,length(x))
|
||||
@.k=@.k x /*where the rubber meets the road*/
|
||||
end /*k*/
|
||||
end /*forever*/
|
||||
|
||||
count=0 /* [↓] show tabular results. */
|
||||
do lines=-1 until _==''; _= /*process each line for each env.*/
|
||||
do j=1 for N /*process each environment. */
|
||||
select /*choose how to process the line.*/
|
||||
when count== 1 then _=_ right(words(@.j)-1, w)
|
||||
when lines==-1 then _=_ right(j, w) /*hdr.*/
|
||||
when lines== 0 then _=_ right('', w, '─') /*sep.*/
|
||||
otherwise _=_ right(word(@.j, lines), w)
|
||||
end /*select*/
|
||||
end /*j*/
|
||||
|
||||
if count==1 then count=2
|
||||
_=strip(_,'T'); if _=='' then count=count+1 /*if null, bump*/
|
||||
if count==1 then _=copies(" "left('', w, "═"), N) /*foot sep*/
|
||||
if _\=='' then say substr(_,2) /*show the (foot) counts.*/
|
||||
end /*lines*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*─────────────────────────────────────HAILSTONE (Collatz) subroutine───*/
|
||||
hailstone: procedure expose @.; parse arg y; _=word(@.y, words(@.y))
|
||||
if _==1 then return '' ; @.0=0; if _//2==0 then return _%2; return _*3+1
|
||||
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)
|
||||
when lines==-1 then _=_ right(j, w) /*the header. */
|
||||
when lines== 0 then _=_ right('', w, "─") /*the separator. */
|
||||
otherwise _=_ right(word(@.j, lines), w)
|
||||
end /*select*/
|
||||
end /*j*/
|
||||
if #==1 then #=2
|
||||
if _='' then #=# + 1 /*Null? Bump #. */
|
||||
if #==1 then _=copies(" "left('', w, "═"), N) /*foot separator.*/
|
||||
if _\='' then say strip( substr(_, 2), "T") /*display 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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
@ -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();
|
||||
Loading…
Add table
Add a link
Reference in a new issue