Anonymous user
Combinations with repetitions/Square digit chain: Difference between revisions
Combinations with repetitions/Square digit chain (view source)
Revision as of 10:06, 8 September 2016
, 7 years ago→{{header|zkl}}: fix stupid
(→{{header|zkl}}: get lazy) |
(→{{header|zkl}}: fix stupid) |
||
Line 181:
=={{header|zkl}}==
{{trans|Ruby}}
<lang zkl>
fcn countNumberChains(K){
F:=(K+1).pump(List,fcn(n){ (1).reduce(n,'*,1) }); #Some small factorials
g:=fcn(n){
Line 191 ⟶ 192:
n,G:=K*81+1,n.pump(List,g);
N:=n.pump(List,'wrap(n){ n=g(n); while(n>1){ n=G[n] } n });
[0..9].pump(List,fcn(n){ n*n }):Utils.Helpers.combosKW(K,_) # combos of (0,1,4,9,16,25,36,49,64,81)
.pump(Void,'wrap(ds){ #Iterate over unique digit combinations
Line 203 ⟶ 204:
println("%,d numbers produce 1 and %,d numbers produce 89".fmt(z,(10).pow(K)-1-z));
}</lang>
combosKW(k,sequence) is lazy, which, in this case, is quite a bit faster than the non-lazy version.
<lang zkl>foreach K in (T(7,8,11,14,17)){ countNumberChains(K) }</lang>
{{out}}
Line 210 ⟶ 212:
k=(8) in the range 1 to 99,999,999
k=(11) in the range 1 to 99,999,999,999
15,
k=(14) in the range 1 to 99,999,999,999,999
13,
k=(17) in the range 1 to 99,999,999,999,999,999
12,
</pre>
|