Combinations with repetitions/Square digit chain: Difference between revisions

(→‎{{header|zkl}}: fix stupid)
Line 181:
=={{header|zkl}}==
{{trans|Ruby}}
<lang zkl>fcnvar countNumberChains(K){z; // I don't like this but I'm lazy
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 });
var z=0; #Running count of numbers translating to 1
[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
1514,674255,519666 numbers produce 1 and 8485,325744,480333 numbers produce 89
 
k=(11) in the range 1 to 99,999,999,999
15,106091,873199,875356 numbers produce 1 and 84,893908,126800,124643 numbers produce 89
 
k=(14) in the range 1 to 99,999,999,999,999
13,785770,960853,153279,559684 numbers produce 1 and 86,214229,039146,846720,440315 numbers produce 89
 
k=(17) in the range 1 to 99,999,999,999,999,999
12,038024,482696,364404,921768,583024 numbers produce 1 and 87,961975,517303,635595,078231,416975 numbers produce 89
</pre>
Anonymous user