• Faresh@lemmy.ml
    link
    fedilink
    English
    arrow-up
    4
    ·
    10 months ago

    I’ve no idea why the factorial example is so popular,

    It is because anyone who has done anything with math (as is the case with cs students), will know how the factorial is defined in math which is also a recursive definition, so the code ends up looking almost exactly like the definition in math, and because of its simplicity, it’s excellent to introduce students to the idea of recursion. So, it’s a combination of familiarity and simplicity that makes it such a well known example.

    Of course, in the real world you would use the implementation from a library. So whether recursion or iteration is better for factorials isn’t that important of a question, since you won’t be using your solution in a normal situation. The factorial example is supposed to be only used as nothing more than an example.