A Number is called Armstrong Number if the sum of the cube of each digit of that number is equals to the Number itself.
Example:
153 = 1 + 5^
A quick and dirty solution using ?strsplit
:
armstrong <- function(x) {
tmp <- strsplit(as.character(x), split="")
cubic <- sapply(tmp, function(y)sum(as.numeric(y)^3))
return(cubic == x)
}
E.g.:
armstrong(c(153, 142))
# [1] TRUE FALSE
# find all 3 digit numbers:
s <- 100:999
s[armstrong(s)]
# [1] 153 370 371 407
# @CarlWitthoft: wikipedia was right ;)