0

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.

Jaap
  • 81,064
  • 34
  • 182
  • 193
  • 2
    R is **not** an arbitrary precission calculator! http://stackoverflow.com/questions/24847918/floating-point-precision-in-r – jogo Oct 30 '16 at 11:24
  • 1
    I solved this problem in JavaScript and IIRC 100! had around 300 digits. I don't know if R can handle large integers, but there is a way (in JS, Python and also R). I will not spoil you the thrill of discovering how. – R. Schifini Oct 30 '16 at 12:32

0 Answers0