Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 数字的前几位_Python - Fatal编程技术网

Python 数字的前几位

Python 数字的前几位,python,Python,我想要无限循环的fibonocci级数的前9位和后9位。单独运行测试,我使用模运算符no得到了更好的结果,最后9个数字与前9个数字相当。我运行了不同的测试,比如int(str(b)[:-1])%10**9,b/(10**(len(str(b))-9)),但结果仍然相同。我认为这是因为更高数字的数字到字符串的转换。有没有其他方法可以打印前9位而不转换为字符串,或者有无字符串的有效方法 def fibby(): a,b = 1,1 yield [a,a] yield [b,b

我想要无限循环的fibonocci级数的前9位和后9位。单独运行测试,我使用模运算符no得到了更好的结果,最后9个数字与前9个数字相当。我运行了不同的测试,比如
int(str(b)[:-1])%10**9,b/(10**(len(str(b))-9))
,但结果仍然相同。我认为这是因为更高数字的数字到字符串的转换。有没有其他方法可以打印前9位而不转换为字符串,或者有无字符串的有效方法

def fibby():
    a,b = 1,1
    yield [a,a]
    yield [b,b]
    while True:
        a,b = b,a+b
        yield [str(b)[:9], b%10**9]

这里有一些版本和获取前30000个前缀的时间安排。为了简单起见,我省略了前两个收益率和最后一个数字

  • fibby1
    是使用
    str(b)[:9]
    的原始方法
  • fibby2
    跟踪10除以的适当幂
  • fibby3
    保留
    a
    b
    中的前9位,保留
    a
    b
    中的其余数字。与
    fibby2
    相比,这避免了将大的
    b
    除以大的10次方。大的数字只能加/减/比较,或与小的数字相乘
  • fibby4
    使用@therefromhere建议的
    math.log10
  • fibby5
    使用该模块
输出:

All agree? True
fibby1: 24.835 seconds
fibby2:  0.289 seconds
fibby3:  0.201 seconds
fibby4:  2.802 seconds
fibby5:  0.216 seconds
为了比较,我还尝试了str(b%10**9),即最后九位数字,耗时0.506秒。这比我的前九位快速解决方案要慢

守则:

def fibby1():
    a, b = 1, 1
    while True:
        yield str(b)[:9]
        a, b = b, a+b

def fibby2():
    a, b = 1, 1
    div = 1
    while True:
        while True:
            front = b // div
            if front < 10**9:
                break
            div *= 10
        yield str(front)
        a, b = b, a+b

def fibby3():
    a,b = 1,1
    A,B,C = 0,0,1
    while True:
        yield str(b)
        a, b = b, a+b
        A, B = B, A+B
        if B >= C:
            B -= C
            b += 1
        if b >= 10**9:
            A += a%10 * C
            B += b%10 * C
            a //= 10
            b //= 10
            C *= 10

def fibby4():
    from math import log10
    a, b = 1, 1
    while True:
        yield str(b // 10**max(0, int(log10(b) - 8)))
        a, b = b, a+b

def fibby5():
    from decimal import Decimal, getcontext
    getcontext().prec = 7000 # enough for n = 30000
    a, b = Decimal(1), Decimal(1)
    while True:
        yield str(b)[:9]
        a, b = b, a+b

from timeit import timeit
from itertools import islice
from time import time

n = 30000
t0 = time()
list1 = list(islice(fibby1(), n))
t1 = time()
list2 = list(islice(fibby2(), n))
t2 = time()
list3 = list(islice(fibby3(), n))
t3 = time()
list4 = list(islice(fibby4(), n))
t4 = time()
list5 = list(islice(fibby5(), n))
t5 = time()
print('All agree?', list1 == list2 == list3 == list4 == list5)
print('fibby1: %6.3f seconds' % (t1 - t0))
print('fibby2: %6.3f seconds' % (t2 - t1))
print('fibby3: %6.3f seconds' % (t3 - t2))
print('fibby4: %6.3f seconds' % (t4 - t3))
print('fibby5: %6.3f seconds' % (t5 - t4))
def fibby1():
a、 b=1,1
尽管如此:
收益率str(b)[:9]
a、 b=b,a+b
def fibby2():
a、 b=1,1
div=1
尽管如此:
尽管如此:
前=b//div
如果前部<10**9:
打破
div*=10
产量(前)
a、 b=b,a+b
def fibby3():
a、 b=1,1
A、 B,C=0,0,1
尽管如此:
产量str(b)
a、 b=b,a+b
A、 B=B,A+B
如果B>=C:
B-=C
b+=1
如果b>=10**9:
A+=A%10*C
B+=B%10*C
a/=10
b/=10
C*=10
def fibby4():
从数学导入日志10
a、 b=1,1
尽管如此:
产量序列(b//10**max(0,int(log10(b)-8)))
a、 b=b,a+b
def fibby5():
从decimal导入decimal,getcontext
getcontext().prec=7000#足够n=30000
a、 b=十进制(1),十进制(1)
尽管如此:
收益率str(b)[:9]
a、 b=b,a+b
从timeit导入timeit
从itertools导入islice
从时间导入时间
n=30000
t0=时间()
list1=list(islice(fibby1(),n))
t1=时间()
list2=list(islice(fibby2(),n))
t2=时间()
list3=list(islice(fibby3(),n))
t3=时间()
list4=list(islice(fibby4(),n))
t4=时间()
list5=list(islice(fibby5(),n))
t5=时间()
打印('All agree?',list1==list2==list3==list4==list5)
打印('fibby1:%6.3f秒“%(t1-t0))
打印('fibby2:%6.3f秒“%(t2-t1))
打印('fibby3:%6.3f秒“%(t3-t2))
打印('fibby4:%6.3f秒“%(t4-t3))
打印('fibby5:%6.3f秒“%(t5-t4))

您所说的“更好的结果”是什么意思?你得到了哪些结果?它们有什么错?我不确定它是否比将整数转换成字符串更有效,但你可以使用
math.log10()
来计算数字,然后
/
进行整数除法以获取最有意义的数字。我指的是时间性能。比如,最低有效位在6秒内弹出到10**8,而MSB需要2分钟以上(单独测试)。MSB可以是LSB的时间匹配吗?@DeepakKota“在6秒内弹出到10**8”是什么意思?我正在解决项目Euler 104中的一个问题。有一个更好的算法,但当我尝试使用暴力时,我想知道第一个数字和最后一个数字是否有时间匹配。