I am completing a programming question on project euler, the question is :
n! means n × (n − 1) × ... × 3 × 2 × 1
For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800, and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
Find the sum of the digits in the number 100!.
The code I am using in R is :
options(scipen = 999)
x <- 100
y <- 1
while (x>=1){
y <- y * x
x <- x - 1
}
sum(as.numeric(strsplit(as.character(y),"")[[1]]))
The scipen is to force my integer to not print as an exponent, so that i can print it as a character, spit the integers and then change it back to a numeric and sum the components. This method all works fine when i test for 10!, but gives an incorrect answer for 100! I this there may be an issue with printing the correct number when i force it not to be an exponent, does anybody know what i'm doing wrong.