Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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中得到SPOJ prime1错误答案_Python - Fatal编程技术网

在python中得到SPOJ prime1错误答案

在python中得到SPOJ prime1错误答案,python,Python,我用python中的以下代码来回答SPOJ的PRIME1问题,结果是错误的。我自己已经在各种测试用例上测试过它,但是找不到失败的测试用例。有人能找出我代码中的问题吗 from math import sqrt def sieve_1(n): l = [True for i in xrange(int(sqrt(n)) + 1)] m = [] for i in xrange(2, int(sqrt(n)) + 1): if l[i] == True:

我用python中的以下代码来回答SPOJ的PRIME1问题,结果是错误的。我自己已经在各种测试用例上测试过它,但是找不到失败的测试用例。有人能找出我代码中的问题吗

from math import sqrt

def sieve_1(n):
    l = [True for i in xrange(int(sqrt(n)) + 1)]
    m = []
    for i in xrange(2, int(sqrt(n)) + 1):
        if l[i] == True:
            m.append(i)
            for j in xrange(i, int(sqrt(n)) + 1, i): l[j] = False
    #print m
    return m

def sieve_2(a, b):
    l = sieve_1(b)
    t = a
    r = [True for i in xrange(a, b + 1)]
    if a == 1 : r[0] = False
    for p in list(l):
        if (a/p)*p != a: a = (a/p)*p + p
        for i in xrange(a - t, b + 1 - t, p): r[i] = False
    for i in list(l):
        if i > t: r[i - t] = True
    return r


def prime(a, b):
    l = sieve_2(a, b)
    for i in xrange(0, b + 1 - a):
        if l[i] == True:
            print i + a


n = int(raw_input())
for i in xrange(n):
    a, b = map(int, raw_input().split())
    prime(a, b)
    if i != n - 1: print' '