Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
59
Task/Fraction-reduction/Zkl/fraction-reduction.zkl
Normal file
59
Task/Fraction-reduction/Zkl/fraction-reduction.zkl
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
fcn toInt(digits,remove_digit=0){
|
||||
if(remove_digit!=0) digits=digits.copy().del(digits.index(remove_digit));
|
||||
digits.reduce(fcn(s,d){ s*10 + d });
|
||||
}
|
||||
fcn nDigits(n){
|
||||
//-- generate numbers with unique digits efficiently
|
||||
//-- and store them in an array for multiple re-use,
|
||||
//-- along with an array of the removed-digit values.
|
||||
res,digits := List(), n.pump(List(),'+(1)); // 1,2,3,4..n
|
||||
used := List.createLong(n,1).extend(List.createLong(9-n,0));
|
||||
while(True){
|
||||
nine:=List.createLong(9,0);
|
||||
foreach i in (used.len()){ if(used[i]) nine[i]=toInt(digits,i+1) }
|
||||
res.append(T(toInt(digits),nine));
|
||||
found:=False;
|
||||
foreach i in ([n-1..0, -1]){
|
||||
d:=digits[i];
|
||||
if(not used[d-1]) println("ack!");
|
||||
used[d-1]=0;
|
||||
foreach j in ([d..8]){
|
||||
if(not used[j]){
|
||||
used[j]=1;
|
||||
digits[i]=j+1;
|
||||
foreach k in ([i+1..n-1]){
|
||||
digits[k] = used.find(0) + 1;
|
||||
used[digits[k] - 1]=1;
|
||||
}
|
||||
found=True;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(found) break;
|
||||
}//foreach i
|
||||
if(not found) break;
|
||||
}//while
|
||||
res
|
||||
}
|
||||
|
||||
foreach n in ([2..5]){
|
||||
rs,rsz,count,omitted := nDigits(n),rs.len()-1, 0, List.createLong(9,0);
|
||||
foreach i in (rsz){
|
||||
xn,rn := rs[i];
|
||||
foreach j in ([i+1..rsz]){
|
||||
xd,rd := rs[j];
|
||||
foreach k in ([0..8]){
|
||||
yn,yd := rn[k],rd[k];
|
||||
if(yn!=0 and yd!=0 and
|
||||
xn.toFloat()/xd.toFloat() == yn.toFloat()/yd.toFloat()){
|
||||
count+=1;
|
||||
omitted[k]+=1;
|
||||
if(count<=12)
|
||||
println("%d/%d --> %d/%d (removed %d)".fmt(xn,xd,yn,yd,k+1));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
println("%d-digit fractions found: %d, omitted %s\n"
|
||||
.fmt(n,count,omitted.concat(",")));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue