Computer Science: An Overview: Global Edition (12th Edition)

Published by Pearson Higher Education
ISBN 10: 1292061162
ISBN 13: 978-1-29206-116-0

Chapter 5 - Algorithms - Chapter Review Problems - Page 268: 57

Answer

Yes — this is the standard recursive definition of factorial.

Work Step by Step

The given code is correct. It returns 1 if 1 when N is 0, which is the base case and in other cases it returns N*(fact(N-1)) , on substituting the value of fact(N-1) we will get $N*((N-1) * fact(N-2)) $ and if we keep on substituting until base case we will get $N*(N-1)*(N-2)*...*3*2*1 $ which is the definition of N!. Hence the given code is correct. Iterative version of the program- def fact(N): ans=1 while(N>0): ans = ans * N N-=1 return ans
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.