python - 使用 Python 2.7.9 的 TWOSQRS SPOJ 给出运行时错误 (NZEC)

标签 python algorithm python-2.7

我试图使用 Python 2.7.9 作为我的编码语言来解决 SPOJ 的 TWOSQRS 问题,并为此设计了一个程序。由于代码运行完美,没有在给定测试用例输入的情况下在我的系统上引发任何异常。如果有人可以为我提供更多测试用例或找到我代码中的错误,那将会很有帮助。

问题链接:http://www.spoj.com/problems/TWOSQRS/

    # -*- coding: utf-8 -*-
'''To solve the problem 2 primary condition that should be met are:
    1. No should not be of the form 4k+3 as for sum of square of two nos will always be of form 4k or 4k+1
    2. All the prime factors of form 4k+3 should have even power from the Fermat thorem.
   Steps involved in solving the problem are:
    1. Sieve a list of prime nos. upto 1000001 as in problem. 
    2. Check if all the prime factors has even powers.
    3. Check if the no is not of form 4k+3'''

import numpy

def sieve(n):
    """ An implementation that sieves separately 
    for primes of the form 6i−1 and 6i+1, due to Robert William Hanks"""

    prime = numpy.ones(n//3 + (n%6==2), dtype=numpy.bool)
    for i in range(3, int(n**.5) + 1, 3):
        if prime[i // 3]:
            p = (i + 1) | 1
            prime[       p*p//3     ::2*p] = False
            prime[p*(p-2*(i&1)+4)//3::2*p] = False
    result = (3 * prime.nonzero()[0] + 1) | 1
    result[0] = 3
    return numpy.r_[2,result]

primes=sieve(10**6+1)            #List of all the prime upto 10**6

def main():
    noOfCase=input()
    for i in range(noOfCase):
        N=input()
        is_multiple= True
        i = 0
        while(primes[i]*primes[i] <= N):
       count = 0;
       while (N % primes[i] == 0):
           count+=1;
           N/= primes[i];
           if (primes[i]%4 == 3 and count%2 == 1):
               is_multiple = False;
               break;
           i+=1

    if (N%4 == 3):
       is_multiple = False
    if(is_multiple):
       print "Yes"
    else:
       print "No"


main()

最佳答案

您的问题几乎可以肯定是您使用的是 NumPy,但选择了不支持它的 Python 集群/版本。

作为PYTHON: What you should know解释说,Pyramid 集群没有适用于任何 Python 版本的 NumPy,Cube 集群适用于所有 CPython 版本但没有 PyPy 版本。

关于python - 使用 Python 2.7.9 的 TWOSQRS SPOJ 给出运行时错误 (NZEC),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30660581/

相关文章:

algorithm - 给定 RNG 算法和一系列数字,是否有可能确定产生该系列的种子?

algorithm - 计算非凸多边形地理坐标的面积

python - 为什么我不能使用 python smtplib 向自己发送邮件?

python regular express 如何获取一个值,该值将由 "&page=2"处理或不能处理

python - 迭代 pandas 数据框并创建另一个具有重复记录的数据框

python - SampleRNN - Pytorch 实现初学者

python - 返回一个对象 vs 返回一个元组

python - 使用带有 psycopg2 的二进制 COPY 表 FROM

c++ - 从 std::string 中删除特定的连续字符重复

python - DLL 加载失败 : The specified module could not be found when doing "from mpi4py import MPI"