Project

partitions

0.0
No release in over 3 years
Low commit activity in last 3 years
Generate integer, set and multiset partitions
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.9
~> 5.6
~> 10.0
~> 0.10
 Project Readme

Partitions

Partitions is a rubygem to list integer, set and multiset partitions.

Installation

Add this line to your application's Gemfile:

gem 'partitions'

And then execute:

$ bundle

Or install it yourself as:

$ gem install partitions

Usage

>> require 'partitions' #=> true
>> 5.partitions #=> nil
[1, 1, 1, 1, 1]
[1, 1, 1, 2]
[1, 1, 3]
[1, 2, 2]
[1, 4]
[2, 3]
[5]

>> 5.partitions{|partition| p partition};nil #=> nil
[1, 1, 1, 1, 1]
[1, 1, 1, 2]
[1, 1, 3]
[1, 2, 2]
[1, 4]
[2, 3]
[5]


irb(main):001:0> sp = Partitions::SetPartitions.new(4)
=> #<Partitions::SetPartitions:0x007fedab516f58 @n=4, @k=[0, 0, 0, 0], @m=[0, 0, 0, 0], @p=nil, @size=0>
irb(main):002:0> sp.each_partition{|partition| p partition};nil
[0, 0, 0, 0]
[0, 0, 0, 1]
[0, 0, 1, 0]
[0, 0, 1, 1]
[0, 0, 1, 2]
[0, 1, 0, 0]
[0, 1, 0, 1]
[0, 1, 0, 2]
[0, 1, 1, 0]
[0, 1, 1, 1]
[0, 1, 1, 2]
[0, 1, 2, 0]
[0, 1, 2, 1]
[0, 1, 2, 2]
[0, 1, 2, 3]
=> nil

irb(main):003:0> sp = Partitions::SetPartitions.new(3)
=> #<Partitions::SetPartitions:0x007fedab4fea48 @n=3, @k=[0, 0, 0], @m=[0, 0, 0], @p=nil, @size=0>
irb(main):004:0> sp.next_partition
=> [0, 0, 1]
irb(main):005:0> sp.next_partition
=> [0, 1, 0]
irb(main):006:0> sp.next_partition
=> [0, 1, 1]
irb(main):007:0> sp.next_partition
=> [0, 1, 2]
irb(main):008:0> sp.next_partition
=> nil
irb(main):009:0> sp.previous_partition
=> [0, 1, 1]
irb(main):010:0> sp.previous_partition
=> [0, 1, 0]
irb(main):011:0> sp.previous_partition
=> [0, 0, 1]
irb(main):012:0> sp.previous_partition
=> [0, 0, 0]
irb(main):013:0> sp.previous_partition
=> nil

irb(main):001:0> sp = Partitions::SetPartitions.new(5)
=> #<Partitions::SetPartitions:0x007f903c426a70 @n=5, @k=[0, 0, 0, 0, 0], @m=[0, 0, 0, 0, 0], @p=nil, @size=0>
irb(main):002:0> sp.count
=> 52

irb(main):003:0> sp = Partitions::SetPartitions.new(8)
=> #<Partitions::SetPartitions:0x007f903c417458 @n=8, @k=[0, 0, 0, 0, 0, 0, 0, 0], @m=[0, 0, 0, 0, 0, 0, 0, 0], @p=nil, @size=0>
irb(main):004:0> sp.count
=> 4140

irb(main):005:0> sp = Partitions::SetPartitions.new(6,5)
=> #<Partitions::SetPartitions:0x007fd9c284def8 @n=6, @k=[0, 0, 1, 2, 3, 4], @m=[0, 0, 1, 2, 3, 4], @p=5, @size=5>
irb(main):006:0> sp.each_partition{|partition| p partition};nil
[0, 0, 1, 2, 3, 4]
[0, 1, 0, 2, 3, 4]
[0, 1, 1, 2, 3, 4]
[0, 1, 2, 0, 3, 4]
[0, 1, 2, 1, 3, 4]
[0, 1, 2, 2, 3, 4]
[0, 1, 2, 3, 0, 4]
[0, 1, 2, 3, 1, 4]
[0, 1, 2, 3, 2, 4]
[0, 1, 2, 3, 3, 4]
[0, 1, 2, 3, 4, 0]
[0, 1, 2, 3, 4, 1]
[0, 1, 2, 3, 4, 2]
[0, 1, 2, 3, 4, 3]
[0, 1, 2, 3, 4, 4]
=> nil

##References ###Integer Partitions

###Set Partitions

Orlov, M.: Efficient generation of set partitions. Tech. rep., Engineering and Computer Sciences, University of Ulm (2002)

###Stirling Number of the Second Kind

http://mathworld.wolfram.com/StirlingNumberoftheSecondKind.html

Development

After checking out the repo, run bin/setup to install dependencies. Then, run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release to create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

  1. Fork it ( https://github.com/chaitanyav/partitions/)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request