Ejemplo n.º 1
0
def main(verbose=False):
    non_squares = [num for num in range(1, 100 + 1)
                   if not is_power(num, 2)]
    running_sum = 0
    for n in non_squares:
        running_sum += sum(stable_expansion(100, n))
    return running_sum
Ejemplo n.º 2
0
def is_possible(n, primes):
    if n % 2 == 0 or n in primes:
        raise Error("Value poorly specified")

    primes_less = [prime for prime in primes if prime < n and prime != 2]
    for prime in primes_less:
        if is_power((n - prime) / 2.0, 2):
            return True
    return False
Ejemplo n.º 3
0
def main(verbose=False):
    TARGET = 10**6
    M = 1
    solutions = 0
    while solutions < TARGET:
        M += 1
        # need a + b with (a + b)**2 + M**2
        for inferior_sum in xrange(2, 2*M + 1):
            if is_power((inferior_sum)**2 + M**2, 2):
                solutions += unique_pairs(inferior_sum, M)
    return M
Ejemplo n.º 4
0
def all_values_on_form(form, value):
    """
    Returns all lattice points (not necessarily coprime)
    that produce the desired value on the form

    Given the recurrence for the form, these values
    can serve to determine *all* solutions for
    the given value due to the repeating nature
    of the infinite river
    """
    factor_list = factors(value)
    valid_factors = [factor for factor in factor_list
                     if is_power(value/factor, 2)]

    roots = all_positive_roots(form)
    found = set()
    for root in roots:
        candidates = seek_up_to_val(root, value)
        to_add = [candidate for candidate in candidates
                  if candidate[1] in valid_factors] + \
                 [candidate for candidate in root
                  if candidate[1] in valid_factors]
        found.update(to_add)
    found = list(found)

    # We may get some duplicates from since when we include
    # values from the river, we don't check that they come from
    # a different iteration of the river
    x_mult, y_mult, _ = get_recurrence(form)
    checked = found[:]
    for candidate in found:
        coords, val = candidate
        next_x = sum(operator.mul(*pair) for pair in zip(coords, x_mult))
        next_y = sum(operator.mul(*pair) for pair in zip(coords, y_mult))
        if ((next_x, next_y), val) in found:
            checked.remove(((next_x, next_y), val))

    # Finally we must scale up factors to account for
    # the reduction by a square multiple
    result = []
    for cell in checked:
        (x, y), val = cell
        if val < value:
            ratio = int(sqrt(value/val))
            x *= ratio
            y *= ratio
        result.append((x,y))

    return result
Ejemplo n.º 5
0
def main(verbose=False):
    non_squares = [num for num in range(1, 10000 + 1)
                   if not is_power(num, 2)]
    cycle_lengths = [len(continued_fraction_cycle(num)) - 1
                     for num in non_squares]
    return len([num for num in cycle_lengths if num % 2 == 1])