Fraction reduction

From Rosetta Code
Revision as of 06:46, 3 September 2019 by rosettacode>Gerard Schildberger (Created page with "{{draft task|Puzzles}} ''There is a fine line between numerator and denominator.''       ''─── anonymous'' A method to   "reduce"...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Fraction reduction is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
              There is a fine line between numerator and denominator.       ─── anonymous


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

       16                                                  16
      ────     and then (simply) cross─out the sixes:     ────
       64                                                  64

resulting in:

        1
       ───     (the result)
        4


Naturally,   this "method" of reduction must reduce to the proper value   (shown as a fraction).


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


Task

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

  •   show 2-digit fractions found   (like the example shown above)
  •   show 3-digit fractions
  •   show 4-digit fractions
  •   show 5-digit fractions   (and higher)       (optional)
  •   show each (above) n-digit fractions separately from other different n-sized fractions, don't mix different "sizes" together
  •   for each "size" fraction,   only show a dozen examples   (the 1st twelve found)
  •   (it's recognized that not every programming solution will have the same generation algorithm)
  •   for each "size" fraction:
  •   show a count of how many reducible fractions were found.   The example (above) is size 2
  •   show a count of which digits were crossed out   (one line for each different digit)
  •   for each "size" fraction,   show a count of how many were found.   The example (above) is size 2
  •   show each n-digit example   (to be shown on one line):
  •   show each n-digit fraction
  •   show each reduced n-digit fraction
  •   show what digit was crossed out for the numerator and the denominator


Task requirements/restrictions
  •   only proper fractions and their reductions   (the result)   are to be used   (no vulgar fractions)
  •   only positive fractions are to be used   (no negative signs anywhere)
  •   only base ten integers are to be used for the numerator and denominator
  •   no zeros   (decimal digit)   can be used within the numerator or the denominator
  •   the numerator and denominator should be composed of the same number of digits
  •   no digit can be repeated in the numerator
  •   no digit can be repeated in the denominator
  •   (naturally)   there should be a shared decimal digit in the numerator   and   the denominator
  •   fractions can be shown as   16/64   (for example)


Show all output here, on this page.


Related task

Farey sequence.     ───   Well, somewhat related, it has fractions.


References



REXX

<lang rexx>/*REXX pgm reduces fractions by "crossing out" matching digits in nominator&denominator.*/ parse arg high show . /*obtain optional arguments from the CL*/ if high== | high=="," then high= 4 /*Not specified? Then use the default.*/ if show== | show=="," then show= 12 /* " " " " " " */ say center(' some samples of reduced fractions by crossing out digits ', 79, "═") $.=0 /*placeholder array for counts; init. 0*/

     do L=2  to high                            /*do 2-dig fractions to HIGH-dig fract.*/
     say
        do n=10**(L-1)  to 10**L - 1            /*generate some  N  digit  fractions.  */
        if pos(0, n)\==0  then iterate          /*Does  it  have a zero?  Then skip it.*/
        if hasDup(n)      then iterate          /*  "    "    "  " dup?     "    "   " */
            do d=n+1  to  10**L - 1                       /*only process like-sized #.s*/
            if pos(0, d)\==0         then iterate         /*Have a zero? Then skip it. */
            if verify(d, n, 'M')==0  then iterate         /*No digs in common?  Skip it*/
            if hasDup(d)             then iterate         /*Any digs are dups?    "   "*/
            q=n/d                                         /*compute quotient just once.*/
                 do e=1  for L;      xo= substr(n, e, 1)  /*try crossing out each digit*/
                 nn= elide(n, xo)                         /*elide from the numerator.  */
                 dd= elide(d, xo)                         /*  "     "   "  denominator.*/
                 if nn/dd \== q  then iterate             /*Not the same quotient? Skip*/
                 $.L= $.L + 1                             /*Eureka!   We found one.    */
                 $.L.xo= $.L.xo + 1                       /*count the silly reduction. */
                 if $.L>show  then iterate                /*Too many found? Don't show.*/
                 say center(n'/'d   " = "   nn'/'dd  "  by crossing out the" xo"'s.", 79)
                 end   /*e*/
            end        /*d*/
        end            /*n*/
     end               /*L*/

say; @with= ' with crossed-out' /* [↓] show counts for any reductions.*/

     do k=1  for 9                              /*Is this a zero count?  Then skip it. */
     if $.k==0  then iterate                    /*Is this a zero count?  Then skip it. */
     say;    say center('There are '     $.k     " "k'-digit fractions.', 79, "═")
                         @for= '          For ' /*literal for SAY indentation (below). */
        do #=1  for 9;   if $.k.#==0  then iterate
        say @for    k"-digit fractions, there are "    right($.k.#, k-1)   @with   #"'s."
        end   /*#*/
     end      /*k*/

exit /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ hasDup: procedure; parse arg x; w= length(x); if w<2 then return 0

          do i=1  for w-1;      if pos(substr(x,i,1), substr(x,i+1)) \== 0  then return 1
          end   /*i*/;                                                           return 0

/*──────────────────────────────────────────────────────────────────────────────────────*/ elide: procedure; parse arg x,c; return space( translate(x, , c), 0)</lang>

output   when using the input of:     5   12
══════════ some samples of reduced fractions by crossing out digits ═══════════

                   16/64  =  1/4   by crossing out the 6's.
                   19/95  =  1/5   by crossing out the 9's.
                   26/65  =  2/5   by crossing out the 6's.
                   49/98  =  4/8   by crossing out the 9's.

                 132/231  =  12/21   by crossing out the 3's.
                 134/536  =  14/56   by crossing out the 3's.
                 134/938  =  14/98   by crossing out the 3's.
                 136/238  =  16/28   by crossing out the 3's.
                 138/345  =  18/45   by crossing out the 3's.
                 139/695  =  13/65   by crossing out the 9's.
                 143/341  =  13/31   by crossing out the 4's.
                 146/365  =  14/35   by crossing out the 6's.
                 149/298  =  14/28   by crossing out the 9's.
                 149/596  =  14/56   by crossing out the 9's.
                 149/894  =  14/84   by crossing out the 9's.
                 154/253  =  14/23   by crossing out the 5's.

               1234/4936  =  124/496   by crossing out the 3's.
               1239/6195  =  123/615   by crossing out the 9's.
               1246/3649  =  126/369   by crossing out the 4's.
               1249/2498  =  124/248   by crossing out the 9's.
               1259/6295  =  125/625   by crossing out the 9's.
               1279/6395  =  127/635   by crossing out the 9's.
               1283/5132  =  128/512   by crossing out the 3's.
               1297/2594  =  127/254   by crossing out the 9's.
               1297/3891  =  127/381   by crossing out the 9's.
               1298/2596  =  128/256   by crossing out the 9's.
               1298/3894  =  128/384   by crossing out the 9's.
               1298/5192  =  128/512   by crossing out the 9's.

             12349/24698  =  1234/2468   by crossing out the 9's.
             12356/67958  =  1236/6798   by crossing out the 5's.
             12358/14362  =  1258/1462   by crossing out the 3's.
             12358/15364  =  1258/1564   by crossing out the 3's.
             12358/17368  =  1258/1768   by crossing out the 3's.
             12358/19372  =  1258/1972   by crossing out the 3's.
             12358/21376  =  1258/2176   by crossing out the 3's.
             12358/25384  =  1258/2584   by crossing out the 3's.
             12359/61795  =  1235/6175   by crossing out the 9's.
             12364/32596  =  1364/3596   by crossing out the 2's.
             12379/61895  =  1237/6185   by crossing out the 9's.
             12386/32654  =  1386/3654   by crossing out the 2's.


═══════════════════════There are  4  2-digit fractions.════════════════════════
          For  2-digit fractions, there are  2  with crossed-out 6's.
          For  2-digit fractions, there are  2  with crossed-out 9's.

══════════════════════There are  122  3-digit fractions.═══════════════════════
          For  3-digit fractions, there are   9  with crossed-out 3's.
          For  3-digit fractions, there are   1  with crossed-out 4's.
          For  3-digit fractions, there are   6  with crossed-out 5's.
          For  3-digit fractions, there are  15  with crossed-out 6's.
          For  3-digit fractions, there are  16  with crossed-out 7's.
          For  3-digit fractions, there are  15  with crossed-out 8's.
          For  3-digit fractions, there are  60  with crossed-out 9's.

══════════════════════There are  660  4-digit fractions.═══════════════════════
          For  4-digit fractions, there are   14  with crossed-out 1's.
          For  4-digit fractions, there are   25  with crossed-out 2's.
          For  4-digit fractions, there are   92  with crossed-out 3's.
          For  4-digit fractions, there are   14  with crossed-out 4's.
          For  4-digit fractions, there are   29  with crossed-out 5's.
          For  4-digit fractions, there are   63  with crossed-out 6's.
          For  4-digit fractions, there are   16  with crossed-out 7's.
          For  4-digit fractions, there are   17  with crossed-out 8's.
          For  4-digit fractions, there are  390  with crossed-out 9's.

══════════════════════There are  5087  5-digit fractions.══════════════════════
          For  5-digit fractions, there are    75  with crossed-out 1's.
          For  5-digit fractions, there are    40  with crossed-out 2's.
          For  5-digit fractions, there are   376  with crossed-out 3's.
          For  5-digit fractions, there are    78  with crossed-out 4's.
          For  5-digit fractions, there are   209  with crossed-out 5's.
          For  5-digit fractions, there are   379  with crossed-out 6's.
          For  5-digit fractions, there are   591  with crossed-out 7's.
          For  5-digit fractions, there are   351  with crossed-out 8's.
          For  5-digit fractions, there are  2988  with crossed-out 9's.