A challenge test says:
factorialize(0) should return 1. Can anyone give a simple straightforward explanation of why 0! or zero factorial is equal to 1?
A challenge test says:
You are referring to the recursion question right?
So yeah this is more of a math question but it is always used for recursion coding challenges.
Math is fun has a pretty simple explanation.
Hope that helps!
This is a fairly good explanation:
Not sure it’s as simple as you would like though
It’s a convenience I guess? Someone better at maths could explain this, but afaik it’s because you can multiply exponents in maths. And for that to work properly 0 has to equal 1. If it equalled 0, then multiplying by 0 also equals 0 and it would prevent it working (x^2 × x^0 needs to equal x^2, not 0)
Edit: I’ve just read an explanation similar to the one in the link above that makes sense to me. You use factorials to calculate how many unique permutations of something there can be, and the formula is
n!/k!(k - n)! (“number of ways of choosing n things from a collection of k things”).
So if you want to see how many people can shake hands in a group of n people, k is 2 (2 people shake hands). If there are only 2 people (n is also 2), then obviously there can be only 1 handshake, but to calculate it using the formula:
2! / 2!(2 - 2)! 2! / 2!0! 2! / 2! 2 / 2 1
If 0! equalled 0, then that would mean the division would be
2 / 0
Which wouldn’t be correct.
(Edit2: it still doesn’t quite make it clear to me how a mathematician can just sorta wave their hands and go “let’s just make this equal 1, it’ll make the other sums work”, I don’t know enough about maths to understand why doing that doesn’t matter. I suspect my knowledge of multiplication is stuck at very basic high school level and in fact I’m slightly misunderstanding what multiplication actually does)
After knowing @JeremyLT for 17 years, I still struggle with this.
The glib answer - it is needed for all of the math to work
The real answer - ooooookaaaay…
My favorite way to think about factorial numbers in that
n! means “how many different ways can I arrange
n balls of different colors”.
Suppose I have 3 balls,
['red', 'yellow', 'blue'].
n! = 3 * 2 * 1 = 6 ways for me to arrange these balls:
['red', 'yellow', 'blue']
['red', 'blue', 'yellow']
['yellow', 'red', 'blue']
['yellow', 'blue', 'red']
['blue', 'red', 'yellow']
['blue', 'yellow', 'red']
From this you can see, this ball arranging definition makes sense, because I have
3 choices for my first ball. Once I’ve picked my first ball, I have
2 choices for my second ball, and after that I have
1 choice for my remaining ball. That’s a total of
6 different ball arrangements.
This pattern works for any number of balls.
Now, how many different ways can you arrange
1 way! You do nothing, and that is the only possible arrangement.
Looking back at the glib answer now, this ‘ball arranging logic’ shows up in a lot of deep mathematical logic, and this logic only works out right if we consistently use the idea that there is only
1 way to arrange
0 balls (or the equivalent statement for the various deep mathematical logic statements under consideration).
there was also this other, with fractions
5! / 5 = 4! 4! / 4 = 3! 3! / 3 = 2! 2! / 2 = 1! 1! / 1 = 0!
the only way to make this last line true is if 0! equals 1, as on the left hand side there is 1/1