In Coded Triangle Numbers, the 42nd problem on Project Euler, we are provided with a 16K text file containing approximately two-thousand English words.

We are asked to associate letters of words with their index in the alphabet, so "A" is 1, "E" is 5, etc., and take the sum of each word. For the sum of each word, \(s\), we need to determine if \(s\) is a triangle number.

Triangle numbers are defined by the formula,

\[ T_n = \frac{1}{2} n ( n + 1)\,.\]```
function coded_trinumbers(buffer)
triangular(n) = div(n^2 + n, 2)
wordsum(word) = [(Int(c) - 64) for c in word] |> sum
current_max_N = 100
trinums = triangular.(1:current_max_N)
trimatches = 0
for word in split(read(buffer, String), ",")
word = replace(word, "\"" => "")
s = wordsum(word)
while s > trinums[end]
trinums = vcat(trinums, triangular.(current_max_N+1:current_max_N+100))
current_max_N += 100
end
if s in trinums
trimatches += 1
end
end
trimatches
end
@time coded_trinumbers(filename)
> 0.124345 seconds (443.99 k allocations: 26.976 MiB, 5.49% gc time, 99.21% compilation time)
>
> 162
```

© Daniel Marvin. Last modified: July 22, 2021.