2 ms·
If an optimization means that an algorithm reaches the desired result or not, that's not an optimization anymore. def fact(n, r=1): if n == 1: return r
by bad_user 17y ago
If an optimization means that an algorithm reaches the desired result or not, that's not an optimization anymore.
def fact(n, r=1):
if n == 1: return r
return fact(n-1, r * n)
Regardless of any available stack-space you may have, for the above method (which can be tail-call optimized) try calculating fact(1000000). If that's the requirement, then without TCO this implementation is incorrect.
- eru 17y agoIndeed. Though you have to exclude preservation of timing (and memory usage) effects from your definition of semantics to make any optimization possible.