freeCodeCamp Basic Algorithm: Factorialize a Number

Factorialize a Number Banner

We are back to break down another one of the Basic Algorithm challenges from freeCodeCamp.

Last time we looked at the Reverse a String algorithm. This time we are going to tackle the second challenge, Factorialize a Number.

This one certainly sounds more difficult than the first challenge. What the heck does factorialize even mean? According to my spell check, it isn’t even a word!

The Challenge

Let’s ignore for the time being whether factorialize is a word and start by looking at the challenge itself.

Factorialize a Number algorithm assignment

There is a lot of math terminology being thrown around which makes the problem seem more difficult than it is.

If we look at the example, things start to make a lot more sense.

We are given a number, in this example: 5. We want to multiply each number between 1 and 5, like so: 1 * 2 * 3 * 4 * 5.

This is what they mean by factorialize.

Let’s look at some of the other examples they give us to make sure we are on the right track.

Factorialize algorithm test cases

We already know that factorializing 5 is going to return 120 (1 * 2  * 3 * 4 * 5 = 120). What if we factorialize 10?

1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 does indeed equal 3,628,800 so that checks out.

This same method also works using 20 as the example but I won’t waste time typing out all the numbers.

Now that we know what we need to do, we need to start thinking about how to do it using code.

Brainstorming a solution

In the previous algorithm breakdown, I mentioned that the helpful links provided will often point you towards a solution.

In this case, the helpful links aren’t really all that helpful.

As with most algorithms, there are a few different ways that we can go about solving the problem.

The most efficient way is probably to use Recursion but that may not be something with which you are familiar. The freeCodeCamp curriculum hasn’t covered Recursion at the point at which this challenge is presented.

With that in mind, I am not going to use Recursion in the solution I show you, but I would strongly encourage you to get familiar with the concept. It comes up regularly when solving algorithms. Check out this documentation to learn more about Recursion.

We need to work our way through a series of numbers, which sounds like the perfect job for a Loop.

You could use a While Loop, but I went with a For Loop since it is what I was most comfortable with at the time.

What I did

First, I wrote a For Loop that looked like this:

My initial solution to the factorialize a number algorithm

The variable i is equal to num – 1, which in this example would be 5 – 1. The loop will continue to run as long as i is greater than or equal to 1. Each time the loop runs, i will be reduced by one.

Inside the loop, the variable num is equal to the previous value of num times i.

That means, that the first time through the loop, num would equal 5 (the previous value of num) * 4 (i).

The second time through, num would equal 5 * 4 (the previous value of num) * 3 (i).

The third time through, num would equal 5 * 4 * 3 (the previous value of num) * 2 (i).

The fourth time through, num would equal 5 * 4 * 3 * 2 (the previous value of num) * 1 (i). The loop would end at this point since afterward i would be reduced to 0.

The result of this loop is that num is now equal to 5 * 4 * 3 * 2 * 1 for a total of 120, which passes the test when num is returned.

This loop also passes when we are given 10 or 20 as the number to factorialize.

But there is a problem…

What about the final test case, when we need to factorialize 0?  The correct solution should return 1 but our solution returns 0.

To account for this, I added a simple If Statement before the For Loop that returns 1 if num is either 0 or 1.

Another solution to the factorialize algorithm

Turns out, the second test for num equaling 1 isn’t necessary but I didn’t catch that at the time.

This solution passes all the tests but in preparation for this article, I realized that we can avoid the If Statement by tweaking how the For Loop is written.

A better solution

Our original solution was just a little more complicated than it needed to be.

We can use all the same basic logic and come up with a more simplified version that looks like this:

Final solution for factorialize a number algorithm

As you can see, it is very similar to what we came up the first time.

It works in much the same way except we start with i is equal to 1 and we track the loop counter using num instead of i.

The role of i and num is also reversed inside the loop. Now each time the loop runs i is equal to the previous value of i multiplied by num.

Because we start with i being equal to 1, this solution also passes the test when we factorialize 0.

The wrap-up

As I mentioned before, these aren’t the only solutions for this algorithm. They aren’t necessarily the best solutions either (don’t forget to read about Recursion!).

In fact, I hate to even use the term “best solution” because it is so relative to where you are on your coding journey.

Early in your journey, the “best solution” is going to be anything that works.

You can see that by looking at my first solution.

At the time that was the best solution I could come up with. Now, after several more months of learning, I can look at that old solution and see how to improve it.

Working with algorithms is a two-step process.

The first step is to find a solution that works. The second step is to look for ways to improve upon that solution.

The fact that there can be so many different ways to solve the same problem is part of what makes algorithms so interesting.

Have you tackled this algorithm yet? What type of solution did you come up with?

Sharing is caring!

Author: Lee

Hi, my name is Lee and I am a 40-something who recently made the decision to become a self-taught programmer. This site was set up to chronicle that journey and my experiences along the way. Feel free to contact me with any comments, questions, or suggestions.

Leave a Reply

Your email address will not be published. Required fields are marked *

15 − four =