Jump to content

Fraction reduction: Difference between revisions

m (Fix Perl 6 -> Raku links)
Line 1,228:
 
Took 42.251172302s
</pre>
 
=={{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'
0 0
0 1
0 2
1 0
1 1
1 2
)
 
o=: '123456789' {~ odometer@:(#&9) NB. odometer is y digit odometer in base 9
 
g=: [: (-: ~.)"1 Filter o NB. ensure unique digits from the odometer
 
f=: ;"1/&g~ NB. generate all numerators and denominator digits as a matrix of boxes
 
mask=: [: </~&i. # NB. need the lower triangular items
 
av=: [: ; (<@#"1 2~ mask)@:f NB. anti-vulgarization
 
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
 
cancellation=: monad define
ND =. > c y
rats=. (,'r'&,)/
RAT=. rats"2 ND
RED=. ([: -. (-: ]&.".)"1) RAT
CANDIDATES=: RED"_ Filter ND
result=. 0 3 $ a:
for_pair. CANDIDATES do.
original=. rats pair
fraction =. ". original
for_d. intersect/ pair do.
trial=. rats&(-.&d)/ pair
if. fraction = ". trial do.
result =. result , original ; trial ; d
end.
end.
end.
result
)
</lang>
 
<pre>
A=: cancellation&.>2 3 4
 
report=:[: (/:_2&{"1)(((3 ": #) , ' ' , 's' ,~ _1&({::)@:{.)/.~ {:"1)
summary=: ' reducibles' ,~ ":@#
dozen=: ({.~ (12 <. #))L:_1
 
9!:17]0 1 NB. positioning within box
 
(report&.> , summary&.> ,: dozen) A
┌─────────────┬─────────────────┬─────────────────────┐
│ 2 6s │ 9 3s │ 14 1s │
│ 2 9s │ 1 4s │ 25 2s │
│ │ 6 5s │ 92 3s │
│ │ 15 6s │ 14 4s │
│ │ 16 7s │ 29 5s │
│ │ 15 8s │ 63 6s │
│ │ 60 9s │ 16 7s │
│ │ │ 17 8s │
│ │ │ 390 9s │
├─────────────┼─────────────────┼─────────────────────┤
│4 reducibles │ 122 reducibles │ 660 reducibles │
├─────────────┼─────────────────┼─────────────────────┤
│┌─────┬───┬─┐│┌───────┬─────┬─┐│┌─────────┬───────┬─┐│
││16r64│1r4│6│││132r231│12r21│3│││1234r4936│124r496│3││
│├─────┼───┼─┤│├───────┼─────┼─┤│├─────────┼───────┼─┤│
││19r95│1r5│9│││134r536│14r56│3│││1239r6195│123r615│9││
│├─────┼───┼─┤│├───────┼─────┼─┤│├─────────┼───────┼─┤│
││26r65│2r5│6│││134r938│14r98│3│││1246r3649│126r369│4││
│├─────┼───┼─┤│├───────┼─────┼─┤│├─────────┼───────┼─┤│
││49r98│4r8│9│││136r238│16r28│3│││1249r2498│124r248│9││
│└─────┴───┴─┘│├───────┼─────┼─┤│├─────────┼───────┼─┤│
│ ││138r345│18r45│3│││1259r6295│125r625│9││
│ │├───────┼─────┼─┤│├─────────┼───────┼─┤│
│ ││139r695│13r65│9│││1279r6395│127r635│9││
│ │├───────┼─────┼─┤│├─────────┼───────┼─┤│
│ ││143r341│13r31│4│││1283r5132│128r512│3││
│ │├───────┼─────┼─┤│├─────────┼───────┼─┤│
│ ││146r365│14r35│6│││1297r2594│127r254│9││
│ │├───────┼─────┼─┤│├─────────┼───────┼─┤│
│ ││149r298│14r28│9│││1297r3891│127r381│9││
│ │├───────┼─────┼─┤│├─────────┼───────┼─┤│
│ ││149r596│14r56│9│││1298r2596│128r256│9││
│ │├───────┼─────┼─┤│├─────────┼───────┼─┤│
│ ││149r894│14r84│9│││1298r3894│128r384│9││
│ │├───────┼─────┼─┤│├─────────┼───────┼─┤│
│ ││154r253│14r23│5│││1298r5192│128r512│9││
│ │└───────┴─────┴─┘│└─────────┴───────┴─┘│
└─────────────┴─────────────────┴─────────────────────┘
</pre>
 
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.