Discussion:
[Tutor] for loop for long numbers
Dima Kulik
2015-08-03 18:15:45 UTC
Permalink
Hi to all.
Can you help me plz.
I want to make a for loop with a huge numbers.
for example:

for i in range (0,9000000000):
 make_some_code

but range and xrange cant operate with such big numbers.
Can some on help me?
Thanks.
--
Dima Kulik
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.o
Alan Gauld
2015-08-03 20:56:59 UTC
Permalink
Post by Dima Kulik
I want to make a for loop with a huge numbers.
make_some_code
but range and xrange cant operate with such big numbers.
What makes you think so? Did you try? What error did you get?
And what version of python were you using?

FWIW I tried xrange with 10**16 and it seemed to work,
albeit it took a long time.(about 15min for each 10**10
iterations)) I gave up waiting after it reached 10**11...

But if you are sure that doesn't work you can always use
a while loop instead:

n = 0
while n < 10**15
# do something
n += 1
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Mark Lawrence
2015-08-03 21:02:40 UTC
Permalink
Post by Dima Kulik
Hi to all.
Can you help me plz.
I want to make a for loop with a huge numbers.
make_some_code
but range and xrange cant operate with such big numbers.
Can some on help me?
Thanks.
You cannot do anything about it directly with Python 2 but this works
fine on Python 3. Work arounds from
http://stackoverflow.com/questions/9816603/range-is-too-large-python are:-

import itertools
range = lambda stop: iter(itertools.count().next, stop)

OR

def range(stop):
i = 0
while i < stop:
yield i
i += 1

Having said that what are you trying to achieve? If you tell us we may
be able to suggest a cleaner solution.
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Martin A. Brown
2015-08-03 21:20:00 UTC
Permalink
Greetings Dima,
Post by Dima Kulik
Can you help me plz.
I want to make a for loop with a huge numbers.
 make_some_code
but range and xrange cant operate with such big numbers.
Can some on help me?
Ah, range and xrange. It would help us to know which Python version you are
using, as there is a very slight difference in how Python 2.x and Python 3.x
deal with range().

Well, if you are using Python 2.x, I'm assuming that you are seeing something
Post by Dima Kulik
range(0,9000000000)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
MemoryError

That's because range() is trying to return you a list, and is
allocating a bunch of memory so that it can create the list. You
probably don't have this much memory in your machine.

I didn't have any problem using xrange() in a for loop in Python
2.7.3. None at all.

I also gave Python 3.3.0 a try, by running this:

for i in range(0, 9000000000):
x = 0

This also ran without a hitch....

So, perhaps you are having some sort of other problem. Would you
like to provide more detail?

-Martin

P.S. I wrote a little function to see if I could figure out how long
it would take to run a for loop on your 9 billion (~9e9), without
actually running it. I started by timing the loop on range(0,
100,000) and then increased it until it took an appreciable amount
of time for a human. At 1e9 (range of 1 billion), it took my little
anemic laptop about 12.8 seconds to count through the loop. So,
before accounting for any work that you plan to undertake inside the
loop, you have a runtime of ~115 seconds.
--
Martin A. Brown
http://linux-ip.net/
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/
Danny Yoo
2015-08-03 22:00:42 UTC
Permalink
Post by Dima Kulik
Hi to all.
Can you help me plz.
I want to make a for loop with a huge numbers.
make_some_code
Can you say more why you are trying to do this, by the way?

Without context, this request seems strange, as we often want to make
our programs do as little work as necessary to get the job done. A
computer is not often called to be a space heater.
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Dima Kulik
2015-08-04 05:03:54 UTC
Permalink
I want to ask user how many time he would like to make an iteration of crypt function. Also if he/she wants to do it numerous times, my code should work. So i use such code:
x = input("How many time you want to encrypt?")
for i in xrange(0,x+1):
 make_code

of course i can make rule to prevent from huge numbers but I want to make no limits.
Thanks.
Post by Danny Yoo
Post by Dima Kulik
Hi to all.
Can you help me plz.
I want to make a for loop with a huge numbers.
make_some_code
Can you say more why you are trying to do this, by the way?
Without context, this request seems strange, as we often want to make
our programs do as little work as necessary to get the job done. A
computer is not often called to be a space heater.
--
Dima Kulik
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options
Steven D'Aprano
2015-08-04 01:50:20 UTC
Permalink
Post by Dima Kulik
Hi to all.
Can you help me plz.
I want to make a for loop with a huge numbers.
 make_some_code
but range and xrange cant operate with such big numbers.
In Python 2, range() builds the entire list up front, so
range(9000000000) will require *at least* 144000032000 bytes, or 134 GB
of memory just for the list. That's 134 GIGABYTES.

So forget about using range.

In Python 2, xrange() is limited in the largest number you can use. If
your Python is compiled for a 32-bit operating system:

py> xrange(2**31-1)
xrange(2147483647)
py> xrange(2**31)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: Python int too large to convert to C long


2**31 is much smaller than the number you use:

py> 9000000000 - 2**31
6852516352L


If you have a 64-bit operating system, you can use a 64-bit version of
Python, and the limit will be something like 2**63 - 1 or so. I can't
test this myself, as I have a 32-bit system like you.

Another alternative is to use itertools.count:

from itertools import count
for i in count(0):
if i >= 9000000000: break


Or you can upgrade to Python 3, where range() has no restrictions until
you run out of memory, and can work with ludicrously big numbers:

py> range(10**50)
range(0, 100000000000000000000000000000000000000000000000000)
--
Steve
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
eryksun
2015-08-04 03:20:18 UTC
Permalink
Post by Steven D'Aprano
If you have a 64-bit operating system, you can use a 64-bit version of
Python, and the limit will be something like 2**63 - 1 or so. I can't
test this myself, as I have a 32-bit system like you.
A notable exception to the above claim is 64-bit Windows, which uses a
32-bit C long that limits xrange to 2**31 - 1.
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Peter Otten
2015-08-04 06:17:19 UTC
Permalink
Post by Dima Kulik
I want to make a for loop with a huge numbers.
some_code()
Post by Dima Kulik
but range and xrange cant operate with such big numbers.
Can some on help me?
If you are just beginning with Python and do not require any libraries that
are only available for Python 2 the obvious solution is to switch to Python
3.

But note that if some_code() takes 1 microsecond the loop will run for about
Post by Dima Kulik
9000000000/60/60/24/1000
104.16666666666667

104 days. Did you take that into consideration? What are you trying to
achieve? If you explain what you want to do with your script one of us may
be able to come up with a better way to tackle your problem.

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Peter Otten
2015-08-04 08:28:55 UTC
Permalink
Post by Peter Otten
But note that if some_code() takes 1 microsecond the loop will run for about
Sorry, that should be millisecond (1/1000 s).
Post by Peter Otten
9000000000/60/60/24/1000
104.16666666666667
104 days.
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Continue reading on narkive:
Loading...