def zero_absent(relation, initial_values, modulus): initial = [value % modulus for value in initial_values] curr = initial[:] if 0 in initial: return False curr = [value % modulus for value in recurrence_next(relation, curr)] while curr != initial: if 0 in curr: return False curr = [value % modulus for value in recurrence_next(relation, curr)] return True
def main(verbose=False): relation = [1, 2] h_values = [1, 3] k_values = [1, 2] count = 0 for i in range(2, 1000 + 1): h_values = recurrence_next(relation, h_values) k_values = recurrence_next(relation, k_values) if len(str(h_values[1])) > len(str(k_values[1])): count += 1 return count
def solutions(limit): # We seek x_k^2 - 3y_k^2 = 4 # Where 3*n - sign = x_k x_mult, y_mult, relation = get_recurrence([1, -3]) starting_points = all_values_on_form([1, -3], 4) series = [start_to_series(initial, x_mult, 'x') for initial in starting_points] result = [pair[0] for pair in series if pair[0] % 3 != 0 and pair[0] > 0] while max(result) < 2 * limit: next = [pair[1] for pair in series if pair[1] % 3 != 0 and pair[1] > 0] result.extend(next) series = [recurrence_next(relation, values) for values in series] # We seek perimeters n + n + (n + sign) = 3n + sign # We currently have 3n - sign, so if we can determine the sign # If value == 1 mod 3, then 3n - sign == 1, hence sign = -1 # and 3n + sign = 3n - 1 = value - 2 # If value == -1 mod 3, then 3n - sign == -1, hence sign = 1 # and 3n + sign = 3n + 1 = value + 2 result = sorted(((value + 2) if value % 3 == 2 else (value - 2)) for value in result) # The first two solutions are 1-1-0 and 1-1-2, which are both degenerate return [perimeter for perimeter in result if perimeter < limit and perimeter not in (2, 4)]
def main(verbose=False): a, b = 0, 2 running_sum = 0 while b <= 4000000: running_sum += b a, b = recurrence_next([1, 4], [a, b]) return running_sum
def main(verbose=False): # we have h_(-1) = 1, k_(-1) = 0 # h_0/k_0 = 2 h_values = [1, 2] k_values = [0, 1] # The problem wants the 100th convergent which will # be h_99/k_99. To get to this, we need the first 99 # values of a a = reduce(operator.add, [[1,2*k,1] for k in range(1, 33 + 1)]) for a_i in a: relation = [1, a_i] h_values = recurrence_next(relation, h_values) k_values = recurrence_next(relation, k_values) h_99 = h_values[1] k_99 = k_values[1] reduced = h_99/(gcd(h_99, k_99)) return sum(int(dig) for dig in str(reduced))
def solutions(limit): # We seek 5x_k^2 - y_k^2 = 1 # Where L = x_k x_mult, y_mult, relation = get_recurrence([5, -1]) starting_points = all_values_on_form([5, -1], 1) series = [start_to_series(initial, x_mult, 'x') for initial in starting_points] result = [pair[0] for pair in series if pair[0] > 1] while len(result) < 2*limit: next = [pair[1] for pair in series if pair[1] > 1] result.extend(next) series = [recurrence_next(relation, values) for values in series] return sorted(result)[:limit]
def stable_expansion(digits, n): values = continued_fraction_cycle(n) h_values = [1, values[0]] # a_0 k_values = [0, 1] cycle_length = len(values) - 1 # we only cycle over a_1,...,a_{k-1} last = expanded_digits(h_values[1], k_values[1], digits) index = 1 relation = [1, values[index]] h_values = recurrence_next(relation, h_values) k_values = recurrence_next(relation, k_values) current = expanded_digits(h_values[1], k_values[1], digits) while current != last: index += 1 last = current relative_index = ((index - 1) % cycle_length) + 1 # we want residues 1,..,k-1 instead of the traditional 0,...,k-2 relation = [1, values[relative_index]] h_values = recurrence_next(relation, h_values) k_values = recurrence_next(relation, k_values) current = expanded_digits(h_values[1], k_values[1], digits) return current
def golden_nuggets(limit): # We seek 5x_k^2 - y_k^2 = 4 # Where 5n + 1 = y_k x_mult, y_mult, relation = get_recurrence([5, -1]) starting_points = all_values_on_form([5, -1], 4) series = [start_to_series(initial, y_mult, 'y') for initial in starting_points] nuggets = [pair[0] for pair in series if pair[0] % 5 == 1 and pair[0] > 1] while len(nuggets) < 2 * limit: next = [pair[1] for pair in series if pair[1] % 5 == 1 and pair[1] > 1] nuggets.extend(next) series = [recurrence_next(relation, values) for values in series] return sorted([(value - 1) / 5 for value in nuggets])[:limit]
def main(verbose=False): # y = 2T - 1, T > 10**12 implies the following: LOWER_LIMIT = 2 * (10 ** 12) - 1 # We seek 2x^2 - y^2 = 1 x_mult, y_mult, relation = get_recurrence([2, -1]) starting_points = all_values_on_form([2, -1], 1) series = [start_to_series(initial, y_mult, 'y') for initial in starting_points] result = [pair[0] for pair in series] while max(result) <= LOWER_LIMIT: result.extend([pair[1] for pair in series]) series = [recurrence_next(relation, values) for values in series] min_y = min(y for y in result if y > LOWER_LIMIT) min_x = sqrt((1 + min_y ** 2) / 2) return int((min_x + 1) / 2)