Пример #1
0
def all_base10_base2_palindromes(n):
    result = []
    base_10 = 1
    base_2 = '1'

    while base_10 < n:
        if is_palindrome(base_10) and is_palindrome(base_2):
            result.append(base_10)
        base_10 += 1
        base_2 = binary_incrementer(base_2)
    return result
Пример #2
0
def palindromic_square_sums(n):
    # first populate all pairs that add to less than n
    # 2k**2 < k**2 + (k + 1)**2 < n
    MAX_k = int(round(sqrt(n / 2.0)))
    curr = [index ** 2 + (index + 1) ** 2 for index in range(1, MAX_k)]
    curr = [num for num in curr if num < n]

    result = [num for num in curr if is_palindrome(num)]
    num_squares = 2
    while curr:
        num_squares += 1
        curr = [curr[i] + (i + num_squares) ** 2 for i in range(len(curr))]
        curr = [num for num in curr if num < n]
        result.extend([num for num in curr if is_palindrome(num)])

    return set(result)
Пример #3
0
def main(verbose=False):
    lychrel_sequences = {}
    for i in range(1, 10000):
        update_hash(i, 50, lychrel_sequences)

    # We begin with the inital assumption that every number
    # is a Lychrel number, and reduce the count every time
    # we encounter a number which is not
    count = 9999
    for key, value in lychrel_sequences.items():
        if 50 in value:
            iterations = 50
        else:
            iterations = max(value)

        if is_palindrome(value[iterations]):
            count -= 1

    return count
Пример #4
0
def update_hash(n, max_iterations, hash_={}):
    """
    Uses the hash values and continually updates
    the sequence until a palindrome is found or until
    the number of iterations exceeds max_iterations
    """
    curr = next_lychrel_value(n)
    to_add = {0: n, 1: curr}
    index = 1
    while not is_palindrome(curr) and index <= max_iterations:
        if curr in hash_:
            covered = hash_[curr].copy()
            for i in range(1, max(covered) + 1):
                to_add[index + i] = covered[i]
            index += max(covered)
        else:
            curr = next_lychrel_value(curr)
            index += 1
            to_add[index] = curr
    hash_[n] = to_add
    return to_add
Пример #5
0
def main(verbose=False):
    products = apply_to_list(operator.mul, range(100, 1000))
    return max(elt for elt in products if is_palindrome(elt))