[Tutor] OverflowError in lucky numbers script
Shreesh bhat
shreeshbhat90 at gmail.com
Mon Jan 23 07:10:53 CET 2012
Thank you all for helping me understand the overflow error.
I m a newbie on mailing lists.I apologize for my errors.
Program:
def sieve(maxi):
primes = range(2,maxi+1)
for i in primes:
j = 2
while i * j <= primes[-1]:
if i * j in primes:
primes.remove(i*j)
j += 1
return primes
maxi=(10**2)*18 #Generating the table till the largest possible prime
tab=sieve(maxi)
table={}
for i in tab:
table[i]=0
def isprime(n):
return table.has_key(n)
count=0
def islucky(n): # modified islucky function
global count
sum1=0
sum2=0
for letter in str(n):
tmp=ord(letter)-48
sum1+=tmp
sum2+=tmp**2
if isprime(sum1):
if isprime(sum2):
count+=1
number=raw_input() # Number of test cases.Its constraint (1,10000)
def execute():
global count
for i in range(int(number)):
inp=raw_input() # startnumber and endnumber pair. Its constraint
(1,10**18)
a=inp.split()
startnum=int(a[0])
endnum=int(a[1])
count=0
while startnum != endnum:
islucky(startnum)
startnum+=1
print count
execute()
The program is executing correctly but it has to execute 16 seconds for the
constraints.
I have optimized the way i sum up digits and used "consult-table" approach.
Still the program doesn't reach the 16 seconds target.
How to achieve this target?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120123/d97dbbc0/attachment-0001.html>
More information about the Tutor
mailing list