[Tutor] OverflowError in lucky numbers script
Dave Angel
d at davea.name
Mon Jan 23 01:14:30 CET 2012
You sent me this message privately, instead of on the list (use
Reply-All in most mail programs). Two problems with that: 1) nobody
else gets to help 2) I don't give private help, except as a contractor.
On 01/22/2012 12:44 PM, Shreesh bhat wrote:
> *Lucky numbers:*
> 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=(9**2)*19
> tab=sieve(maxi)
> table={}
> for i in tab:
> table[i]=0
>
> def isprime(n):
> return table.has_key(n)
>
> count=0
>
> def islucky(n):
> 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()
> def execute():
> global count
> for i in range(int(number)):
> inp=raw_input()
> a=inp.split()
> startnum=int(a[0])
> endnum=int(a[1])
> count=0
> while startnum != endnum:
> islucky(startnum)
> startnum+=1
> print count
>
> execute()
>
> Hi Sir,
> The program still doesn't work with "consult-table" approach.
Define "doesn't work". If you give a specific trace-back message,
somebody is bound to recognize the problem. Or if it doesn't give an
exception, describe in what way the answer is wrong. Or if it's
completely correct, but simply too slow, then say so.
> I have also optimised digit's sum method.
> Can you please tell me another method to work around it to get more
> execution speed?
>
What part is slow? Calculating the table, or looping through your while
loop?
--
DaveA
More information about the Tutor
mailing list