tags:

views:

334

answers:

5

I'm looking for a library or existing code to simplify fractions.

Does anyone have anything at hand or any links?

P.S. I already understand the process but really don't want to rewrite the wheel

Update

Ok i've checked out the fraction library on the CodeProject BUT the problem I have is a little bit tricker than simplifying a fraction.

I have to reduce a percentage split which could be 20% / 50% / 30% (always equal to 100%)

+1  A: 

This library looks like it might be what you need:

var f = new Fraction(numerator, denominator);
numerator = f.Numerator;
denominator = f.Denominator;

Although, I haven't tested it, so it looks like you may need to play around with it to get it to work.

MiffTheFox
+1  A: 

The best example of Fraction (aka Rational) I've seen is in Timothy Budd's "Classic Data Structures in C++". His implementation is very good. It includes a simple implementation of GCD algorithm.

It shouldn't be hard to adapt to C#.

duffymo
+2  A: 

You can use Microsoft.FSharp.Math.BigRational, which is in the free F# Power Pack library. Although it depends on F# (which is gratis and included in VS2010), it can be used from C#.

BigRational reduced = BigRational.FromInt(4)/BigRational.FromInt(6);
Console.WriteLine(reduced);
    2/3
Console.WriteLine(reduced.Numerator);
    2
Console.WriteLine(reduced.Denominator);
    3
Matthew Flaschen
+1  A: 

A custom solution:

void simplify(int[] numbers)
{
    for (int divideBy = 50; divideBy > 0; divideBy--)
    {
        bool divisible = true;
        foreach (int cur in numbers)
        {   

            //check for divisibility
            if ((int)(cur/divideBy)*divideBy!=cur){
                divisible = false;
                break;
            }

        }
        if (divisible)
        {
            for (int i = 0; i < numbers.GetLength(0);i++ )
            {
                numbers[i] /= divideBy;
            }
        }
    }
}

Example usage:

int [] percentages = {20,30,50};
simplify(percentages);
foreach (int p in percentages)
{
    Console.WriteLine(p);
}

Outupts:

2
3
5

By the way, this is my first c# program. Thought it would simply be a fun problem to try a new language with, and now I'm in love! It's like Java, but everything I wish was a bit different is exactly how I wanted it

<3 c#


Edit: Btw don't forget to make it static void if it's for your Main class.

Cam
Adding the primality check was wrong. Imagine what happens when your data is `{2, 98}`. Furthermore, rather than attempting to divide by every integer <= 50, you should just use your prime list. Then you can build up a GCD and do the division loop only once.
Gabe
Also, since this was your first C# program, you might not have realized that you can use `cur % divideBy != 0` to check for indivisibility, and since the array only has one dimension you can use `numbers.Length` to get the number of elements.
Gabe
+1 for both of those. Thanks.
Cam
Doesn't work for cases like {3, 3, 3} which should reduce down to {1, 1, 1}
Harry
@Harry: Yes, it does work for that case. Just tried it.
Cam
+3  A: 

I think you just need to divide by the GCD of all the numbers.

void Simplify(int[] numbers)
{
    int gcd = GCD(numbers);
    for (int i = 0; i < numbers.Length; i++)
        numbers[i] /= gcd;
}
int GCD(int a, int b)
{
    while (b > 0)
    {
        int rem = a % b;
        a = b;
        b = rem;
    }
    return a;
}
int GCD(int[] args)
{
    // using LINQ:
    return args.Aggregate((gcd, arg) => GCD(gcd, arg));
}

I haven't tried the code, but it seems simple enough to be right (assuming your numbers are all positive integers and you don't pass an empty array).

Gabe
yeah that's definitely a legitimate way to code it: http://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations
Cam
I would select this solution as it does not require a small fixed domain for your numbers (ie. [0, 100]). The method `Simplify` is also easy to understand. +1
Michael Petito
Yeah, it took a while to come back to this problem. but this is definetly the working answer!THANKS MATE
Harry