Recursion

Bob Calco rcalco at cortechs.com
Mon Jan 1 17:08:38 EST 2001


Tim:

# You'll find that it's a lot faster to use iteration than
# recursion in these
# languages.

One of Remco's examples did precisely that.

def factor(N):
  n = 1L
  for i in xrange(1,N+1):
    n *= i
  return n

I tried it with 5000, and then 10000, and while it isn't super fast in
either case, and seemed more than twice as slow at 10000 than 5000, it sure
did work.

# Python distinguishes between ints (native C "long") and longs (unbounded)
# because ints are often used as arguments to C libraries.  If you want
# unbounded ints, you have to "do something" to get them.  This distinction
# made more sense 10 years ago when it was first made; it's gotten
# more clumsy
# not because of factorials <wink>, but because systems are getting larger,
# and systems that support files larger than 2Gb are becoming more common
# (i.e., 32-bit signed ints no longer suffice for some common file
# operations).  The distinction between ints and longs isn't as sharp as it
# used to be:  every release, a few more pieces of the internals tolerate
# either.  Over time, I expect the user-visible distinction will mostly
# vanish.

And mostly used to either straight programming languages with lots of types
or completely typeless scripting languages, I overlooked the obvious fact,
which dawned on me mere seconds after I hit the "Send" button (as such
things always do), that Python did in fact make such a distinction. If I
were trying to do this in C or Object Pascal, I don't think I'd have made
this error even if I were completely smashed drunk. For one thing, the
compilers wouldn't let me...

While I like the idea of teaching programming concepts with high level
languages, there's still no substitute for real C (even Assembly)
experience, and no reason whatsoever to believe that such experience would
not be most helpful in really mastering even a simple but subtle script
language like Python. "Apply what you know" -- if that's the sign of
intelligence, I just got humbled. :/

# Except you didn't test recursion:  you inadvertently tested limits on
# integer sizes (or, in Perl's case, the overflow limit of IEEE-754
# doubles).
#
# most-people-should-never-use-numbers<wink>-ly y'rs  - tim

See my last email response to Remco. I was just trying to do something
simple that didn't require much thought. Alas, there is no such thing in
this business!

I'm fitting myself for a nice designer model dunce cap right about now,
green and yellow, to match the Python icon... Thanks to all who helped me
determine the right size (XL)!

most-people-should-never-try-to-work-with-a-mild-hangover-ly yours,

;)

Bob Calco





More information about the Python-list mailing list