How to resolve the algorithm Fraction reduction step by step in the zkl programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Fraction reduction step by step in the zkl programming language

Table of Contents

Problem Statement

A method to   "reduce"   some reducible fractions is to   cross out   a digit from the numerator and the denominator.   An example is: resulting in:

Naturally,   this "method" of reduction must reduce to the proper value   (shown as a fraction). This "method" is also known as   anomalous cancellation   and also   accidental cancellation.

(Of course,   this "method" shouldn't be taught to impressionable or gullible minds.)       😇

Find and show some fractions that can be reduced by the above "method".

Show all output here, on this page.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Fraction reduction step by step in the zkl programming language

Source code in the zkl programming language

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(",")));
}

  

You may also check:How to resolve the algorithm Biorhythms step by step in the VBA programming language
You may also check:How to resolve the algorithm Random number generator (included) step by step in the GAP programming language
You may also check:How to resolve the algorithm Pernicious numbers step by step in the Groovy programming language
You may also check:How to resolve the algorithm Sum digits of an integer step by step in the BQN programming language
You may also check:How to resolve the algorithm Infinity step by step in the Scheme programming language