Ruby

Day 10 of 30 - Ruby Coding Challenge - Recursive Factorial Numbers

Day 10 of 30 - Ruby Coding Challenge - Recursive Factorial Numbers

Hey!

Today we’re going to solve the previous Factorial problem using recursion in Ruby 👌. This is the post version of the Youtube video :)

Just a reminder, it doesn’t matter if you’re using Ruby, Python, JavaScript, or your favorite language, what really matters is the concepts and logic to solve problems

If you haven’t watched the previous video, I just want to remember the definition of a Factorial number

A Factorial number N is the product of all positive integers less than or equal to the number N

We indicate a Factorial number as N!

Real example:

5! = 5*4*3*2*1
# result in 120

A possible (and not so beautiful) solution to this problem is the solution in the first video:

def factorial(number)
  result = number
  while number > 1
    result = result * (number - 1)
    number = number - 1
  end
  return result
end

puts factorial(5) # 120

Recursion

Again, just recalling what the recursion technique is:

Recursion is when you solve a problem by breaking the problem down into smaller versions of the same problem

And when it comes to coding, the method or function calls on itself :)

Before going to code, let’s see the first example:

5! = 5*4*3*2*1

But you can actually calculate this way:

5! = 5*4!

Because 4! = 432*1, which is a small part of the 5! problem

You can go further and say:

5! = 5*4*3!

A little bit more

5! = 5*4*3*2!

And finally

5! = 5*4*3*2*1

Notice that we’ve broken that down into smaller Factorial calculations

The recursive Ruby method will be the following:

def factorial(number)
  return 1 if number == 0
  return number * factorial(number - 1)
end

Notice that:

  • returns 1 if the number is equal to 0. That’s the criteria to stop the method execution
  • returns number * factorial(number - 1) which, as we saw, is a method calling on itself

A more leaner version of this method in Ruby would be:

def factorial(number)
  number == 0 ? 1 : number * factorial(number - 1)
end

Also notice that:

  • We don’t need to explicitly use return in Ruby
  • We’re using a ternary condition, which in our case makes the readability (in my opinion) much better

That’s it!

Hope you liked it and see you tomorrow in the next challenge :)

Don’t forget to keep in touch and say hi Alex

Twitter Youtube Instagram Linkedin GitHub

Join My Newsletter

You’ll receive coding courses, tutorials and personal posts! I’ll be happy to send them to you, weekly :)