Exemple #1
0
        //<summary>
        //Code snippet by Eric Lippert from stackexchange
        //</summary>
        public static IEnumerable <FractionSet> SubsetSum(FractionSet fractions, FractionData target)
        {
            return(from subset in fractions.EnumeratePowerSet()
                   where General.SimplifyFraction(subset.FractionSum().numerator, subset.FractionSum().denominator) [0] == General.SimplifyFraction(target.numerator, target.denominator) [0] &&
                   General.SimplifyFraction(subset.FractionSum().numerator, subset.FractionSum().denominator) [1] == General.SimplifyFraction(target.numerator, target.denominator) [1]
                   select subset);
//			return from subset in fractions.EnumeratePowerSet ()
//			       where subset.FractionSum ().numerator == target.numerator && subset.FractionSum ().denominator == target.denominator
//			       select subset;

//			return from subset in fractions.EnumeratePowerSet ()
//			       where subset.Sum () == (target.numerator / (float)target.denominator)
//			       select subset;
        }
Exemple #2
0
        //<summary>
        //Algorithm by SergeyS from stackoverflow
        //</summary>
        public IEnumerable <FractionSet> EnumeratePowerSet()
        {
            int n                   = fractions.Count;
            int powerSetCount       = 1 << n;
            List <FractionSet> sets = new List <FractionSet> ();

            for (int setMask = 0; setMask < powerSetCount; setMask++)
            {
                FractionSet set = new FractionSet();
                for (int i = 0; i < n; i++)
                {
                    if ((setMask & (1 << i)) > 0)
                    {
                        set.fractions.Add(fractions [i]);
                    }
                }
                sets.Add(set);
            }

            return(sets);
        }