java 自然常数e中出现的连续的第一个10个数字组成的质数_(e.com中第一个10位数的质数).com python google challenge 2004...

以下是一种方法: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

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, '.')

找到e中的前10位素数(我的贡献):for i in range(len(e[2:])-10):

x = int(reduce(operator.add,e[2:][i:i+10]))

if isprime(x):

print x

print i

break

打印:7427466391

98

这意味着e中的前10位素数出现在与http://explorepdx.com/firsten.html一致的小数点后的第98位,在“答案的位置”下。在import operator

import decimal as dc

def edigits(p):

dc.getcontext().prec = p

factorial = 1

euler = 2

for x in range(2, 150):

factorial *= x

euler += dc.Decimal(str(1.0))/dc.Decimal(str(factorial))

return euler

estring = edigits(150).to_eng_string()[2:]

for i in range(len(estring)-10):

x = int(reduce(operator.add,estring[i:i+10]))

if isprime(x):

print x

print i

break

打印:7427466391

98

正如@MarkDickinson所指出的,一种更简单的方法是直接使用十进制模块生成具有必要精度的e。例如:import operator

import decimal

decimal.getcontext().prec = 150

e_from_decimal = decimal.Decimal(1).exp().to_eng_string()[2:]

for i in range(len(e_from_decimal)-10):

x = int(reduce(operator.add,e_from_decimal[i:i+10]))

if isprime(x):

print x

print i

break

打印:7427466391

98

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值