Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Calculate 2 to the power of N

// Calculate 2 to the power of N
// Returns "double" value


        private double TwoPowerN(int n)
        {
            int k;
            int i = 1;

            for (k=1; k <= n; k++)
            {
                    i = i * 2;   
            }
            return i
        }

Power-Set generating function for Ruby

Extend Ruby's Array class by a method that returns the "power set" with the following code.

class Array
  # Returns the "power set" for this Array. This means that an array with all
  # subsets of the array's elements will be returned.
  def power
    # the power set line is stolen from http://johncarrino.net/blog/2006/08/11/powerset-in-ruby/
    inject([[]]){|c,y|r=[];c.each{|i|r<<i;r<<i+[y]};r}
  end
end
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS