Skip to content

Instantly share code, notes, and snippets.

@dsparks
Created March 1, 2011 01:39
Show Gist options
  • Save dsparks/848442 to your computer and use it in GitHub Desktop.
Save dsparks/848442 to your computer and use it in GitHub Desktop.
Of triangle numbers and word values
rm(list = ls())
# from http://projecteuler.net/index.php?section=problems&id=42
Text <- readLines("http://projecteuler.net/project/words.txt")
TextString <- gsub("\"", "", unlist(strsplit(Text, "\",\"")))
TriangleNumbers <- cumsum(1:100)
StringLetters <- strsplit(TextString, "")
LetterRank <- rank(LETTERS)
names(LetterRank) <- LETTERS
WordValues <- unlist(lapply(StringLetters, function(x){sum(LetterRank[x])}))
names(WordValues) <- TextString
sort(WordValues)
IsTriangleWord <- is.element(WordValues, TriangleNumbers)
table(IsTriangleWord)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment