The Factorialize a number solution is strange

I solved this challenge pretty quickly, but I did in a way that makes sense to me. I have started making a habit of looking at the suggested solutions. While sometimes my solutions are exactly correct, sometimes they’re very different.

In the case of “Factorialize a Number” challenge, the suggested solution is this:

function factorialize(num) {
  if (num === 0) { return 1; }
  return num * factorialize(num-1);
}

But this is not really a natural way for me to think about it.

My solution is this:

let i = 1;
  while(num > 0) {
    i *= num;
    num--;
  }
  return i;

My solution makes more sense. What should I consider when determining the efficiency and/or performance? Why is the suggested solution better?

What you are seeing is two different approaches. The solution that you found (which is only one of the perfectly valid ways to solve this problem) is a recursive solution while yours is iterative. Sometimes one solution will be much more elegant or intuitive. Other times it doesn’t really matter. If you are interested, you can also do some research on when recursion is more or less efficient, for which languages, and why.

2 Likes

Thanks for your response!

Here’s a response I liked. Obviously, they’re talking about Java which runs on a virtual machine and is way different than javascript, but memory is memory, right?

"
The fact is that recursion is rarely the most efficient approach to solving a problem, and iteration is almost always more efficient. This is because there is usually more overhead associated with making recursive calls due to the fact that the call stack is so heavily used during recursion.
This means that many computer programming languages will spend more time maintaining the call stack then they will actually performing the necessary calculations.

Does recursion use more memory than iteration? Generally speaking, yes it does. This is because of the extensive use of the call stack.

Should I use recursion or iteration?

Recursion is generally used because of the fact that it is simpler to implement, and it is usually more ‘elegant’ than iterative solutions. Remember that anything that’s done in recursion can also be done iteratively, but with recursion there is generally a performance drawback. But, depending on the problem that you are trying to solve, that performance drawback can be very insignificant – in which case it makes sense to use recursion. With recursion, you also get the added benefit that other programmers can more easily understand your code – which is always a good thing to have.
"

Recursion is a crucial and necessary concept in computer science. However in practice, iterative approach is often prefers, some compilers, if they can, will roll recursive logic into iterative logic underneath for performance purposes.

The concept is important because it enables you to think and solve algorithm problems critically. Some questions are easier to solve in recursive logic than iterative logic. In complex algorithms, there is often a need to simplify, divide and conquer in order to have an optimal solution, and it requires you to use recursive logic

1 Like