Knuth's algorithm S: Difference between revisions

no edit summary
No edit summary
Line 279:
[30075 29955 30024 30095 30031 30018 29973 29642 30156 30031]
</pre>
 
=={{header|Icon}} and {{header|Unicon}}==
 
The following solution makes use of the <tt>makeProc</tt> procedure
defined in the <tt>UniLib</tt> library and so is Unicon specific. However,
the solution can be modified to work in Icon as well.
<lang unicon>import Utils
 
procedure main(A)
freq := table(0)
every 1 to 100000 do {
s_of_n := s_of_n_creator(\A[1] | 3)
every sample := s_of_n(0 to 9)
every freq[!sample] +:= 1
}
every write(i := 0 to 9,": ",right(freq[i],6))
end
 
procedure s_of_n_creator(n)
items := []
itemCnt := 0.0
return makeProc {
repeat {
item := (items@&source)[1]
itemCnt +:= 1
if *items < n then put(items, item)
else if ?0 < (n/itemCnt) then ?items := item
}
}
end</lang>
 
=={{header|J}}==