Re: [Tutor] OverflowError in lucky numbers script

2012-01-23 Thread Shreesh bhat
I tried optimizing everything all things you guys pointed out and still its
orders of magnitude away from the expected result.
The program should check the islucky condition between range of (1,10**18)
numbers and iterate over that 10**5 times.
This program slows down more than 16 secs at (1,10**8) and 1 time.
Which approach should i follow?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] OverflowError in lucky numbers script

2012-01-23 Thread Shreesh bhat
I have given the definition of lucky numbers and constraints involved at
the starting of the thread.
when a number's sum of digits and square of sum of digits is prime,it is
called lucky.

I already tried generating prime numbers using sieve of atkin algorithm
rather than doing primality test.
Efficiency improved a lot but still couldnt reach 16 sec target.

The large numbers are the only hindrance to the speed.
Since i m new to Python.I dont know all its recipes and tricks inlvolved to
charm-the-snake.
So can i improve the islucky method more than what i mentioned before?
Can i work around that way in python or should i come up with a new
algorithm?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] OverflowError in lucky numbers script

2012-01-23 Thread Shreesh bhat
No,i meant sum of digits is prime and also sum of square of digits is prime.
E.g: 23 is lucky cos
2+3=5 (prime)
2**2+3**2 = 4+9 = 13 (prime)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] OverflowError in lucky numbers script

2012-01-22 Thread Shreesh bhat
I m using Python 2.7
Steven wrote:
 Scale your numbers from time to time, to avoid them getting too big 
What does this mean?

inp refers to the sample input test case I have given at first.Its a string
containing two numbers,
The program has to handle large numbers till 10**18 and also has to execute
considerably fast (within 16 CPU time).

Using xrange() causes the OveflowError,Whereas using range() causes too
many items
Is writing my own generator only solution? Or is there another way in which
i can generate big numbers and in considerably fast manner?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] OverflowError in lucky numbers script

2012-01-22 Thread Shreesh bhat
Calculating the table is fast.
I think either my luckiness test (where i find the sum of all digits and
sum of squares of all digits of a large number)
or generating numbers is slow.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] OverflowError in lucky numbers script

2012-01-22 Thread Shreesh bhat
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,1)
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?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] OverFlow Error

2012-01-21 Thread Shreesh bhat
How to correct this error?

* OverflowError: Python int too large to convert to C long*
___
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 n2:
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-requ...@python.org 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 shreeshbha...@gmail.com
 To: tutor@python.org
 Subject: [Tutor] OverFlow Error
 Message-ID:
ca+xjmj5mvbwqvbtwhnwo4_5+xpdd_pnv2c66m1+vm6w+a7t...@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 +
 From: Alan Gauld alan.ga...@btinternet.com
 To: tutor@python.org
 Subject: Re: [Tutor] OverFlow Error
 Message-ID: jfeg80$dh5$1...@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 geony...@gmail.com
 To: tutor@python.org
 Subject: Re: [Tutor] Tutor Digest, Vol 95, Issue 53
 Message-ID:
CAM71YVE43XUXv5FmOvaXhL3Pv=-jvrvuoxehu-fqky08oeh...@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