Vimal CK
Vimal CK

Reputation: 3563

How Factorial logic works?

I have got a code snippet to find out the factorial number for a given value. I tried to find out the code flow by debugging the snippet. But still i am confused about the flow. Below is the code snippet i have, Can anyone help me to understand the flow?

static void Main()
{
   long value = factorial(5);
}

static long factorial(long num)
{
   if (num <= 1)
        return 1;
   else
        return num * factorial(num - 1);
} 

Upvotes: 3

Views: 2980

Answers (5)

saidesh kilaru
saidesh kilaru

Reputation: 748

Recursion is when a function calls itself. Recursive function allows you to divide your complex problem into identical single simple cases which can handle easily. This is also a well-known computer programming technique: divide and conquer.

Upvotes: 1

TGH
TGH

Reputation: 39268

This is a recursive function which means that it calls itself with different input. It will continue to call itself until it reaches a stoping point which is often referred to as the "base" case. When the base case is reached all function calls will yield an integer return value, which means the multiplications may take place as the return values are evaluated all they way to the initial call. It's essentially a weird thing to wrap your head around at first, but it's a very useful concept to grasp.

In closing: Factorial is often used to explain how recursion works, but it's also the clearest case for when you shouldn't use it. Factorial is much better solved using a for loop. The most common use case for recursion is when you don't know how many times a procedure must be repeated. N-factorial is not a good candidate for that since it's already determined that you have to do something n times.

Upvotes: 2

pcnThird
pcnThird

Reputation: 2372

The method is recursive which means it calls itself. With each continuous call, it decrements the current number by 1. Example: Factorial of 5: 5*4*3*2*1 = 120.

Factorials work like this:

Original number: 5
(5)*(5-1)*(5-2)*(5-3)*(5-4)

Once the current number is at 1 or less (0), the method multiplies the previous value by 1. This is also useful if the number originally passed to the method is 1 or 0 because both 0! and 1! are 1.

Upvotes: 1

Tilak
Tilak

Reputation: 30718

Recursive Methods in c# in an interesting read on the same topic.

A Recursive usuallly, has the two specifications:
-> Recursive method calls itself so many times until being satisfied.
-> Recursive method has parameter(s) and calls itself with new parameter values.

Factorial by recursion Wiki

Upvotes: 1

p.s.w.g
p.s.w.g

Reputation: 149030

It's called recursion, and it's a fundamental concept in computer science. The factorial function is a classic example of recursion, since it's typically defined in a recursive manner.

factorial(0) := 1
factorial(1) := 1
factorial(n) := n * factorial(n - 1)

So for any number 0 or 1, factorial is defined as a constant value, and for any number n > 1, it can be computed by multiplying recursively. The program will continue to multiply n, n-1, n-2, ... until it reaches 1.

Upvotes: 8

Related Questions