Erhan's Blog

its all just a bunch of pixels

About the author

Author Name is someone.
E-mail me Send mail

Recent comments

Authors

Tags

None

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2008

    Combinatorics

    Here's a basic class to provide enumerable permutations & combinations ...

    public class Combinatorics
    {
        public static IEnumerable<IEnumerable<T>> Permutations<T>(IList<T> items, int setSize)
        {
            if (setSize > 0)
                foreach (T item in items)
                {
                    IList<T> remainingItems = items.Where(n => !item.Equals(n)).ToList();
    
                    foreach (IEnumerable<T> remainingPermutations in Permutations(remainingItems, setSize - 1))
                        yield return Enumerable.Concat<T>(new T[] { item }, remainingPermutations);
                }
            else
                yield return new T[0];
        }
    
    
        public static IEnumerable<IEnumerable<T>> Combinations<T>(IList<T> items, int setSize)
        {
            int[] a = new int[setSize];
            List<T> returnList = new List<T>(setSize);
    
            for (int i = 0; i < a.Length; i++)
                a[i] = i;
    
            foreach (int item in a)
                returnList.Add(items[item]);
    
            yield return returnList.ToArray();
    
            long numLeft = GetTotalNumberOfCombinations(items.Count, setSize);
    
            while (numLeft > 1)
            {
                int i = setSize - 1;
                while (a[i] == items.Count - setSize + i)
                    i--;
    
                a[i] = a[i] + 1;
                for (int j = i + 1; j < setSize; j++)
                    a[j] = a[i] + j - i;
    
                numLeft--;
    
                returnList.Clear();
    
                foreach (int item in a)
                    returnList.Add(items[item]);
    
                yield return returnList.ToArray();
    
            } 
        }
    
        private static long GetTotalNumberOfCombinations(long itemCount, long setSize)
        {
            //takes advantage of the fact that COMBIN(100,97) == COMBIN(100,3) 
            // where's BigInteger when you need it ? 
            
            if (itemCount < setSize) return 0;
            if (itemCount == setSize) return 1;
    
            long delta, iMax;
    
            delta = setSize < itemCount - setSize ? itemCount - setSize : setSize;
            iMax = setSize < itemCount - setSize ? setSize : itemCount - setSize;
            
            long returnValue = delta + 1;
    
            for (long i = 2; i <= iMax; ++i)
                checked { returnValue = (returnValue * (delta + i)) / i; }
    
            return returnValue;
        }
    }

    Posted by ehosca on Friday, February 15, 2008 6:31 AM
    Permalink | Comments (0) | Post RSSRSS comment feed

    Generating a Number Sequence

    Here's a quick way to generate a sequence of numbers using T-SQL.

    WITH NumberGen (number) AS
    (   SELECT 0
        UNION ALL
        SELECT number + 1 FROM NumberGen WHERE number < 99)
    -- extend as required 
    SELECT Sequence = first.number * 100 + second.number 
    FROM NumberGen first, NumberGen second
    ORDER BY Sequence

    And with LINQ using statements

    foreach(int num in Enumerable.Range(0,10000)) // modify to taste 
    {
        Console.WriteLine(num);
    }

    and using Expressions

    from num in Enumerable.Range(0,10000)
    select (num) 

    Posted by ehosca on Thursday, February 07, 2008 10:14 AM
    Permalink | Comments (0) | Post RSSRSS comment feed