# Write this function

![](http://i.imgur.com/mlbRlEm.png)

`for i from 1 to n`, do `i % m` and return the `sum`

    f(n=10, m=5) // returns 20 (1+2+3+4+0 + 1+2+3+4+0)

*You'll need to get a little clever with performance, since n can be a very large number*

Difficulty: introductory

Starter code:

def f(n, m):
	

Solutions:
def f(n, m):
    re, c = divmod(n,m) 
    return m*(m-1)/2*re + (c+1)*c/2

def f(n, m):
    # Function to return the sum of the remnainders when n is divided by m for all n from 1 to n
    # The remainders cycle from 0, 1, 2, 3,... m-1.  This happens n // m times, with a final sequnce 
    # up to n % m.  The sum of 0, ... m-1 is m * (m - 1) / 2.  The sum of 0, 1, 2, 3, n % m is similar.
    #
    return (n // m) * (m - 1) * m / 2 + (n % m) * (n % m + 1) / 2

def sumrange(n):
    return (n - 1) * n / 2
def f(n, m):
    n, m = int(n), int(m)
    return (n / m) * sumrange(m) + sumrange(n % m + 1)

def f(n, m):
    # Either n or m seems to have a floating-point representation in one of the official tests.
    # Since it's a .0, converting to int is fine.
    n, m = int(n), int(m)
    
    # n=14, m=5  ->  2 whole runs, plus the 4 first modulos
    whole_runs = n / m
    leftovers = n % m
    
    # n=5, m=5  ->  1+2+3+4+0 == (m-1)/(m-1+1)/2
    per_run = (m - 1) * (m - 1 + 1) / 2
    leftovers_sum = leftovers * (leftovers + 1) / 2
  
    return whole_runs * per_run + leftovers_sum

def f(n, m):
  x,y =  divmod(n,m)
  return x * m * (m - 1) / 2 + y * (y + 1) / 2

def f(n, m):
    q, r = divmod(n, m)
    return q * m * (m - 1) // 2 + r * (r + 1) // 2

def f(n, m):
    k = n // m
    r = n % m
    return (m * (m -1) // 2) * k + r * (r + 1) // 2


def f(n, m):
    times = n // m
    extra = n % m
    sum_mini_series = (m - 1) * (m - 1 + 1) // 2
    sum_extra = (extra) * (extra + 1) // 2
    return sum_mini_series * times + sum_extra

def f(n, m):
    sum_1_to_n = lambda n: (n + 1) * n // 2
    a, b = divmod(n, m)
    return a * sum_1_to_n(m - 1) + sum_1_to_n(b)

def f(n, m):
    return ((m-1)**2+(m-1))//2 * (n//m) + ((n%m)**2+(n%m))//2