Пример #1
0
def generate_min_ps(max_k):
    # First, we only need to consider values twice that of k
    limit = 2 * max_k
    # We also only need to use primes that are <= limit
    prime_set = primes.generate_primes(limit)
    # Now generate the set of all products using those values.
    start_psr = MinPSumRecord([], 1, 0)
    prods = dict()
    # The results are returned through the history dictionary "prods"
    generate_prods(prime_set, limit, prods, start_psr)
    # So now go through the values and generate the min prod sums
    min_psn = dict()
    for psr in prods.values():
        # The value is just the product
        val = psr.product
        # For the size of the set, we need to add 1s to make up the difference
        # based on the difference between the product and sum
        set_size = len(psr.factors) + (psr.product - psr.sum)
        # Must be composed of at least two numbers
        if set_size < 2 or set_size > max_k:
            continue
        # If we haven't seen this set size or the value is smaller, update
        if set_size not in min_psn or val < min_psn[set_size]:
            # print set_size, val, psr
            min_psn[set_size] = val
    # We have the result
    return min_psn
Пример #2
0
def compute_prime_mods(xs):
    # All primes less than the max value are relevant.
    ps = generate_primes(max(xs))
    mods = defaultdict(set)
    for p in ps:
        # Compute all the xs mod p.
        xsp = set(x % p for x in xs)
        # A value can be added if does not become 0 mod p for some x.
        for i in xrange(p):
            if (p - i * i) % p not in xsp:
                mods[p].add(i)
    return mods
Пример #3
0
def solve_p187(limit, verbose=True):
    if verbose:
        print "Generating primes..."
    primes = list(generate_primes(limit / 2))
    if verbose:
        print "Computing value..."
    hi = len(primes)
    result = 0
    i = 0
    while hi > 0 and i < len(primes):
        hi = bisect_right(primes, limit / primes[i], lo=i, hi=hi)
        result += hi - i
        i += 1
    return result
Пример #4
0
def gen_composite(limit):
    result = dict()
    result[1] = defaultdict(int)
    ps = list(generate_primes(limit / 2))

    def rec_composite(n, ps, fact):
        p = ps[0]
        curr = n * p
        i = 1
        while curr <= limit:
            fact[curr] = fact[n].copy()
            fact[curr][p] = i
            if len(ps) > 1:
                for j in xrange(len(ps) - 1):
                    if curr * ps[j + 1] > limit:
                        break
                    rec_composite(curr, ps[j + 1:], fact)
            i += 1
            curr *= p

    for i in xrange(len(ps)):
        rec_composite(1, ps[i:], result)
    return result
Пример #5
0
def solve_p243(plimit, ratio):
    # First compute the primorial which has resilience less than the ratio.
    ps = list(generate_primes(plimit))
    totient = 1
    prod = 2
    index = 1
    last_p = 2
    while index < len(ps) and totient/(prod-1.0) >= ratio:
        totient *= ps[index] - 1
        prod *= ps[index]
        last_p = ps[index]
        index += 1
    # Now compare the primorial value against multiples of the previous
    # primorial which are also less than the ratio.
    best = prod
    start_totient = totient/(last_p - 1)
    start_prod = prod/(last_p)
    for i in xrange(2, last_p):
        mul_fact = factor_int_dict(i)
        new_totient = reduce(lambda a, p: a * p**mul_fact[p], mul_fact, start_totient)
        new_prod = start_prod * i
        if new_totient/(new_prod - 1.0) < ratio and new_prod < best:
            best = new_prod
    return best    
Пример #6
0
def solve_p133(limit, test_limit=None):
    result = 0
    for p in generate_primes(limit):
        if not test_repunit_factor(p, test_limit):
            result += p
    return result
Пример #7
0
def solve_p110(target, prime_limit, exp_limit):
    ps = list(generate_primes(prime_limit))
    best_fact = find_min_target(target, ps, dict(), exp_limit)
    return prod_fact(best_fact)