From Leonard Cohen comes a puzzle at the intersection of language and mathematics:
In Jewish study, “Gematria” is an alphanumeric code where words are assigned numerical values based on their letters. We can do the same in English, assigning 1 to the letter A, 2 to the letter B, and so on, up to 26 for the letter Z. The value of a word is then the sum of the values of its letters. For example, RIDDLER has an alphanumeric value of 70, since R + I + D + D + L + E + R becomes 18 + 9 + 4 + 4 + 12 + 5 + 18 = 70.
But what about the values of different numbers themselves, spelled out as words? The number 1 (ONE) has an alphanumeric value of 15 + 14 + 5 = 34, and 2 (TWO) has an alphanumeric value of 20 + 23 + 15 = 58. Both of these values are bigger than the numbers themselves.
Meanwhile, if we look at larger numbers, 1,417 (ONE THOUSAND FOUR HUNDRED SEVENTEEN) has an alphanumeric value of 379, while 3,140,275 (THREE MILLION ONE HUNDRED FORTY THOUSAND TWO HUNDRED SEVENTY FIVE) has an alphanumeric value of 718. These values are much smaller than the numbers themselves.
If we consider all the whole numbers that are less than their alphanumeric value, what is the largest of these numbers?
To begin to solve this puzzle, the key was to transform any number into its spelled form. So I started the search for a package that already did this, such package is english. For my solution I will use this and tidyverse.
First, the gematria function is defined.
gematria <- function(x){
str_to_lower(x) %>%
str_split(pattern = '') %>%
unlist() %>%
sapply(function(y){which(letters == y)}) %>%
sum()
}## [1] 70
Now it is time to try the library.
## [1] "one"
## [2] "two"
## [3] "one thousand four hundred and seventeen"
## [4] "three million one hundred and forty thousand two hundred and seventy-five"
For some numbers there are characters that we do not want to contemplate in the transformation, so we will take it into account when creating the function that returns the alphanumeric value.
## [1] 34 58 379 718
Everything is correct.
Now, for the purpose of the riddle the following filter function is defined.
The first idea to find the integer value that answers the question, is to iterate over a certain interval, in this case from 0 to 1000.
## [1] 279
Despite being just a test to evaluate our functions, it seems that we have reached a promising result, however it would be an error to affirm that the real answer is in this subset, because we have not yet reviewed what happens in the following values.
However, iterating over thousands of values would be time consuming and inefficient. For this reason, a better way to analyze subsequent sets of numbers would be to analyze them in a graph.
As you can see, the identity function grows faster than the one that gives the alphanumeric value, so there is no value greater than 279 that meets the condition of the problem.
A work by Jacob Hernández Mejía
jacobhdezm@outlook.com