Consider two irreducible fractions:
r1=p1q1
r2=p2q2
with r1≥0 and r2≥0.
How the modulo % is defined over rational numbers (I think that is r3 such that r1=r2×n+r3 with n a positive integer but I am not sure of that), and how to compute the numerator p3 and the denominator q3 from p1,q1,p2,q2 and using only the following operations on integers: +,−,×,/,%,gcd(),lcm() ?
Answer
If you don't impose any condition on n then clearly
r3=r1−nr2
is a solution as n goes over all the integers. If you want the one that minimizes r3 then choose
n=⌊r1r2⌋
If you want the smallest r3 in magnitude then round the ratio instead of flooring it, i.e
n=⌊r1r2+12⌋
If you program in C/C++ and similar programming language, the code would be
n = (p1 * q2)/(p2*q1); // Using floor
n = (p1 * q2 + ((p2*q1) >> 1)/(p2*q1); // Using rounding
No comments:
Post a Comment