def factor_p(n, primes, limit=1000000):
    """
    Factor given value using sieve up to a certain limit
    :param n: number to factor
    :param limit: sieve limit
    :return: list of factors and residue
    """
    factors = []
    for prime in primes:
        while n % prime == 0 and n > 2:
            n //= prime
            factors.append(prime)
        if n < 2:
            break
    else:
        if n < limit ** 2:
            factors.append(n)
            n = 1
    return factors, n
