Ask your own question, for FREE!
Mathematics 40 Online
OpenStudy (anonymous):

What does the following algorithm return? f(n){ if (n < 2) return 1 else return f(n – 1) * n:

OpenStudy (anonymous):

Would it not depend on the value of 'n'?

OpenStudy (kinggeorge):

This looks like the recursive algorithm for finding the factorial of n.

OpenStudy (anonymous):

Let's assume 'n' is 3, It would return 3X2 = 6. How is that the factorial?

OpenStudy (kinggeorge):

3*2*1=6

OpenStudy (anonymous):

well it is in that case, i just realized but what if it was 4?

OpenStudy (kinggeorge):

4*3*2*1=24

OpenStudy (anonymous):

It just multiplies 4X3, not 2 and 1.

OpenStudy (anonymous):

f(n-1) * n

OpenStudy (kinggeorge):

it multiplies \(f(3)*4 =6*4=24\) since \(f(3)=6\)

OpenStudy (anonymous):

looks like the recursive definition of factorial right?

OpenStudy (anonymous):

so n!

OpenStudy (kinggeorge):

exactly.

OpenStudy (anonymous):

yes, that is it. multiply n - 1 th number by n

OpenStudy (anonymous):

Ok thanks

OpenStudy (anonymous):

Alright. I'll take your word for it.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!