Euler problem 22 is another trivial one that takes us to the realm of ASCII codes. ASCII is a method to convert symbols into numbers, originally invented for telegraphs. Back in the 8-bit days, ASCII art was a method to create images without using lots of memory. Each image consists of a collection of text characters that give the illusion of an image. Euler problem 22 is, unfortunately, a bit less poetic. This problem is similar to Euler problem 17 that asks to count the length of words expressing numbers.

## Euler Problem 22 Definition

Using names.txt, a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.

For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938^{th} name in the list. So, COLIN would obtain a score of 938 × 53 = 49,714.

What is the total of all the name scores in the file?

## Solution

This code reads and cleans the file and sorts the names alphabetically. The charToRaw function determines the numerical value of each character in each name. This output of this function is the hex ASCII code for each character. The letter A is number 65, so we subtract 64 from each value to get the sum total.

names <- readLines("https://projecteuler.net/project/resources/p022_names.txt", warn =FALSE) names <- unlist(strsplit(names, ",")) names <- gsub("[[:punct:]]", "", names) names <- sort(names) # Total Name scores answer <- 0 for (i in names) { value <- sum(sapply(unlist(strsplit(i, "")), function(x) as.numeric(charToRaw(x)) - 64)) value <- value * which(names==i) answer <- answer + value } print(answer)

We can have a bit more fun with this problem by comparing this list with the most popular baby names in 2016. The first section of the code extracts the list of popular names from the website. The rest of the code counts the number of matches between the lists and shows the popular baby names not present in the Project Euler list.

library(rvest) url <- "https://www.babycenter.com/top-baby-names-2016.htm" babynames <- url %>% read_html() %>% html_nodes(xpath = '//*[@id="babyNameList"]/table') %>% html_table() babynames <- babynames[[1]] # Convert Project Euler list and test for matches proper <- function(x) paste0(toupper(substr(x, 1, 1)), tolower(substring(x, 2))) names <- proper(names) sum(babynames$GIRLS %in% names) sum(babynames$BOYS %in% names) babynames$GIRLS[!(babynames$GIRLS %in% names)] babynames$BOYS[!(babynames$BOYS %in% names)]

View the latest version of this code on GitHub.

## 2 thoughts on “Euler Problem 22 : Names Scores”