Narcissistic decimal number: Difference between revisions

Content added Content deleted
m (→‎{{header|R}}: Syntax highlighting.)
m (→‎While loop solution: Improved syntax.)
Line 4,047: Line 4,047:
*As we are using format anyway, we take the chance to make the output look nicer.
*As we are using format anyway, we take the chance to make the output look nicer.


<lang rsplus>generateArmstrong<-function(howMany)
<lang rsplus>generateArmstrong <- function(howMany)
{
{
resultCount<-i<-0
resultCount <- i <- 0
while(resultCount<howMany)
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.
#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),"")))
digits <- as.integer(unlist(strsplit(format(i, scientific = FALSE), "")))
if(i==sum(digits^(length(digits))))
if(i == sum(digits^(length(digits)))) cat("Armstrong number ", resultCount <- resultCount + 1, ": ", format(i, big.mark = ","), "\n", sep = "")
{
i <- i + 1
cat("Armstrong number ",resultCount<-resultCount+1,": ",format(i, big.mark = ","),"\n",sep="")
}
i<-i+1
}
}
}
}