The Computer Language
Benchmarks Game

pidigits Python 3 #2 program

source code

# The Computer Language Benchmarks Game
# http://benchmarksgame.alioth.debian.org/
#
# contributed by Rene Bakker
# fixed by Isaac Gouy

import sys
from io import StringIO
from gmpy2 import xmpz,div,mul,add

N = int(sys.argv[1])
f = StringIO()

w = xmpz(0)
k = 1

n1  = xmpz(4)
n2  = xmpz(3)
d   = xmpz(1)
f10 = xmpz(10)
n10 = xmpz(-10)

i = 0
while True:
    # digit
    u = int(div(n1,d))
    v = int(div(n2,d))
    if u == v:
        f.write(chr(48+u))
        i += 1
        if i % 10 == 0:
            f.write("\t:%d\n" % i)

        if i == N:
            break

        # extract
        u  = mul(d, mul(n10, u))
        n1 = mul(n1, f10)
        n1 = add(n1, u)
        n2 = mul(n2, f10)
        n2 = add(n2, u)
    else:
        # produce
        k2 = k << 1
        u  = mul(n1, k2 - 1)
        v  = add(n2, n2)
        w  = mul(n1, k - 1)
        n1 = add(u, v)
        u  = mul(n2, k + 2)
        n2 = add(w, u)
        d  = mul(d, k2 + 1)
        k += 1;

if i % 10 != 0:
    f.write("%s\t:%d\n" % (' ' * (10 - (i%10)),N))
print(f.getvalue(),end="")
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
Python 3.6.1 (default, Apr 18 2017, 10:33:41)
[GCC 6.3.0 20170406]
--enable-optimizations --with-lto
make profile-opt


Wed, 19 Apr 2017 00:07:42 GMT

MAKE:
mv pidigits.python3-2.python3 pidigits.python3-2.py
0.01s to complete and log all make actions

COMMAND LINE:
/usr/local/src/Python-3.6.1/bin/python3.6 -OO pidigits.python3-2.py 2000

PROGRAM FAILED 


PROGRAM OUTPUT:

Traceback (most recent call last):
  File "pidigits.python3-2.py", line 9, in <module>
    from gmpy2 import xmpz,div,mul,add
ModuleNotFoundError: No module named 'gmpy2'