# Help on algorithm to calculate combinations

**URL:** <https://www.ruby-forum.com/t/help-on-algorithm-to-calculate-combinations/184110>\
**Category:** Ruby\
**Created:** [February 25, 2010, 9:40am UTC](https://www.ruby-forum.com/t/help-on-algorithm-to-calculate-combinations/184110 "2010-02-25T09:40:52Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![motoris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@motoris](https://www.ruby-forum.com/u/motoris)\
**Post date:** [February 25, 2010, 9:40am UTC](https://www.ruby-forum.com/t/help-on-algorithm-to-calculate-combinations/184110/1 "2010-02-25T09:40:52Z")

</div>

Hello all  
I would like to find an algorithm to caclulate all options on the  
following problem.  
Lets suppose we have a room of max capacity of 4 persons.  
Which are the combinations of man - child in this room?  
some of them will be:  
empty room (none inside)  
1 man  
1 child  
2 men  
2 children  
2 men 1 children  
2 men 2 children  
.  
.  
4 men  
4 children

I appreciate your help

---

<div class="post-metadata">

**Author:** ![motoris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@motoris](https://www.ruby-forum.com/u/motoris)\
**Post date:** [February 25, 2010, 10:14am UTC](https://www.ruby-forum.com/t/help-on-algorithm-to-calculate-combinations/184110/2 "2010-02-25T10:14:59Z")

</div>

On 25 February 2010 09:40, Alexander A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hello all  
> I would like to find an algorithm to caclulate all options on the  
> following problem.  
> Lets suppose we have a room of max capacity of 4 persons.  
> Which are the combinations of man - child in this room?

0 children / 0 men  
0 children / 1 men  
0 children / 2 men  
0 children / 3 men  
0 children / 4 men  
1 children / 0 men  
1 children / 1 men  
1 children / 2 men  
1 children / 3 men  
2 children / 0 men  
2 children / 1 men  
2 children / 2 men  
3 children / 0 men  
3 children / 1 men  
4 children / 0 men

---

<div class="post-metadata">

**Author:** ![motoris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@motoris](https://www.ruby-forum.com/u/motoris)\
**Post date:** [February 25, 2010, 11:35am UTC](https://www.ruby-forum.com/t/help-on-algorithm-to-calculate-combinations/184110/3 "2010-02-25T11:35:46Z")

</div>

Thomas P. wrote:

> On 25 February 2010 09:40, Alexander A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > Hello all  
> > I would like to find an algorithm to caclulate all options on the  
> > following problem.  
> > Lets suppose we have a room of max capacity of 4 persons.  
> > Which are the combinations of man - child in this room?
> 
> 0 children / 0 men  
> 0 children / 1 men  
> 0 children / 2 men  
> 0 children / 3 men  
> 0 children / 4 men  
> 1 children / 0 men  
> 1 children / 1 men  
> 1 children / 2 men  
> 1 children / 3 men  
> 2 children / 0 men  
> 2 children / 1 men  
> 2 children / 2 men  
> 3 children / 0 men  
> 3 children / 1 men

max = 4  
@result = []  
(0…max).each do |x|  
(0…(max - x)).each do |y|  
@combination = []  
x.times { @combination \<\< “children” }  
y.times { @combination \<\< “men” }  
@result \<\< @combination  
end  
end

Thank you very much

> 4 children / 0 men

---

<div class="post-metadata">

**Author:** ![motoris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@motoris](https://www.ruby-forum.com/u/motoris)\
**Post date:** [February 25, 2010, 1:01pm UTC](https://www.ruby-forum.com/t/help-on-algorithm-to-calculate-combinations/184110/4 "2010-02-25T13:01:07Z")

</div>

> max = 4  
> @result = []  
> (0…max).each do |x|  
> (0…(max - x)).each do |y|  
> @combination = []  
> x.times { @combination \<\< “children” }  
> y.times { @combination \<\< “men” }  
> @result \<\< @combination  
> end  
> end
> 
> Thank you very much
> 
> > 4 children / 0 men

it doesn’t produce 1 children (for instance)

results = []  
0.upto(4) do |n|  
0.upto(2\*\*n - 1) do |i|  
s = i.to\_s(2).rjust(n,‘0’).split(’’).sort.join  
results \<\< s unless results.include?(s)  
end  
end

p results.map! {|e| e.tr(‘01’,‘cm’)}

---

<div class="post-metadata">

**Author:** ![motoris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@motoris](https://www.ruby-forum.com/u/motoris)\
**Post date:** [February 25, 2010, 1:29pm UTC](https://www.ruby-forum.com/t/help-on-algorithm-to-calculate-combinations/184110/5 "2010-02-25T13:29:54Z")

</div>

On Thu, Feb 25, 2010 at 2:40 AM, Alexander A.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> 2 children

def people\_in\_room(occupants)  
for men in 0…occupants  
for children in 0…occupants - men  
yield men , children  
end  
end  
end

puts “in a room with 4 people, you could occupy it in the following  
ways:”  
people\_in\_room 4 do |men,children|  
puts “men: #{men} , children: #{children}”  
end

---

<div class="post-metadata">

**Author:** ![motoris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@motoris](https://www.ruby-forum.com/u/motoris)\
**Post date:** [February 25, 2010, 1:32pm UTC](https://www.ruby-forum.com/t/help-on-algorithm-to-calculate-combinations/184110/6 "2010-02-25T13:32:14Z")

</div>

Sorry, computer lagged and I hit ‘send’ rather than clicking in the  
window  
to edit.

Here is a solution I meant to post, which has the correct inflections  
[http://pastie.org/842110](http://pastie.org/842110)

def people\_in\_room(occupants)  
for men in 0…occupants  
for children in 0…occupants - men  
yield men , children  
end  
end  
end

puts “in a room with 4 people, you could occupy it in the following  
ways:”  
people\_in\_room 4 do |men,children|  
to\_print = Array.new  
to\_print \<\< “#{men} #{ men == 1 ? ‘man’ : ‘men’ }”  
unless  
men.zero?  
to\_print \<\< “#{children} #{ children == 1 ? ‘child’ : ‘children’ }”  
unless  
children.zero?  
puts to\_print.join(’ ')  
end

---

<div class="post-metadata">

**Author:** ![motoris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@motoris](https://www.ruby-forum.com/u/motoris)\
**Post date:** [February 25, 2010, 1:09pm UTC](https://www.ruby-forum.com/t/help-on-algorithm-to-calculate-combinations/184110/7 "2010-02-25T13:09:52Z")

</div>

Giampiero Z. wrote:

> it doesn’t produce 1 children (for instance)
> 
> results = []  
> 0.upto(4) do |n|  
> 0.upto(2\*\*n - 1) do |i|  
> s = i.to\_s(2).rjust(n,‘0’).split(’’).sort.join  
> results \<\< s unless results.include?(s)  
> end  
> end
> 
> p results.map! {|e| e.tr(‘01’,‘cm’)}

It produces the 1 children option plus the empty room one. We have the  
same results 14 for you that don’t iclure the empty room combination and  
15 for me with the empty room.

---

<div class="post-metadata">

**Author:** ![motoris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@motoris](https://www.ruby-forum.com/u/motoris)\
**Post date:** [February 25, 2010, 1:43pm UTC](https://www.ruby-forum.com/t/help-on-algorithm-to-calculate-combinations/184110/8 "2010-02-25T13:43:06Z")

</div>

Josh C. wrote:

> Sorry, computer lagged and I hit ‘send’ rather than clicking in the  
> window  
> to edit.

Thanks Josh 🙂

---

<div class="post-metadata">

**Author:** ![motoris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@motoris](https://www.ruby-forum.com/u/motoris)\
**Post date:** [February 25, 2010, 7:19pm UTC](https://www.ruby-forum.com/t/help-on-algorithm-to-calculate-combinations/184110/9 "2010-02-25T19:19:59Z")

</div>

On Thu, Feb 25, 2010 at 6:43 AM, Alexander A.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> Josh C. wrote:
> 
> > Sorry, computer lagged and I hit ‘send’ rather than clicking in the  
> > window  
> > to edit.
> 
> ## Thanks Josh 🙂
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

lol, you’re welcome. But I just saw that it prints an empty line for the  
empty room. That’s what I get for not testing. Try adding this line to  
the  
beginning of the block:  
next( puts “empty room” ) if men.zero? && children.zero?
