September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,64 @@
function isMainOrInclude()
-- returns 1 if called from the main file, 0 if from an include
integer res
#ilASM{
[32]
mov eax,[ebp+20] -- prev_ebp
mov eax,[eax+8] -- rtn
mov [res],eax
[64]
mov rax,[rbp+40] -- prev_ebp
mov rax,[rax+16] -- rtn
mov [res],rax
[]
}
return res=21 -- (21=T_maintls)
end function
--global (if you want to be able to call this from test.exw)
function hailstone(atom n)
sequence s = {n}
while n!=1 do
if remainder(n,2)=0 then
n /= 2
else
n = 3*n+1
end if
s &= n
end while
return s
end function
global function hailstone_count(atom n)
integer count = 1
while n!=1 do
if remainder(n,2)=0 then
n /= 2
else
n = 3*n+1
end if
count += 1
end while
return count
end function
if isMainOrInclude() then
sequence s = hailstone(27)
integer ls = length(s)
s[5..-5] = {".."}
puts(1,"hailstone(27) = ")
? s
printf(1,"length = %d\n\n",ls)
integer hmax = 1, imax = 1,count
for i=2 to 1e5-1 do
count = hailstone_count(i)
if count>hmax then
hmax = count
imax = i
end if
end for
printf(1,"The longest hailstone sequence under 100,000 is %d with %d elements.\n",{imax,hmax})
end if

View file

@ -0,0 +1,11 @@
include hail.exw
sequence counts = {}
for i=1 to 100000 do
integer l = hailstone_count(i)
if length(counts)<l then
counts &= repeat(0,l-length(counts))
end if
counts[l] += 1
end for
printf(1,"The hailstone length returned most often between 1 and 100,000 is %d.\n",{largest(counts,1)})

View file

@ -0,0 +1,12 @@
fcn collatz(n,z=L()){ z.append(n); if(n==1) return(z);
if(n.isEven) return(self.fcn(n/2,z)); return(self.fcn(n*3+1,z)) }
h27:=collatz(27);
println("Hailstone(27)-->",h27[0,4].concat(","),"...",
h27[-4,*].concat(",")," length ",h27.len());
[2..0d100_000].pump(Void, // loop n from 2 to 100,000
collatz, // generate Collatz sequence(n)
fcn(c,n){ // if new longest sequence, save length/C, return longest
if(c.len()>n[0]) n.clear(c.len(),c[0]); n}.fp1(L(0,0)))
.println();

View file

@ -0,0 +1,12 @@
#!/home/craigd/Bin/zkl
collatz:=Import("hailstone",False,False,False).collatz; // don't run constructor
d:=Dictionary();
[2..0d100_000].pump(Void, // loop n from 2 to 100,000
collatz, // generate Collatz sequence(n)
'wrap(c){ d.incV(c.len()) } // save length
);
println("Number of different lengths: ",d.len());
longest:=(0).max(d.values);
mostFreqLen:=d.filter1('wrap([(k,v)]){ v==longest })[0];
println("Most frequent length: %d; %d sequences of that length."
.fmt(mostFreqLen,longest));