Skip to content

Shuffling Cards

So last week I entered this programming competition and one of the questions was about permutations of letters. Essentially, there was some string of letters and we had to count how many permutations of that string were substrings of a larger string we were given.

The details don’t really matter, the main point is that at some point pretty much everyone in the competition thought about using the permutations function to solve it. Basically what it does is it takes some string as an input and outputs all of the permutations of that string. For example if you gave it ‘abc’, it would output: [‘abc’, ‘acb’, ‘bac’, ‘bca’, ‘cab’, ‘cba’].

Now this was all well and good for small 3 letter strings, but what a lot of us didn’t initially realise was the sheer number of permutations that came with even slightly longer strings. Even when you just increase the input to strings with 10 characters, the function outputs over 3 and a half MILLION strings. Somehow none of us realised how impossible using this function would be, and I think it comes down to the fact that humans have a pretty terrible idea of how fast factorials grow. So let’s talk about it using cards.

Alright, let’s suppose you’re shuffling a deck of cards. Every time you shuffle, the 52 cards fall in some arrangement. We can count the total number of arrangements possibly quite easily. For our first card, we have 52 choices. For the second, we have 51 (since we’ve already used one card), for the 3rd, 50, and so on until we reach the final card. The total product is 52 x 51 x 50 x… 3 x 2 x 1 = 52! (That’s 52 factorial, I’m not just really excited about the number 52) This is what we mean by the number permutations of the deck. This is also the number of strings that the ‘permutations’ function would’ve output if we had given it a 52-letter input. 

The number of possible permutations of a deck

Now, we can write 52! in standard form; it’s 8.06 x 10^67. Simple enough. But that doesn’t even come CLOSE to representing how massive that is. So let’s try and visualise it.

We’re gonna make some assumptions. Let’s say around the world, there are 5000 decks of cards being shuffled every second. Doesn’t seem like too unreasonable an estimate. Supposing every new shuffle made is an arrangement that’s never been made before, how long do you reckon it’ll take to get through all of them at that rate? A couple of years? A couple of centuries? Millennia? The lifetime of the UNIVERSE?

Not. Even. Close.

5000 shuffles a second means 1.61 x 10^64 seconds. That is a LONG time. By contrast, the age of the universe is only about 4.3 x 10^17 seconds. 

Let’s do a thought experiment in an attempt to visualise this length of time. Suppose you’re waiting until that momentous day when, at last, every single possible arrangement of 52 cards will have been done. What can you do to pass the time? I mean, apart from reading Astronomical Blunder Comics? 

Well, you could type out every single word in the Oxford English Dictionary, of course! But don’t worry about being a bad or slow typer. For our purposes, you’re just gonna have to type at the gentle pace of one word every billion years.

Yes, you read that correctly. One word. Every billion YEARS. The sun will swallow up the Earth in its entirety and collapse into a white dwarf long before you’ve finished your first page. And remember while all this is going on, you’ve still got 5000 new arrangements a SECOND being made.

Roughly equal to one word every billion years.

So, there’s about 600,000 words in the OED as of now, and that’s the copy we’ll use. After painstakingly typing out all 600,000 words at that snaillike rate, go take a trip to the Sahara Desert and remove a singular grain of sand (Let’s just pretend the Earth still exists after this time. Nothing else about this scenario is normal either). Delete all those words you spent all that time typing up, and restart from the beginning, at the same rate. When you’ve once again compiled the entirety of the OED, take out another grain of sand from the Sahara.  Repeat until the Sahara is no longer a desert, just a landscape of bedrock. That should be about 1.5 septillion grains of sand, which means 1.5 septillion copies of the OED, all typed at 1 word every billion years.

Now that you’ve reduced the Sahara to just large rocks and gravel, let’s help out the environment a bit. Let’s remove one kilogram of CO2 from the atmosphere (we’re also going to assume no-one is going to be polluting earth anymore because they’ll all be too busy waiting for all the card arrangements to be reached). So, re-fill the Sahara, delete all the words and start typing again. 

It’s a thought experiment, don’t read too deeply into it…

Finish the OED, then remove a grain of sand until you drain the Sahara, then remove a kilogram of CO2. Once ALL of the world’s CO2 has been removed and the atmosphere is completely clear of any carbon…

There will be 1.60 x 10^64 seconds left on the clock. We won’t have even gone through 1% of all the possible combinations. Only once we do that entire process 160 more times, will we have gone through every single permutation. Which means putting back all the carbon in the atmosphere, filling the sahara with sand, starting typing on a blank new document at the rate of 1 word every billion years, and repeat. One hundred and sixty times. 1.61 x 10^64 seconds is a LOOONG time.

This seems almost ridiculously false, because a deck of cards really isn’t all that much; it’s something you can fit in the palm of your hand. Yet the combinations that can be derived from it are barely comprehensible or countable from a human perspective. 

So just know that every time you rearrange your 52 cards, there’s a pretty good chance that it will be the first, and probably only, time that specific arrangement is made for all of human history. If you’re ever feeling unspecial and want to create a historic new moment that will likely never occur again…

Shuffle a deck of cards.