def test(n=100*1000): print("%dth Fibonacci number of various types:" % n) for z in 0, gmpy.mpz(0), gmpy.mpf(0): tip, tim, tot = timedfib(n, z) print(" %5.3f %s %s" % (tim, gmpy.fdigits(tot,10,6), tip)) tip, tim, tot = timedfibsp(n, 1) print(" %5.3f %s %s" % (tim, gmpy.fdigits(tot,10,6), "gmpy.fib"))
#!/usr/bin/python # Calculate digits of e # Marcus Kazmierczak, [email protected] # July 29th, 2004 # the formula # e = 1/0! + 1/1! + 1/2! + 1/3! + 1/4! + ... 1/N! # You should chop off the last five digits returned # they aren't necessarily accurate # precision library import gmpy # how many digits (roughly) N = 1000 gmpy.set_minprec(int(N*3.5+16)) e = gmpy.mpf('1.0') for n in range(1,N+3): e = gmpy.fdigits(gmpy.mpf(e) + (gmpy.mpf('1.0') / gmpy.fac(n))) print e
import gmpy, time def timedfib(n, zero): start=time.clock() a=zero; b=a+1 for i in range(n): a,b=b,a+b stend=time.clock() return type(zero), stend-start, a def timedfibsp(n, one): start=time.clock() result=gmpy.fib(n) stend=time.clock() return type(one), stend-start, result def test(n=100*1000): print "%dth Fibonacci number of various types:" % n for z in 0L, gmpy.mpz(0), gmpy.mpf(0): tip, tim, tot = timedfib(n, z) print " %5.3f %s %s" % (tim, gmpy.fdigits(tot,10,6), tip) tip, tim, tot = timedfibsp(n, 1) print " %5.3f %s %s" % (tim, gmpy.fdigits(tot,10,6), "gmpy.fib") if __name__=='__main__': test()