Narcissistic decimal number: Difference between revisions
m
→While loop solution: Improved syntax.
ReeceGoding (talk | contribs) m (→{{header|R}}: Syntax highlighting.) |
ReeceGoding (talk | contribs) m (→While loop solution: Improved syntax.) |
||
Line 4,047:
*As we are using format anyway, we take the chance to make the output look nicer.
<lang rsplus>generateArmstrong <- function(howMany)
{
resultCount <- i <- 0
while(resultCount < howMany)
{
#The next line looks terrible, but I know of no better way to convert a large integer in to its digits in R.
digits <- as.integer(unlist(strsplit(format(i, scientific = FALSE), "")))
if(i == sum(digits^(length(digits)))) cat("Armstrong number ", resultCount <- resultCount + 1, ": ", format(i, big.mark = ","), "\n", sep = "")
}
}
|