Problem 42 - Project Euler
テキストファイルの最後に改行いれないとうまくいかない。
あと文字列ベクトルとして最初から読み込む方法が分からない。
words <- read.table("words.txt", sep=",") scores <- numeric(length(words)) n <- 1:26 for(i in 1:length(words)){ char <- unlist(strsplit(as.character(words[[i]]), "")) s <- 0 for(j in 1:length(char)){ s <- s + n[LETTERS==char[j]] } scores[i] <- s } is.triangle <- function(x){ n <- floor(sqrt(2*x)) 2*x==n*(n+1) } count <- 0 for(i in 1:length(scores)){ if(is.triangle(scores[i])) count <- count+1 }