Given a fraction in form of a/b where a & b are positive integers. Find ΔX such that when it is added to numerator as well as denominator of given fraction it will result into a new Ir-reducible fraction c/d.
Input : a = 4, b = 10, c = 1, d = 2 Output : ΔX = 2 Explanation : (a + ΔX)/(b + ΔX) = (4 + 2) / (10 + 2) = 6/12 = 1/2 = c/d Input : a = 4, b = 10, c = 2, d = 5 Output : ΔX = 0 Explanation : (a + ΔX) / (b + ΔX) = (4) / (10) = 2 / 5 = c/d
To solve this type of problem rather than implementing just a programming approach we have to do a little of mathematics. mathematics just necessary is as:
As per question we have to find ΔX such that: => (a + ΔX) / (b + ΔX) = c / d => ad + dΔX = bc + cΔX => dΔX - cΔX = bc - ad => ΔX (d - c) = bc -ad => ΔX = (bc - ad) / (d - c)
So, in a way to finding ΔX we have to only calculate
*** QuickLaTeX cannot compile formula: *** Error message: Error: Nothing to show, formula is empty
ΔX = 6
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.