Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to reduce ratio to lowest term

for example : 550:150 to 11:6 using c#
Posted
Comments
CHill60 1-Apr-15 6:24am    
Actually I think that should be 11:3 ...
550 = 11 * 5 * 10
150 = 3 * 5 * 10
Andy Lanng 1-Apr-15 6:57am    
That just goes to show why you should get a computer to do it ^_^
CHill60 1-Apr-15 7:04am    
LOL!

See here: http://stackoverflow.com/questions/5287514/how-to-simplify-fractions[^] - it's exactly the same process.
 
Share this answer
 
Not easily:

First you have to create a method that finds all factors of a given number and output them as a IEnumerable<int>
C#
public IEnumerable<int> Factorize(int number) {
    int max = (int)(number/2);  
    for(int factor = 1; factor <= max; ++factor) { 
        if(number % factor == 0) {
            yield return factor;
        }
    }
    yield number;
}
</int>


Now you have the factors, you need to find factors common to both numbers (common denominator)

C#
public IEnumerable<int> CommonDenominators(int numberLeft,int NumberRight) {
    return Factorize(numberLeft).Union(Factorize(numberRight);
}
</int>


Actually:,You can adapt that to return the highest (i always thought is was the lowest, which would be 1?) common denominator:

C#
public int HighestCommonDenominator(int numberLeft,int NumberRight) {
    return Factorize(numberLeft).Union(Factorize(numberRight).Max(n=>n);
}


Edit: If there are no other common denominators, i.e. 17:181 then the number 1 will be returned, so it's still accurate

Then just divide both numbers by that number:

C#
int left = 550;
int right = 150:
int highestCommonDenominator = HighestCommonDenominator(left, right );
int lowestLeft = left / lowestCommonDenominator;
int lowestRight = right / lowestCommonDenominator;  


I haven't tested the code but the essentials are there

Hope that helps :)
 
Share this answer
 
v6

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900