0

I have written a script, but I don't know if it's possible to have the total. My script would give me total number per loop, but I want to have the lump sum of everything. For instance, if I input 6, I would expect the result to be 168. if I input 5, I would expect the result to be 105.

Thanks

My script is as follow:

def multiples(n, high):
    i = 0
    res = 0
    while i <= high:
        res = res + (n+i)
        i = i + 1
    print res
    return res

def p(high):
    i = 1
    while i <= high:
        multiples(i, i)
        i = i + 1

p(6)  # Expected Output: 168
2

2 Answers 2

1

A few things,

First off, this is a very old-school type of looping.

i = 0
while i <= some_number:
    do_something
    i = i + 1

The last language I used that required a pattern like that was Basic. Use for loops. A standard for loop (note: NOT Python's, but I'll get to that in a minute) looks like for i=0; i<=some_number; i++ { do_something }. That is:

  1. Initialize i
  2. Conditional for when to keep looping
  3. What to do after each loop block

Python is even more clear. for loops over any iterable, so:

for element in [1,3,5,7,9]:
    ...

Gives you 1, then 3, then 5, then 7, then 9 as element in the loop body. Use this in conjunction with the range built-in to loop N times.

for i in range(high):
    # do something `high` number of times

A naive re-write looks like:

def multiples(k, n):
    res = 0
    for i in range(n):
        res += (k+i)
    print(res)
    return res

def p(n):
    for i in range(n):
        multiples(i, i)

However that doesn't really give you what you want either. What you WANT is to assign the value multiples(i, i) TO something.

def p(n):
    total = 0
    for i in range(n):
        total += multiples(i, i)
    return total

Now we're tracking your grand total inside p, and returning it afterwards.

result = p(6)
print(result)  # does what you want


Of course there's no good reason to break up these two functions. You could just as easily write:

def p(n):
    total = 0
    for i in range(n):
        for j in range(i):
            total += j+i
    return total
Sign up to request clarification or add additional context in comments.

1 Comment

Note that depending on what you're trying to do, I may have introduced an off-by-one error. Your original implementation loops from 1 -> N, while mine loops from 0 -> N-1. This isn't an issue in multiples` but might be in p. Fix this with for i in range(1, n+1) instead of for i in range(n)
0

With the most minimal change, this would allow p(6) to return 168:

def multiples(n, high):
   i = 0
   res = 0
   while i <= high:
      res = res + (n+i)
      i = i + 1
   print res
   return res

def p(high):
   i = 1
   lumpSum = 0
   while i <= high:
      lumpSum += multiples(i, i)
      i = i + 1
   return lumpSum

print p(6)

Alternatively, you can shorten p:

def p(high):
    return sum([multiples(i, i) for i in range(1, high + 1)])

Edit: The entire code can indeed be reduced to this: Adam Smith pointed out in his comment.

p = lambda n: sum(i+j for i in range(n) for j in range(i))

1 Comment

or indeed p = lambda n: sum(i+j for i in range(n) for j in range(i))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.