Knuth's algorithm S: Difference between revisions

J
(J)
Line 44:
(funcall f)))
finally (prin1 freq))</lang>output<lang>#(30026 30023 29754 30017 30267 29997 29932 29990 29965 30029)</lang>
 
=={{header|J}}==
 
Note that this approach introduces heavy inefficiencies, to achieve information hiding.
 
<lang j>coclass'inefficient'
create=:3 :0
N=:y
ITEMS=:''
K=:0
)
 
s_of_n=:3 :0
K=:K+1
if.N>#ITEMS do.
ITEMS=:ITEMS,y
else.
if.(N%K)>?0 do.
ITEMS=:(((i.#ITEMS)-.?N){ITEMS),y
else.
ITEMS
end.
end.
)
 
 
s_of_n_creator_base_=: 1 :0
ctx=: conew&'inefficient' m
s_of_n__ctx
)</lang>
 
Required example:
 
<lang j>run=:3 :0
nl=. conl 1
s3_of_n=. 3 s_of_n_creator
r=. {: s3_of_n"0 i.10
coerase (conl 1)-.nl
r
)
 
(~.,._1 + #/.~) (i.10),,D=:run"0 i.1e5
0 30099
1 29973
2 29795
3 29995
4 29996
5 30289
6 29903
7 29993
8 30215
9 29742</lang>
 
=={{header|Python}}==
6,962

edits