Coded Triangle Numbers

The $n$th term of the sequence of triangle numbers is given by, $t_n = \frac12n(n+1)$; so the first ten triangle numbers are:
$$1, 3, 6, 10, 15, 21, 28, 36, 45, 55, \dots$$
By converting each letter in a word to a number corresponding to its alphabetical position and adding these values we form a word value. For example, the word value for SKY is $19 + 11 + 25 = 55 = t_{10}$. If the word value is a triangle number then we shall call the word a triangle word.
Using words.txt (right click and ‘Save Link/Target As…’), a 16K text file containing nearly two-thousand common English words, how many are triangle words?

This problem requires one to integrate the use of mathematics and programming (Python, for example) to read the text file, calculate the word values and then determine if they are triangle words. Let’s break down the steps:

First, it’s crucial to write a function to check if a number is a triangle number. We know a triangle number, $t_n = \frac12n(n+1)$. Solving the quadratic formula for n in terms of $t_n$:
$n = \frac{\sqrt{8*t_n + 1} – 1}{2}$

Only integral n are valid, so we need to check no floating point remains after calculating $n$.

Here is a Python function that checks if a number is a triangle number:

“`python
def is_triangle_number(x):
n = ((8 * x + 1) ** 0.5 – 1) / 2
return n.is_integer()
“`

Next, you need to read the file words.txt and convert the words into numerical values. To do this, we note that ‘A’ is 1, ‘B’ is 2, …, ‘Z’ is 26. The Python programming language can convert letters to their ASCII values using the `ord` function, but we need to subtract 64 because ‘A’ corresponds to 65 in ASCII.

“`python
triangle_word_count = 0
with open(‘words.txt’, ‘r’) as f:
words = f.read().replace(‘”‘, ”).split(‘,’)

for word in words:
word_value = sum(ord(char) – 64 for char in word)

if is_triangle_number(word_value):
triangle_word_count += 1

print(triangle_word_count)
“`

The program reads the text files, then it calculates the word values for each term, checks if each term is a triangle number and finally, it counts how many total triangle words there are in the list.

More Answers:
Integer Right Triangles
Champernowne’s Constant
Pandigital Prime

Error 403 The request cannot be completed because you have exceeded your quota. : quotaExceeded

Share:

Recent Posts