[Tutor] Tutor Digest, Vol 95, Issue 55
Shreesh bhat
shreeshbhat90 at gmail.com
Sun Jan 22 07:11:22 CET 2012
*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 <= 10000
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, <tutor-request at python.org> wrote:
> Send Tutor mailing list submissions to
> tutor at 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-request at python.org
>
> You can reach the person managing the list at
> tutor-owner at 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 <shreeshbhat90 at gmail.com>
> To: tutor at python.org
> Subject: [Tutor] OverFlow Error
> Message-ID:
> <CA+XJMJ5mVBWqVbTwhNwO4_5+XpdD_PnV2c66M1+vm6W+a7TySg at mail.gmail.com
> >
> 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 +0000
> From: Alan Gauld <alan.gauld at btinternet.com>
> To: tutor at python.org
> Subject: Re: [Tutor] OverFlow Error
> Message-ID: <jfeg80$dh5$1 at dough.gmane.org>
> 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 <geonyoro at gmail.com>
> To: tutor at python.org
> Subject: Re: [Tutor] Tutor Digest, Vol 95, Issue 53
> Message-ID:
> <CAM71YVE43XUXv5FmOvaXhL3Pv=-jvrvuOXEhU-fQky08oEHXCA at mail.gmail.com
> >
> 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: <
> http://mail.python.org/pipermail/tutor/attachments/20120121/d6796ae7/attachment-0001.html
> >
>
> ------------------------------
>
> Message: 4
> Date: Sun, 22 Jan 2012 02:07:45 +1100
> From: Steven D'Aprano <steve at pearwood.info>
> To: tutor at python.org
> Subject: Re: [Tutor] Tutor Digest, Vol 95, Issue 53
> Message-ID: <4F1AD4C1.3040406 at pearwood.info>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> George Nyoro wrote:
> > 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.
>
> Did you want to ask a question, or are you just sharing?
>
>
> --
> Steven
>
>
>
> ------------------------------
>
> Message: 5
> Date: Sat, 21 Jan 2012 10:27:34 -0500
> From: Dave Angel <d at davea.name>
> To: George Nyoro <geonyoro at gmail.com>
> Cc: tutor at python.org
> Subject: Re: [Tutor] delete an object from method (was Tutor Digest)
> Message-ID: <4F1AD966.2070702 at davea.name>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> On 01/21/2012 09:58 AM, George Nyoro wrote:
> > 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.
> >
>
> If you're going to misuse so many terms in one query, you'll need to
> supply some code, and tell us in what way it doesn't serve your needs.
>
> In the meantime,
>
> 1) Post a query by addressing it to "tutor at python.org," not by replying
> to a digest message. And if you must reply to a digest message, at
> least change the subject. Thanks, though for deleting the digest content.
>
> 2) How do you expect to delete the instance of the application? It's a
> funny term, but the only meaning I can come up with is you want to kill
> the application's process.
>
> 3) What's a table object? If table is the name of your class, it really
> ought to be uppercase.
>
> 4) "from within the class" -- I'm guessing you mean from within a
> method of the class.
>
> 5) "then it becomes accessible" -- perhaps you mean inaccessible.
>
> 6) Please tell us the version of Python you're using and the operating
> system you're running on.
>
>
>
> --
>
> DaveA
>
>
>
> ------------------------------
>
> Message: 6
> Date: Sun, 22 Jan 2012 00:17:39 +0530
> From: Nikunj Badjatya <nikunjbadjatya at gmail.com>
> To: tutor <tutor at python.org>
> Subject: [Tutor] checking return status of 'ping' in windows
> Message-ID:
> <CAOwQg9u6esoRAvUipX+ZshiFcbc_2gvQmYd8dZFcgQu6617NWA at mail.gmail.com
> >
> Content-Type: text/plain; charset="iso-8859-1"
>
> Hi All,
>
> I am using the following snippet to check the availability of an IP
> address. If that IP addr is found free than it can be used later on for
> further operations.
> Python ver 3.2
> Windows OS
>
> {{{
> pingret = subprocess.Popen('ping {0}'.format(IPaddr),
> shell=True,universal_newlines=True, \
> stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
> status = pingret.wait()
> if status == 0:
> print("WARN The IP given in the input is not free")
> .....
> .....
> }}}
>
> Normal ping operation on windows cmd prompt can give 3 outputs.
> 1) "destination host unreachable
> 2) "request timed out"
> 3) "Reply from 192.168.1.1: bytes=32 time=3ms TTL=64"
>
> Now,
> I was expecting the "status" in above snippet to hold '0' only in case of
> no. 3)
> But even when we have case 1), 'status' is holding '0'.
> i.e. The exit status of ping is 0, even when destination host is
> unreachable.!
>
> How do I make my snippet to work as desired. i.e even if destination host
> is unreachable, 'status' should hold '1' and hold '0' only when it gets
> reply from that ip address.??
>
>
> Thanks,
>
> Nikunj
>
>
> --
> *7*Switch off as you go |*q*Recycle always | P Save Paper - Save Trees | Go
> Green
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <
> http://mail.python.org/pipermail/tutor/attachments/20120122/0250ad3d/attachment-0001.html
> >
>
> ------------------------------
>
> Message: 7
> Date: Sat, 21 Jan 2012 23:53:42 +0100
> From: Hugo Arts <hugo.yoshi at gmail.com>
> To: Nikunj Badjatya <nikunjbadjatya at gmail.com>
> Cc: tutor <tutor at python.org>
> Subject: Re: [Tutor] checking return status of 'ping' in windows
> Message-ID:
> <CAJmBOfni1jcSEwz65g76sXeuuM9oXepcZMsTXXrXECcoJdbkUQ at mail.gmail.com
> >
> Content-Type: text/plain; charset=UTF-8
>
> On Sat, Jan 21, 2012 at 7:47 PM, Nikunj Badjatya
> <nikunjbadjatya at gmail.com> wrote:
> > Hi All,
> >
> > I am using the following snippet to check the availability of an IP
> address.
> > If that IP addr is found free than it can be used later on for further
> > operations.
> > Python ver 3.2
> > Windows OS
> >
> > {{{
> > pingret = subprocess.Popen('ping {0}'.format(IPaddr),
> > shell=True,universal_newlines=True, \
> > ? ? ? ? ? ? ? ? ? ? stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
> > status = pingret.wait()
> > if status == 0:
> > ? ? print("WARN ?The IP given in the input is not free")
> > .....
> > .....
> > }}}
> >
> > Normal ping operation on windows cmd prompt can give 3 outputs.
> > 1) "destination host unreachable
> > 2) "request timed out"
> > 3) "Reply from 192.168.1.1: bytes=32 time=3ms TTL=64"
> >
> > Now,
> > I was expecting the "status" in above snippet to hold '0' only in case of
> > no. 3)
> > But even when we have case 1), 'status' is holding '0'.
> > i.e. The exit status of ping is 0, even when destination host is
> > unreachable.!
> >
>
> This appears to be windows specific. Linux ping will return an exit
> code of 1 if either zero responses are received or a packetcount and
> deadline are specified and not met. I'm not sure why it doesn't work
> that way on windows, but testing a bit for myself it seems to be the
> platform's fault and not python's, since calling EXIT 1 will correctly
> return a status code of 1.
>
> > How do I make my snippet to work as desired. i.e even if destination
> host is
> > unreachable, 'status' should hold '1' ?and hold '0' only when it gets
> reply
> > from that ip address.??
> >
>
> rather than using the wait() call, use Popen.communicate() which
> returns the output of the program, and check that directly for your
> three cases.
>
> HTH,
> Hugo
>
>
> ------------------------------
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
> End of Tutor Digest, Vol 95, Issue 55
> *************************************
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120122/8e3ba4e9/attachment-0001.html>
More information about the Tutor
mailing list