Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
126 views
in Technique[技术] by (71.8m points)

python - Code to Generate e one Digit at a Time

I am trying to make a constant random number generators (I mean a RNG that outputs a series of numbers that doesn't repeat, but stays the same every time it starts from the beginning). I have one for pi. I need an algorithm to generate e digit by digit to feed into the RNG, preferably in form of Python iterator or generator. I also welcome codes that generates other irrational numbers. Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes! I did it with continued fraction!

I found these code from Generating digits of square root of 2

def z(contfrac, a=1, b=0, c=0, d=1):
    for x in contfrac:
        while a > 0 and b > 0 and c > 0 and d > 0:
            t = a // c
            t2 = b // d
            if not t == t2:
                break
            yield t
            a = (10 * (a - c*t))
            b = (10 * (b - d*t))
            # continue with same fraction, don't pull new x
        a, b = x*a+b, a
        c, d = x*c+d, c
    for digit in rdigits(a, c):
        yield digit

def rdigits(p, q):
    while p > 0:
        if p > q:
           d = p // q
           p = p - q * d
        else:
           d = (10 * p) // q
           p = 10 * p - q * d
        yield d

I made the continued fraction generator:

def e_cf_expansion():
    yield 1
    k = 0
    while True:
        yield k
        k += 2
        yield 1
        yield 1

and put them together:

def e_dec():
    return z(e_cf_expansion())

Then:

>>> gen = e_dec()
>>> e = [str(gen.next()) for i in xrange(1000)]
>>> e.insert(1, '.')
>>> print ''.join(e)
2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427427466391932003059921817413596629043572900334295260595630738132328627943490763233829880753195251019011573834187930702154089149934884167509244761460668082264800168477411853742345442437107539077744992069551702761838606261331384583000752044933826560297606737113200709328709127443747047230696977209310141692836819025515108657463772111252389784425056953696770785449969967946864454905987931636889230098793127736178215424999229576351482208269895193668033182528869398496465105820939239829488793320362509443117301238197068416140397019837679320683282376464804295311802328782509819455815301756717361332069811250996181881593041690351598888519345807273866738589422879228499892086805825749279610484198444363463244968487560233624827041978623209002160990235304369941849146314093431738143640546253152096183690888707016768396424378140592714563549061303107208510383750510115747704171898610687396965521267154688957035035

Bonus: Code to generate continued fraction for sqrt(n) where n is a positive integer and sqrt(n) is irrational:

def sqrt_cf_expansion(S):
    """Generalized generator to compute continued
       fraction representation of sqrt(S)"""
    m = 0
    d = 1
    a = int(math.sqrt(S))
    a0 = a
    while True:
        yield a
        m = d*a-m
        d = (S-m**2)//d
        a = (a0+m)//d

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...