长整数溢出错误和分辨率 [英] Long integer overflow error and resolution

查看:78
本文介绍了长整数溢出错误和分辨率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个给我一个溢出错误的程序.我意识到,造成这种情况的原因是由于我的输入可以进行直到2147483646的输入.我使用sys.maxint知道了这一点.除此之外的任何事情都给我带来了溢出错误.如何获取较大值的输入?价值,2147483646我的系统挂起.如何处理这样的错误.这里给出问题的说明:如果数字的总和以及数字的平方和均为质数,则该数字称为幸运". A和B之间有多少个数字是幸运的?

I am writing a program that gives me an overflow error. I realized that the cause of that was due to my input which could do inputs till 2147483646. I figured this using the sys.maxint. Anything beyond that gave me an overflow error. How I can take in inputs for large values? For value, 2147483646 my system hangs. How to deal with such an error. The statement of the problem here is given, A number is called lucky if the sum of its digits, as well as the sum of the squares of its digits is a prime number. How many numbers between A and B are lucky?

我在这里附上示例代码.

I am attaching the sample code here.

class luckynumbers():
    #required numbers are only 0-9, their values can be stored in a lookup table
    def __init__(self):
            self.squarelist=[0,1,4,9,16,25,36,49,64,81]

    def isEven(self, n):
            if n%2 ==0:
                    return True
            else:
                    return False

    def isPrime(self,n):
            return_val=True
            if n==2:
                    return_val= True
            if not self.isEven(n):
                    for i in xrange(2,n/2):
                            if n%i ==0:
                                    return_val=False
                                    break
            else:
                    return_val= False
            return return_val

    def sumofDigits(self,n):
            return sum(map(int, n))

    def generateSquares(self, n):
            return map(lambda x: self.squarelist[x], map(int,n))

    def satisfy(self,n):
            return self.isPrime(self.sumofDigits(n)) and self.isPrime(sum(self.generateSquares(n)))

def main():
    luckyno=luckynumbers()
    t=int(raw_input().rstrip())
    count = []
    for i in xrange(t):
            counts = 0
            a,b = map(int, raw_input().rstrip().split())
            if a==1:
                    a=2
            for j in xrange(a,b+1,1):
                     if luckyno.satisfy(str(j)):
                            counts+=1
            count.append(counts)
    for i in count:
            print i

if __name__=='__main__':
    main()

我仍然在看python中的长整数文档.但是还没有想办法.我已经尝试过使用优化.还有什么,我会很感激.

I am still looking at long integers document in python. But haven't figured a way yet. I have tried to use optimization as far as I can think of. Anything more, I will really grateful.

推荐答案

xrange()函数仅限于整数(不是long).如果您仅用range()调用替换发生OverflowError的xrange()调用,该代码就会按预期工作.

The xrange() function is limited to ints (not longs). The code works as expected if you replace the xrange() call where OverflowError occurs with just a range() call.

这篇关于长整数溢出错误和分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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