1

For any given n , I need the following to happen.

def count(n):
  for i in range(0,5):
    for j in range(0,i+1):
      for k in range(0,j+1):
        .......
          n nested loops 
             count=count+1 
   return count 

I would like to know if there is a more concise way of achieving this above code segment.

the code has to return 5,15,35,70 on input of 1,2,3,4 .

13
  • You wrote that there are n nested loops. This is not the same n as the argument of the function, is it? Commented Nov 23, 2018 at 13:53
  • 1
    Take a look at this Commented Nov 23, 2018 at 13:56
  • syntonym , yes , it the same n passed in the argument Commented Nov 23, 2018 at 13:58
  • raj, I had a look at that. That would return 5,25,125 on input of 1,2,3. What I would like is for it to return 5,15,35 for n=1,2,3 . Thanks. Commented Nov 23, 2018 at 14:01
  • 1
    Please don't reuse the name of your function for a local variable in the function, it makes the code harder to read. Do you have to do this with nested loops? Commented Nov 23, 2018 at 14:20

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.