Created
March 1, 2011 01:39
-
-
Save dsparks/848442 to your computer and use it in GitHub Desktop.
Of triangle numbers and word values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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