Code to Generate e one Digit at a Time

2020-02-05 11:02发布

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.

4条回答
▲ chillily
2楼-- · 2020-02-05 11:38

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
查看更多
Melony?
3楼-- · 2020-02-05 11:55

If you call random.seed(n) from the random module with a known n, the result will be the same each time:

>>> import random
>>> random.seed(4) # chosen by fair dice roll
>>> random.randint(0, 9)
2
>>> random.randint(0, 9)
1
>>> random.randint(0, 9)
3
>>> random.randint(0, 9)
1
>>> random.seed(4) # same seed as above
>>> random.randint(0, 9)
2
>>> random.randint(0, 9)
1
>>> random.randint(0, 9)
3
>>> random.randint(0, 9)
1

If you need to pass the state around, use the Random class (somewhat underdocumented):

>>> r = random.Random(4)
>>> r.randint(0, 9)
2
>>> r.randint(0, 9)
1

It's easy to make a generator out of this which lets you produce multiple sequences that don't step on each other's toes:

def random_digits(seed):
  r = random.Random(seed)
  while True:
    yield r.randint(0, 9)
查看更多
The star\"
4楼-- · 2020-02-05 11:58

Are you maybe looking for something like this:

>>> import math
>>> i = 1
>>> while i < 10:
...     print('e = {0:.{1}f}'.format(math.e, i))
...     i += 1
... 
e = 2.7
e = 2.72
e = 2.718
e = 2.7183
e = 2.71828
e = 2.718282
e = 2.7182818
e = 2.71828183
e = 2.718281828

The standard library can give you math.e: the mathematical constant e = 2.718281..., to available precision.

查看更多
走好不送
5楼-- · 2020-02-05 11:59

If you are willing to use pi instead of e there is a digit extraction algorithm for pi. It is unknown if such an algorithm exists for e. The file bbp_pi.py in sympy provides a fine implementation of the algorithm.

查看更多
登录 后发表回答