I am trying to sum all the numbers up to a range, with all the numbers up to the same range.
I am using python:
limit = 10
sums = []
for x in range(1,limit+1):
for y in range(1,limit+1):
sums.append(x+y)
This works just fine, however, because of the nested loops, if the limit is too big it will take a lot of time to compute the sums.
Is there any way of doing this without a nested loop?
(This is just a simplification of something that I need to do to solve a ProjectEuler problem. It involves obtaining the sum of all abundant numbers.)
This still performs just as many calculations but will do it about twice as fast as a for loop.
This avoids a lot of duplicate sums. I don’t know if you want to keep track of those or not.
If you just want every sum with no representation of how you got it then
xrange(2*n + 2)gives you what you want with no duplicates or looping at all.
In response to question: