First we read in the words:
In [1]:
with open("txt/0042_words.txt") as f:
words = {w.strip('"') for w in f.read().split(",")}
We'll also write a simple function for testing if a word is a "triangle word". We'll make use of SageMath's built-in function for testing if a number is triangular.
In [2]:
def is_triangle_word(word):
value = sum(ord(c) - ord('A') + 1 for c in word)
return is_triangular_number(value)
(It's not difficult to apply the quadratic formula to write our own test for triangular numbers.)
Now we just test each word.
In [3]:
triangle_words = {w for w in words if is_triangle_word(w)}
len(triangle_words)
Out[3]:
162
Copyright (C) 2025 filifa¶
This work is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license and the BSD Zero Clause license.