import math


def compute_probability(keyspace, trials):
    """Compute the probability of a collision.

    Uses an approximation of the full sum for computational speed.
    http://preshing.com/20110504/hash-collision-probabilities/

    :param keyspace: keyspace size
    :param trials: number of trials
    :rtype float: Probability, between 0 and 1

    """
    return 1 - math.e ** (-1 * trials * (trials - 1) / (2 * keyspace))