0

I have a number like 9970024 that comes from a total number of 249250621 and I would like to divide the 9970024 number into segments of equal range. That is, 0-9970024 is one iteration that I want to do and then next 9970024 +1 to 19940049 (from 9970025+9970024) until it reaches to the total number which is 249250621. How can I do such thing using python.

My initial try is as follows:

j = 0
x = 9970024
total = 249250621
while (j <= total):
 for i in range(j,x):
  j = j +1
 j = i+1
 x = x+j
Yu Hao
  • 119,891
  • 44
  • 235
  • 294

3 Answers3

1

Let's use some smaller numbers for clarity.

total = 25
fragment = 10
for start in xrange(0, total, fragment):
    print "start = ", start
    for i in range(start, min([start + fragment, total])):
        print i

Output:

start = 0
0
1
2
3
4
5
6
7
8
9
start = 10
10
11
12
13
14
15
16
17
18
19
start = 20
20
21
22
23
24
Falko
  • 17,076
  • 13
  • 60
  • 105
  • 3
    I would recommend the use of `xrange` if Python 2.x is being used, especially as large numbers are involved. [xrange vs range](http://stackoverflow.com/questions/94935/what-is-the-difference-between-range-and-xrange-functions-in-python-2-x) – Martin Evans Jun 30 '15 at 07:10
1

An alternative way of looking at this could be to simply iterate the whole range but determine when a segment boundary is reached.

total = 249250621       
segment = total / 25        # 9970024 

for j in xrange(0, total+1):
    if j % segment == 0:
        print "%u - start of segment" % j
Martin Evans
  • 45,791
  • 17
  • 81
  • 97
0
for i in range(0,249250621,9970024):
    for j in range(i,i+9970024+1):
        pass

You can try this.Here i will increase by 9970024 everytime.And j will loop for 9970024 times.

vks
  • 67,027
  • 10
  • 91
  • 124