# 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*

Source: codewars | Difficulty: EASY
Tags: Mathematics

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):
	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):
	(n, m) = (int(n), int(m))
	whole_runs = n / m
	leftovers = n % m
	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)