Skip to content

Commit 76a9ad2

Browse files
committed
Combinatoric selections
1 parent 6a981b4 commit 76a9ad2

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

problem_053/problem53.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Combinatoric selections
2+
# Problem 53
3+
There are exactly ten ways of selecting three from five, 12345:
4+
5+
123, 124, 125, 134, 135, 145, 234, 235, 245, and 345
6+
7+
In combinatorics, we use the notation, 5C3 = 10.
8+
9+
In general,
10+
11+
nCr = n!/r!(n−r)!
12+
,where r ≤ n, n! = n×(n−1)×...×3×2×1, and 0! = 1.
13+
It is not until n = 23, that a value exceeds one-million: 23C10 = 1144066.
14+
15+
How many, not necessarily distinct, values of nCr, for 1 ≤ n ≤ 100, are greater than one-million?
16+
17+
# Solution Notes
18+
I take advantage of the large number capabilities of Ruby in my solution. The overview
19+
from Project Euler provides some very complex mathematics that could also provide the
20+
soluton. I might analyze this and perhaps code it in a differenct language later.

problem_053/solution.rb

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Combinatorics
2+
def initialize
3+
@factorial = []
4+
@factorial<<1 # 0 factorial is 1
5+
@factorial<<1
6+
(2..100).to_a.each{|n|@factorial<<(1..n).to_a.reduce(:*)}
7+
end
8+
def calc(n,r)
9+
@factorial[n]/(@factorial[r]*@factorial[n-r])
10+
end
11+
end
12+
13+
c = Combinatorics.new
14+
count = 0
15+
(1..100).to_a.each{|n|
16+
(1..100).to_a.each{|r|
17+
count += 1 if c.calc(n,r)>1_000_000
18+
}
19+
}
20+
puts count

0 commit comments

Comments
 (0)