Python can handle Very Large Numbers

vegaseat vegaseat is offline Offline Aug 1st, 2005, 9:57 am |
0
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.
Quick reply to this message  
Python Syntax
  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)

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC