Background

When I learned to code, I hadn’t slept in a math classroom in 14 years.

I studied philosophy and taught courses on Symbolic Logic. I can talk about deductive proofs for days, but when people pull out a of n’s… x²…_ [insert Greek letter] — _I nod along, but trust me, I have no idea what’s happening.

For this reason, I got a huge boost of empowerment when I figured out an algorithm question asking, “How can you determine the mathematical combination (nCr) of two numbers?”

If you’re like ‘what the…?’, then hell yeah, you are me a day ago. Let’s go!

The Meat

Defining the Problem

I’ll try my best to explain a combination:

Image for post

Imagine you’re losing a high stakes poker game. You have sweat on your neck as a couple Mafia grunts stare daggers at you. As the big one cracks his knuckles, you think “It’s a shame I never learned to count cards. But maybe start now?”

Your mind is racing. You start thinking, how do I start? How many possible hands are there in the deck? Well, there are 52 cards. You get 5 distinct cards in each hand, and they can come in any order. Let’s see, some quick math in your head and…

Image for post

Yeah, the answer is not obvious. But, you’re attempting a combination. A combination is when you have a collection of items and you want to find all the possible selections from that collection — for example, every possible hand from a deck of cards.

By the way, the answer is 2,598,960. Each hand has a 1/2,598,960th chance of being drawn. With numbers that big, it’s good we can train a computer to solve the problem.

#ruby #combination #ruby-on-rails #algorithms #dev #algorithms

Algorithms: Calculating Combination with Ruby
1.55 GEEK