Epsilon: [Python] Request for an iterative version of factorial

Created on 6 Oct 2018  路  6Comments  路  Source: numworks/epsilon

Right now, trying to compute a factorial in Python yields an error when the number is more than 9. This is a good example of the limitations of the recursion but yield to comments like 芦the Numworks is not even able to compute factorial(10) like the other calculators禄. So why not propose instead an iterative version, which would works without showing the limitations of micropython? Something like

 def factorial(n):

    p = 1

    for k in range(1,n+1):

        p *= k

    return p
app-python enhancement

Most helpful comment

In the official web based simulator there seem to be much tighter restrictions, as factorial(10) easily runs into the maximum recursion depth error. Perhaps the simulator should be updated to more closely reflect the hardware limitations?

Since it's being presented as a try before you buy option it might leave the wrong impression. I certainly thought it would be limited the same way on the real hardware

All 6 comments

My Numworks, running 1.7.0, will calculate 33! using the recursive micropython expression. I agree it gives an error on 34!, but I have Poincare if I really need bigger factorials, and I find it useful to have an example of recursion.

I agree that not even being able to manage 10! would be unimpressive, but I don't find that to be the case. Which version are you running?

To be fair, the Numworks can actually calculate factorial up to 100 exactly and up to 170 in scientific notation. I got the same result in python preinstalled factorial.py script as madhatter0 on both the "official" firmware and the "bleeding edge" one. The calculator is made for students, and the factorial.py is fine example for learning recursion. I think the script was included for this purpose, not for calculating factorials. It can be made able to calculate factorial up to 34, by replacing the condition n == 0 by n < 2 :) Or to manage the negative numbers:

def factorial(n):
    if n < 0:
        return None
    elif n <= 1:
        return 1
    else:
        return n * factorial(n-1)

Maybe the error is also for good to demonstrate what happens when programmer does not take the recursion depth into account.

In the official web based simulator there seem to be much tighter restrictions, as factorial(10) easily runs into the maximum recursion depth error. Perhaps the simulator should be updated to more closely reflect the hardware limitations?

Since it's being presented as a try before you buy option it might leave the wrong impression. I certainly thought it would be limited the same way on the real hardware

Hello,
I think the emulator should be at the same level as the physical machine.
If the calculator is initially intended for students, to make them understand the limitations of recursion seems premature to me.
I think factorial.py should be written with an iterative loop.
Regards

There are standard loops in one of the other python scripts provided, so my personal feeling is that, as a teaching tool, a single example of recursion (and the limits thereof!) is no bad thing.

Hi, I am closing this since we do not have factorial.py by default anymore :)

Was this page helpful?
0 / 5 - 0 ratings