Anonymous user
Fraction reduction: Difference between revisions
→{{header|J}}
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.
<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)
f=: ,:"1/&g~ NB. f computes a table of all numerators and denominators pairs
mask=: [: </~&i. # NB. the lower triangle will become proper fractions
c=: [: (] >&# -.)&>/"1 Filter av NB. ensure common digit(s)▼
red=: [: ([: -. (-: ]&.".)"1) Filter rat NB. retain reducible fractions▼
▲
cancellation=: monad define
NDL =. c y NB. vector of literal numerator and denominator
rats=. (,'r'&,)/▼
ND =. ". NDL NB. integral version of NDL
FRAC=. _2 x: MASK # ND NB. division
CANDIDATES=
result=. 0 3 $ a:
for_d. intersect/ pair do.
trial=.
if. fraction = _2 x: ". trial do.
result =. result ,
end.
end.
Line 1,288 ⟶ 1,286:
)
</lang>
<pre>
A=: cancellation&.>2 3 4 5
report=:[: (/:_2&{"1)(((
summary=: ' reducibles' ,~ ":@#
dozen=: ({.~ (12 <. #))L:_1
9!:17]0 1 NB. width centering within displayed box
(report&.> , summary&.> ,: dozen) A
┌─────────────┬─────────────────┬─────────────────────┬─────────────────────────┐
│ 2 6s
│ 2 9s
│ │ 6 5s
│ │ 15 6s
│ │ 16 7s
│ │ 15 8s
│ │ 60 9s
│ │ │ 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││
│├─────┼───┼─┤│├───────┼─────┼─┤│├─────────┼───────┼─┤│├───────────┼─────────┼─┤│
││19r95│1r5│9│││134r536│14r56│3│││1239r6195│123r615│9│││12356r67958│1236r6798│5││
│├─────┼───┼─┤│├───────┼─────┼─┤│├─────────┼───────┼─┤│├───────────┼─────────┼─┤│
││26r65│2r5│6│││134r938│14r98│3│││1246r3649│126r369│4│││12358r14362│1258r1462│3││
│├─────┼───┼─┤│├───────┼─────┼─┤│├─────────┼───────┼─┤│├───────────┼─────────┼─┤│
││49r98│4r8│9│││136r238│16r28│3│││1249r2498│124r248│9│││12358r15364│1258r1564│3││
│└─────┴───┴─┘│├───────┼─────┼─┤│├─────────┼───────┼─┤│├───────────┼─────────┼─┤│
│ ││138r345│18r45│3│││1259r6295│125r625│9│││12358r17368│1258r1768│3││
│ │├───────┼─────┼─┤│├─────────┼───────┼─┤│├───────────┼─────────┼─┤│
│ ││139r695│13r65│9│││1279r6395│127r635│9│││12358r19372│1258r1972│3││
│ │├───────┼─────┼─┤│├─────────┼───────┼─┤│├───────────┼─────────┼─┤│
│ ││143r341│13r31│4│││1283r5132│128r512│3│││12358r21376│1258r2176│3││
│ │├───────┼─────┼─┤│├─────────┼───────┼─┤│├───────────┼─────────┼─┤│
│ ││146r365│14r35│6│││1297r2594│127r254│9│││12358r25384│1258r2584│3││
│ │├───────┼─────┼─┤│├─────────┼───────┼─┤│├───────────┼─────────┼─┤│
│ ││149r298│14r28│9│││1297r3891│127r381│9│││12359r61795│1235r6175│9││
│ │├───────┼─────┼─┤│├─────────┼───────┼─┤│├───────────┼─────────┼─┤│
│ ││149r596│14r56│9│││1298r2596│128r256│9│││12364r32596│1364r3596│2││
│ │├───────┼─────┼─┤│├─────────┼───────┼─┤│├───────────┼─────────┼─┤│
│ ││149r894│14r84│9│││1298r3894│128r384│9│││12379r61895│1237r6185│9││
│ │├───────┼─────┼─┤│├─────────┼───────┼─┤│├───────────┼─────────┼─┤│
│ ││154r253│14r23│5│││1298r5192│128r512│9│││12386r32654│1386r3654│2││
│ │└───────┴─────┴─┘│└─────────┴───────┴─┘│└───────────┴─────────┴─┘│
└─────────────┴─────────────────┴─────────────────────┴─────────────────────────┘
</pre>
|