import math


def turan(n, r):
    """
    Calculates the number of edges in a Turan graph
    https://en.wikipedia.org/wiki/Tur%C3%A1n_graph
    :param n: vertex (int)
    :param r: max clique (int)
    :return: number of edges
    :rtype: int
    """
    mod = n % r
    upper = math.ceil(n / r)  # ⌈n/r⌉
    lower = math.floor(n / r)  # ⌊n/r⌋
    return (n * n - mod * upper * upper - (r - mod) * lower * lower) // 2