def four_digit_figurate(size): for n in figurate_numbers(size): if n < 1000: continue if n >= 10000: break yield n
def search_pentagonal_sum_difference(): # Find pentagonal numbers a and b such that a + b and a - b are also pentagonal. # Treat c = a + b as fundamental, and derive b and a - b from it # b = c - a # (a - b) = a - (c - a) = 2 * a - c seen = set() seen_list = [] for c in figurate_numbers(5): for a in seen_list: b = c - a a_minus_b = 2 * a - c #assert a_minus_b == a - b #assert a + b == c if b in seen and a_minus_b in seen: return a_minus_b seen.add(c) seen_list.append(c)
from utility import up_to, figurate_numbers def letter_value(c): """ >>> [letter_value(c) for c in 'abcdefghijklmnopqrstuvwxyz'] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26] """ c = c.lower() return ord(c) - ord('a') + 1 def word_value(word): """ >>> word_value('sky') 55 """ return sum(letter_value(c) for c in word) with open('data/words.txt') as f: word_data = f.read() words = [w.strip('"').lower() for w in word_data.split(',')] t_nums = set(up_to(30 * 26, figurate_numbers(3))) print(sum(1 for w in words if word_value(w) in t_nums))
def tri_pent_hex_numbers(): for n in figurate_numbers(6): if is_triangular_number(n) and is_pentagonal_number(n): yield n