I am trying to program something in eVB that would simplify a fraction. I figure you would do something that divided the numerator and denominator by 2 then use a loop to divide it by a number 1 higher each time (2,3,4,5,6 etc.) then when the round numerator equlas the actual numerator and the denominator equals the actual denominator then it would show the answer and stop the loop. But i haven't been able to do this right so if possible can someone write a program that would do this so I can see an example of how this could be done. Thank you.
- Code:
int GreatestCommonDivisor(iNum1, iNum2)
{
iNum1 = abs(iNum1)
iNum2 = abs(iNum2)
if (iNum2 == 0) // base case
return iNum1
else // induction step
{
Dim remainder
remainder = mod(iNum1, iNum2) ' *** iNum1 % iNum2
//call recursively
return GreatestCommonDivisor(iNum2, remainder)
}
}
function Simplify(ByVal myNumerator, ByVal myDenominator)
{
Dim num = myNumerator
Dim denom = myDenominator
Dim gcd
while(true)
gcd = GreatestCommonDivisor(num, denom)
if (gcd == 1) break
num = num / gcd
denom = denom / gcd
EndW
myNumerator = num
myDenominator = denom
}
I hope it help. Sorry if my vb is not really good, But it's not my speciality lol. But I think the point is how to do it.