Combinations with repetitions/Square digit chain: Difference between revisions

Content added Content deleted
(→‎{{header|zkl}}: fix stupid)
Line 181: Line 181:
=={{header|zkl}}==
=={{header|zkl}}==
{{trans|Ruby}}
{{trans|Ruby}}
<lang zkl>fcn countNumberChains(K){
<lang zkl>var 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
F:=(K+1).pump(List,fcn(n){ (1).reduce(n,'*,1) }); #Some small factorials
g:=fcn(n){
g:=fcn(n){
Line 191: Line 192:
n,G:=K*81+1,n.pump(List,g);
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 });
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
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)
[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
.pump(Void,'wrap(ds){ #Iterate over unique digit combinations
Line 203: Line 204:
println("%,d numbers produce 1 and %,d numbers produce 89".fmt(z,(10).pow(K)-1-z));
println("%,d numbers produce 1 and %,d numbers produce 89".fmt(z,(10).pow(K)-1-z));
}</lang>
}</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>
<lang zkl>foreach K in (T(7,8,11,14,17)){ countNumberChains(K) }</lang>
{{out}}
{{out}}
Line 210: Line 212:


k=(8) in the range 1 to 99,999,999
k=(8) in the range 1 to 99,999,999
15,674,519 numbers produce 1 and 84,325,480 numbers produce 89
14,255,666 numbers produce 1 and 85,744,333 numbers produce 89


k=(11) in the range 1 to 99,999,999,999
k=(11) in the range 1 to 99,999,999,999
15,106,873,875 numbers produce 1 and 84,893,126,124 numbers produce 89
15,091,199,356 numbers produce 1 and 84,908,800,643 numbers produce 89


k=(14) in the range 1 to 99,999,999,999,999
k=(14) in the range 1 to 99,999,999,999,999
13,785,960,153,559 numbers produce 1 and 86,214,039,846,440 numbers produce 89
13,770,853,279,684 numbers produce 1 and 86,229,146,720,315 numbers produce 89


k=(17) in the range 1 to 99,999,999,999,999,999
k=(17) in the range 1 to 99,999,999,999,999,999
12,038,482,364,921,583 numbers produce 1 and 87,961,517,635,078,416 numbers produce 89
12,024,696,404,768,024 numbers produce 1 and 87,975,303,595,231,975 numbers produce 89
</pre>
</pre>