[Tutor] Tutor Digest, Vol 95, Issue 55

Alan Gauld alan.gauld at btinternet.com
Sun Jan 22 11:05:01 CET 2012

On 22/01/12 06:11, Shreesh bhat wrote:

Here goes some general comments that will make it esier to understand 
your code and therefore, hopefully, the problem.

> def isprime(n):
>      ....

> def islucky(n):

There are more efficient ways of doing both tests
but I'll igniore that for now.

> number=raw_input()

It helps to include a prompt in raw_input.
Not only does it help the user know what to type but it can help the 
reader understand what the value represents. This is apparently
a number but what the number is for I have no idea. Which leads to the 
next comment, that variable names should reflect the pourpose of the 
variable not its type.

> for i in range(int(number)):
>      inp=raw_input()

As above, I have no idea what inp represents so I can
only guess at its content

>      a=inp.split()
>      startnum=int(a[0])
>      endnum=int(a[1])

This might be a good place to insert a print statement
showing the values...

>      li=map(islucky,xrange(startnum, endnum))

And here is where you get the error, so presumably you have used 
integers which are too big for xrange?

>      count=0
>      for j in li:
>          if j:
>              count+=1
 >      print count

You could just use the count method of the list:

  print li.count(True)

> It shows this error for very large numbers

Yes thats what it says, the numbers are too big for xrange
to process. You need to find another way to do it, or
build your own pure python equivalent of xrange() - but
that will be even slower!.

> or slows down with large numbers.

large numbers mean lots of iterations. They also mean that Python is 
having to work harder because it's not using the underlying C integers.
Thats the price you pay for processing big numbers. But think on the 
bright side: its still faster than you could do it using pencil and 
paper! :-)

But you can speed it up a bit by making your tests more efficient...

> On Sun, Jan 22, 2012 at 4:24 AM, <tutor-request at python.org
> <mailto:tutor-request at python.org>> wrote:
>    ....
>     When replying, please edit your Subject line so it is more specific
>     than "Re: Contents of Tutor digest..."

Please follow this instruction...
And also, while you are at it trim all the content thats not relevant.
Some people pay for their internet access by the byte...

Alan G
Author of the Learn to Program web site

More information about the Tutor mailing list