code生成电子邮件一位数字时代 [英] Code to Generate e one Digit at a Time

查看:133
本文介绍了code生成电子邮件一位数字时代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让一个恒定的随机数发生器(我的意思是RNG的输出一系列不重复的数字,但它开始一样,每次停留从一开始)。我有一个对圆周率。我需要一个算法,通过数字生成电子邮件数位送入RNG,preferably在Python迭代器或发电机的形式。我也欢迎codeS产生其他无理数。先谢谢了。

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.

推荐答案

是的!我做到了与连分数!

Yes! I did it with continued fraction!

我发现从2的平​​方根生成的数字,这些code

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

和把它们放在一起:

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

然后:

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

奖金:code,产生持续的分数开方(n),其中n是一个正整数,开方(n)是不合理的:

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

这篇关于code生成电子邮件一位数字时代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆