# piBrute pi approximator # thorsdecree 2008 # import floor division using / operand (because we don't want int values!) # import decimals (for precision greater than 16 decimals of float() from __future__ import division from decimal import * # set decimal precision to 50 decimals getcontext().prec = 50 # have to make float value into string to convert to decimal pi = Decimal("3.1415926535897932384626433832795028841971693993751058209749445923078164062862") num = int(22) den = int(7) x = int(0) y = int(0) # begin... need to optimize this! It is WAY slower than float() # even with same precision... /pulls hair/ while y < 100: while x < 100000: x = 1 + x q = Decimal(num) / Decimal(den) if q < pi: num = int(num + 1) elif q > pi: den = int(den + 1) # hmm... any way to eliminate that redundancy?? I don't think # that I need that statement twice, ideally.... q = Decimal(num) / Decimal(den) print 'pi is approximately %.51s' %pi print 'approximately ', q print 'approximation is %i' %num, " / %i" %den y = 1 + y x = int(0) print '----------------------------------------------------------------------------------------' print 'pi is approximately %.51s' %pi print 'approximately ', q print 'approximation is %i' %num, " / %i" %den print 'DONE' # this is the first time I have decently commented ANYTHING I made. w00t for me! # a good programmer never comments his code, though... if it was hard to make # it should be hard to read. Thx Dombo and Richo for your help... well, Dom at least # thx to Richo for tons of other stuff, tho. 'righto!'