944,095 Members | Top Members by Rank

Ad:
  • Python Code Snippet
  • Views: 10599
  • Python RSS
0

Python can handle Very Large Numbers

by on Aug 1st, 2005
Scientists and deficit spenders like to use Python because it can handle very large numbers. I decided to give it a test with factorials. Factorials reach astronomical levels rather quickly. In case you can't quite remember, the factorial of 12 is !12 = 1*2*3*4*5*6*7*8*9*10*11*12 = 479001600, that is 479 million and some change! After that languages like C++ start to fizzle quickly! At !16 we get past the nations debt. Python can handle that and more! I stopped at !69 only because the display started to wrap the line. The result has 98 digits in it, I thought I made my point anyway.
Python Code Snippet (Toggle Plain Text)
  1. # check the numeric range of Python with huge factorials
  2. # factorial(69) still checks out accurately! Has 98 digits in it!
  3. # 171122452428141311372468338881272839092270544893520369393648040923257279754140647424000000000000000
  4. # tested with Python24 vegaseat 01aug2005
  5.  
  6. def getFactorial(n):
  7. """returns the factorial of n"""
  8. if n == 0:
  9. return 1
  10. else:
  11. k = n * getFactorial(n-1)
  12. return k
  13.  
  14. for k in range(1, 70):
  15. print "factorial of", k,"=", getFactorial(k)
Comments on this Code Snippet
Oct 8th, 2010
0

Re: Python can handle Very Large Numbers

I didn't know Python could handle such big numbers natively. Cool stuff, particularly for cryptography I'm sure. I hit a wall at 999.

Python Syntax (Toggle Plain Text)
  1. >>> getFactorial(999)
  2. File "<stdin>", line 5, in g
  3. File "<stdin>", line 5, in g
  4. ...
  5. File "<stdin>", line 5, in g
  6. RuntimeError: maximum recursion depth exceeded

Try this definition instead. It seems to be getting the same results as yours, but is non-recursive.

Python Syntax (Toggle Plain Text)
  1. >>> def getFactorial(n):
  2. ... r = 1
  3. ... for x in range(n):
  4. ... r = r * (n + 1)
  5. ... return r
  6. ...
  7. >>> getFactorial(100000)
  8. (insert monster number here)L
Newbie Poster
mlin is offline Offline
1 posts
since Oct 2010
Oct 8th, 2010
0

Re: Python can handle Very Large Numbers

i know this for a long time. lol

I think i had something x 10^500 or 10^5000 at one point.
Posting Whiz in Training
ultimatebuster is offline Offline
250 posts
since Mar 2010
Message:
Previous Thread in Python Forum Timeline: how to display text entered in a textbox(SharpDevelop)?
Next Thread in Python Forum Timeline: Selectable Label with Tkinter?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC