LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 03-25-2013, 12:27 AM   #1
otkaz
LQ Newbie
 
Registered: Apr 2009
Location: Houston, TX
Posts: 26

Rep: Reputation: 0
python overflow error


I keep getting
OverflowError: Python int too large to convert to C long
on a python script when I run it on 32 bit systems but it works as expected on 64bit
Code:
for number in xrange(int("0101010101"), int("9898989898")+1):
        number = `number`.zfill(10)
        prev = ''
        i = 0
        for c in `number`:
                if c in prev:
                        break
                else:
                        i += 1
                        prev = c
                if i>=len(`number`):
                        print number
Here is a perl equivalent which works on both 32bit and 64bit
Code:
my $num = "";
for $num ("0101010101" .. "0101989898"){
        if ($num =~ /00|11|22|33|44|55|66|77|88|99/o) {
                ++$num;}
        else {
                print "$num\n";}}
I'm trying to use this on a raspberry pi which is 32bit I need to do this in python if posible. Is there an easy way to get around this error?
 
Old 03-25-2013, 03:56 AM   #2
pan64
Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 5,146

Rep: Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364
I would make something like this:
Code:
for number in xrange(int("0101010101"), int("9898989898")+1):
  number = `number`.zfill(10)
  n = str(number)
  found = 0
  for c in range(1, len(n) -1):
    found += n[c] == n[c+1]
  if not found:
    print number
 
Old 03-26-2013, 12:40 AM   #3
otkaz
LQ Newbie
 
Registered: Apr 2009
Location: Houston, TX
Posts: 26

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by pan64 View Post
I would make something like this:
Code:
for number in xrange(int("0101010101"), int("9898989898")+1):
  number = `number`.zfill(10)
  n = str(number)
  found = 0
  for c in range(1, len(n) -1):
    found += n[c] == n[c+1]
  if not found:
    print number
Thanks for the suggestion but still get overflow errors with your code. The problem seems to be with xrange. I ended up doing this which is really messy but works.
Code:
import itertools

for number in itertools.count(int("0101010101"), 1):
        number = `number`.zfill(10).strip('L')
        prev = ''
        i = 0
        if number > "9898989898":
                break
        for c in `number`:
                if c in prev:
                        break
                else:
                        i += 1
                        prev = c
                if i>=len(`number`):
                        print number
The .strip('L') is there because for some reason python after counting past 3000000000 starts adding a L to the end.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RS232 buffer overflow error ravi_shanker Linux - Kernel 1 09-13-2011 02:25 AM
Python 2.6 dependencies error installing python-sqlite2-2.5.5-1.1.i586.rpm jmp007 Linux - Newbie 1 02-11-2011 12:05 PM
apache2 heap overflow error ? jalfaro Linux - Software 0 06-29-2006 03:43 PM
Neighbor Table Overflow error snowmonkey Linux - Software 1 11-04-2003 05:10 PM
math overflow error tda Programming 12 04-03-2003 11:43 AM


All times are GMT -5. The time now is 03:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration