def factor(n):
    """
        find the prime factors of n along with their frequencies. Example:

        >>> factor(786456)
        [(2,3), (3,3), (11,1), (331,1)]

        Source: Project Euler forums for problem #3
    """
    f, factors, prime_gaps = 1, [], [2, 4, 2, 4, 6, 2, 6, 4]
    if n < 1:
        return []
    while True:
        for gap in ([1, 1, 2, 2, 4] if f < 11 else prime_gaps):
            f += gap
            if f * f > n:
                if n == 1:
                    return factors
                else:
                    return factors + [(n, 1)]
            if not n % f:
                e = 1
                n //= f
                while not n % f:
                    n //= f
                    e += 1
                factors.append((f, e))
