Find sum of digits in factorial of a number | Function to multiply x with large number stored in vector v . Result is stored in v . ; Calculate res + prev carry ; updation at ith position ; Returns sum of digits in n ! ; One by one multiply i to current vector and update the vector . ; Find sum of digits in vector v [ ] ; Driver code
let v = [ ] ; function multiply ( x ) { let carry = 0 ; let size = v . length ; for ( let i = 0 ; i < size ; i ++ ) { let res = carry + v [ i ] * x ; v [ i ] = res % 10 ; carry = Math . floor ( res / 10 ) ; } while ( carry != 0 ) { v . push ( carry % 10 ) ; carry = Math . floor ( carry / 10 ) ; } } function findSumOfDigits ( n ) { for ( let i = 1 ; i <= n ; i ++ ) multiply ( i ) ; let sum = 0 ; let size = v . length ; for ( let i = 0 ; i < size ; i ++ ) sum += v [ i ] ; return sum ; } let n = 1000 ; document . write ( findSumOfDigits ( n ) ) ;