Re: [Tutor] Tutor Digest, Vol 95, Issue 55

2012-01-22 Thread Steven D'Aprano

Shreesh bhat wrote:

*Lucky Numbers*
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?


Very little of this is relevant to your problem. In the future, please provide 
a short, self-contained, correct example that demonstrates the problem.


http://sscce.org/

Here is the shortest example I can see, a single line of code:

xrange(2**31-1, 2**31)

which gives the same error:

Traceback (most recent call last):
  File "", line 1, in 
OverflowError: long int too large to convert to int

(The error message itself can vary from version to version.)

Possible solutions:

* Don't use xrange, write your own generator which will do the job.

def my_xrange(start, stop):
i = start
while i < stop:
yield i
i += 1


This will work, but will be much slower.


* Scale your numbers from time to time, to avoid them getting too big.
* Can you rethink your algorithm and avoid needing such huge numbers?
* Just accept that your function can't handle such huge numbers.




Traceback (most recent call last): File
"/run-1327085301-1965755690/solution.py",
 line 35, in li=map(islucky,xrange(startnum, endnum))
OverflowError: Python int too large to convert to C long

---
It shows this error for very large numbers or slows down with large numbers.



It slows down for large numbers because you have written a very inefficient 
isprime() function.





I m using Ubuntu 32-bit.



What is more important than the version of your operating system is the 
version of Python.




On Sun, Jan 22, 2012 at 4:24 AM,  wrote:

[snip hundreds of irrelevant lines]

Please do not reply to digest posts without deleting the unnecessary quoting, 
and setting the subject line appropriately.


You may find it useful to read this:

http://catb.org/esr/faqs/smart-questions.html



--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 95, Issue 55

2012-01-22 Thread Alan Gauld

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, mailto:tutor-requ...@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
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 95, Issue 55

2012-01-21 Thread Shreesh bhat
*Lucky Numbers*
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?
Input:
The first line contains the number of test cases T. Each of the next T
lines contains two integers, A and B.
Output:
Output T lines, one for each case containing the required answer for the
corresponding case.

Constraints:
1 <= T <= 1
1 <= A <= B <= 10^18
Sample Input:
2
1 20
120 130
Sample Output:
4
1
Explanation:
For the first case, the lucky numbers are 11, 12, 14, 16.
For the second case, the only lucky number is 120.

---
My solution:

def isprime(n):
  n=abs(int(n))
  if n<2:
return False
  if n==2:
return True
  if not n & 1:
return False
  for x in range(3,int(n**0.5)+1,2):
if n % x == 0:
  return False
  return True

def islucky(n):
  sum1=0
  sum2=0
  while n!=0:
r=n%10
sum1+=r
sum2+=r*r
n=n/10
  if isprime(sum1) & isprime(sum2):
return True
  return False

number=raw_input()


for i in range(int(number)):
inp=raw_input()
a=inp.split()
startnum=int(a[0])
endnum=int(a[1])
li=map(islucky,xrange(startnum, endnum))
count=0
for j in li:
if j:
count+=1
print count
---

Traceback (most recent call last): File
"/run-1327085301-1965755690/solution.py",
 line 35, in li=map(islucky,xrange(startnum, endnum))
OverflowError: Python int too large to convert to C long

---
It shows this error for very large numbers or slows down with large numbers.
I m using Ubuntu 32-bit.

On Sun, Jan 22, 2012 at 4:24 AM,  wrote:

> Send Tutor mailing list submissions to
>tutor@python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
>http://mail.python.org/mailman/listinfo/tutor
> or, via email, send a message with subject or body 'help' to
>tutor-requ...@python.org
>
> You can reach the person managing the list at
>tutor-ow...@python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Tutor digest..."
>
>
> Today's Topics:
>
>   1. OverFlow Error (Shreesh bhat)
>   2. Re: OverFlow Error (Alan Gauld)
>   3. Re: Tutor Digest, Vol 95, Issue 53 (George Nyoro)
>   4. Re: Tutor Digest, Vol 95, Issue 53 (Steven D'Aprano)
>   5. Re: delete an object from method (was Tutor Digest) (Dave Angel)
>   6. checking return status of 'ping' in windows (Nikunj Badjatya)
>   7. Re: checking return status of 'ping' in windows (Hugo Arts)
>
>
> --
>
> Message: 1
> Date: Sat, 21 Jan 2012 18:40:28 +0530
> From: Shreesh bhat 
> To: tutor@python.org
> Subject: [Tutor] OverFlow Error
> Message-ID:
> >
> Content-Type: text/plain; charset="iso-8859-1"
>
> How to correct this error?
>
> * OverflowError: Python int too large to convert to C long*
> -- next part --
> An HTML attachment was scrubbed...
> URL: <
> http://mail.python.org/pipermail/tutor/attachments/20120121/f7b86624/attachment-0001.html
> >
>
> --
>
> Message: 2
> Date: Sat, 21 Jan 2012 13:57:20 +
> From: Alan Gauld 
> To: tutor@python.org
> Subject: Re: [Tutor] OverFlow Error
> Message-ID: 
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> On 21/01/12 13:10, Shreesh bhat wrote:
> > How to correct this error?
> >
> > * OverflowError: Python int too large to convert to C long*
>
>
> Could we have some context?
>
> What version of Python? What OS?
> What does your code look like?
> Can we see the full error trace please?
>
> Otherwise, based only on what you posted, the only advice
> I can give you is to use a smaller int!
>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
>
> --
>
> Message: 3
> Date: Sat, 21 Jan 2012 17:58:17 +0300
> From: George Nyoro 
> To: tutor@python.org
> Subject: Re: [Tutor] Tutor Digest, Vol 95, Issue 53
> Message-ID:
> >
> Content-Type: text/plain; charset="iso-8859-1"
>
> Hey guys,
> I've been making an application and have made a delete method where the
> user can delete the instance of that application. e.g. if I have a table
> object, I need to be able to delete that instance from within the class and
> then it becomes accessible.
> -- next part --
> An HTML attachment was scrubbed...
> URL: <
>