DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Python (http://www.daniweb.com/forums/forum114.html)
-   -   Code Snippet: Python can handle Very Large Numbers (http://www.daniweb.com/forums/thread216587.html)

vegaseat Aug 1st, 2005 9:57 am
Python can handle Very Large Numbers
 
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.

  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)

All times are GMT -4. The time now is 11:54 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC