Fraction reduction: Difference between revisions

Line 1,230:
</pre>
 
The algorithm generates all potential rational fractions of given size in base 10 and successively applies conditions to restrict the candidates. By avoiding boxing and rational numbers this version is much quicker than that which may be found in the page history.
=={{header|J}}==
 
<lang J>
 
Filter=: (#~`)(`:6)
assert 'ac' -: 1 0 1"_ Filter 'abc'
 
intersect=:-.^:2
assert 'ab' -: 'abc'intersect'razb'
 
odometer=: (4$.$.)@:($&1)
Note 'odometer 2 3'
Line 1,249 ⟶ 1,247:
1 2
)
common=: 0 e. ~:
assert common 1 2 1
assert -. common 1 2 3
 
o=: '123456789' {~ [: -.@:common"1 Filter odometer@:(#&9) NB. odometero is y digitunique odometerdigits, inall baseof 9them
 
f=: ,:"1/&g~ NB. f computes a table of all numerators and denominators pairs
g=: [: (-: ~.)"1 Filter o NB. ensure unique digits from the odometer
 
mask=: [: </~&i. # NB. the lower triangle will become proper fractions
f=: ;"1/&g~ NB. generate all numerators and denominator digits as a matrix of boxes
 
maskav=: (([: </~&i., mask) # ,/)@:f NB. need the lower triangular itemsanti-vulgarization
 
avc=: [: ; (<common@#:,/"1 2~ mask)@:fFilter av NB. anti-vulgarizationensure common digit(s)
 
c=: [: (] >&# -.)&>/"1 Filter av NB. ensure common digit(s)
 
NB. rat and red are unused as is. Retaining the intermediate
NB. results in an explicit definition is convenient.
 
rat=: (,'r'&,)&>/"1@:c NB. create literal versions of rationals
red=: [: ([: -. (-: ]&.".)"1) Filter rat NB. retain reducible fractions
 
cfac=: [: (][: >&#common -,&:~.)&>:q:&:"./)"12 Filter av c NB. ensureassure a common digit(s)factor
cancellation=: monad define
NDL =. c y NB. vector of literal numerator and denominator
ND =. > c y
red=: [: ([: -. (-: ]&.".)"1) Filter rat NB. retain reducible fractions
rats=. (,'r'&,)/
ND =. ". NDL NB. integral version of NDL
RAT=. rats"2 ND
REDMASK=. ([: -.common (-: ],&:~.".&:q:/)"1) RATND NB. assure a common factor
FRAC=. _2 x: MASK # ND NB. division
CANDIDATES=:. RED"_MASK Filter# NDNDL
ratsrat=. (, 'r'&,)/
result=. 0 3 $ a:
for_pairfor_i. i. # CANDIDATES do.
originalfraction =. ratsi { pairFRAC
fraction pair=. ".i { originalCANDIDATES
for_d. intersect/ pair do.
trial=. rats&(pair -.&d)/"1 paird
if. fraction = _2 x: ". trial do.
result =. result , original(rat/pair) ; (rat/trial) ; d
end.
end.
Line 1,288 ⟶ 1,286:
)
</lang>
 
<pre>
A=: cancellation&.>2 3 4 5
 
report=:[: (/:_2&{"1)(((34 ": #) , ' ' , 's' ,~ _1&({::)@:{.)/.~ {:"1)
summary=: ' reducibles' ,~ ":@#
dozen=: ({.~ (12 <. #))L:_1
 
9!:17]boxdraw_j_ 0 1 NB. positioning withinpretty boxboxes
9!:17]0 1 NB. width centering within displayed box
 
(report&.> , summary&.> ,: dozen) A
┌─────────────┬─────────────────┬─────────────────────┬─────────────────────────┐
┌─────────────┬─────────────────┬─────────────────────┐
2 6s 9 3s 14 1s │ 75 1s
2 9s 1 4s 25 2s │ 40 2s
│ │ 6 5s 92 3s │ 376 3s
│ │ 15 6s 14 4s │ 78 4s
│ │ 16 7s 29 5s │ 209 5s
│ │ 15 8s 63 6s │ 379 6s
│ │ 60 9s 16 7s │ 591 7s
│ │ │ 17 8s │ 351 8s
│ │ │ 390 9s │ 2988 9s
├─────────────┼─────────────────┼─────────────────────┼─────────────────────────┤
├─────────────┼─────────────────┼─────────────────────┤
│4 reducibles │ 122 reducibles │ 660 reducibles │ 5087 reducibles
├─────────────┼─────────────────┼─────────────────────┼─────────────────────────┤
├─────────────┼─────────────────┼─────────────────────┤
│┌─────┬───┬─┐│┌───────┬─────┬─┐│┌─────────┬───────┬─┐│┌───────────┬─────────┬─┐│
│┌─────┬───┬─┐│┌───────┬─────┬─┐│┌─────────┬───────┬─┐│
││16r64│1r4│6│││132r231│12r21│3│││1234r4936│124r496│3│││12349r24698│1234r2468│9││
││16r64│1r4│6│││132r231│12r21│3│││1234r4936│124r496│3││
│├─────┼───┼─┤│├───────┼─────┼─┤│├─────────┼───────┼─┤│├───────────┼─────────┼─┤│
│├─────┼───┼─┤│├───────┼─────┼─┤│├─────────┼───────┼─┤│
││19r95│1r5│9│││134r536│14r56│3│││1239r6195│123r615│9│││12356r67958│1236r6798│5││
││19r95│1r5│9│││134r536│14r56│3│││1239r6195│123r615│9││
│├─────┼───┼─┤│├───────┼─────┼─┤│├─────────┼───────┼─┤│├───────────┼─────────┼─┤│
│├─────┼───┼─┤│├───────┼─────┼─┤│├─────────┼───────┼─┤│
││26r65│2r5│6│││134r938│14r98│3│││1246r3649│126r369│4│││12358r14362│1258r1462│3││
││26r65│2r5│6│││134r938│14r98│3│││1246r3649│126r369│4││
│├─────┼───┼─┤│├───────┼─────┼─┤│├─────────┼───────┼─┤│├───────────┼─────────┼─┤│
│├─────┼───┼─┤│├───────┼─────┼─┤│├─────────┼───────┼─┤│
││49r98│4r8│9│││136r238│16r28│3│││1249r2498│124r248│9│││12358r15364│1258r1564│3││
││49r98│4r8│9│││136r238│16r28│3│││1249r2498│124r248│9││
│└─────┴───┴─┘│├───────┼─────┼─┤│├─────────┼───────┼─┤│├───────────┼─────────┼─┤│
│└─────┴───┴─┘│├───────┼─────┼─┤│├─────────┼───────┼─┤│
│ ││138r345│18r45│3│││1259r6295│125r625│9│││12358r17368│1258r1768│3││
│ ││138r345│18r45│3│││1259r6295│125r625│9││
│ │├───────┼─────┼─┤│├─────────┼───────┼─┤│├───────────┼─────────┼─┤│
│ │├───────┼─────┼─┤│├─────────┼───────┼─┤│
│ ││139r695│13r65│9│││1279r6395│127r635│9│││12358r19372│1258r1972│3││
│ ││139r695│13r65│9│││1279r6395│127r635│9││
│ │├───────┼─────┼─┤│├─────────┼───────┼─┤│├───────────┼─────────┼─┤│
│ │├───────┼─────┼─┤│├─────────┼───────┼─┤│
│ ││143r341│13r31│4│││1283r5132│128r512│3│││12358r21376│1258r2176│3││
│ ││143r341│13r31│4│││1283r5132│128r512│3││
│ │├───────┼─────┼─┤│├─────────┼───────┼─┤│├───────────┼─────────┼─┤│
│ │├───────┼─────┼─┤│├─────────┼───────┼─┤│
│ ││146r365│14r35│6│││1297r2594│127r254│9│││12358r25384│1258r2584│3││
│ ││146r365│14r35│6│││1297r2594│127r254│9││
│ │├───────┼─────┼─┤│├─────────┼───────┼─┤│├───────────┼─────────┼─┤│
│ │├───────┼─────┼─┤│├─────────┼───────┼─┤│
│ ││149r298│14r28│9│││1297r3891│127r381│9│││12359r61795│1235r6175│9││
│ ││149r298│14r28│9│││1297r3891│127r381│9││
│ │├───────┼─────┼─┤│├─────────┼───────┼─┤│├───────────┼─────────┼─┤│
│ │├───────┼─────┼─┤│├─────────┼───────┼─┤│
│ ││149r596│14r56│9│││1298r2596│128r256│9│││12364r32596│1364r3596│2││
│ ││149r596│14r56│9│││1298r2596│128r256│9││
│ │├───────┼─────┼─┤│├─────────┼───────┼─┤│├───────────┼─────────┼─┤│
│ │├───────┼─────┼─┤│├─────────┼───────┼─┤│
│ ││149r894│14r84│9│││1298r3894│128r384│9│││12379r61895│1237r6185│9││
│ ││149r894│14r84│9│││1298r3894│128r384│9││
│ │├───────┼─────┼─┤│├─────────┼───────┼─┤│├───────────┼─────────┼─┤│
│ │├───────┼─────┼─┤│├─────────┼───────┼─┤│
│ ││154r253│14r23│5│││1298r5192│128r512│9│││12386r32654│1386r3654│2││
│ ││154r253│14r23│5│││1298r5192│128r512│9││
│ │└───────┴─────┴─┘│└─────────┴───────┴─┘│└───────────┴─────────┴─┘│
│ │└───────┴─────┴─┘│└─────────┴───────┴─┘│
└─────────────┴─────────────────┴─────────────────────┴─────────────────────────┘
└─────────────┴─────────────────┴─────────────────────┘
</pre>
 
Anonymous user