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
< ? php function multiply ( & $ v , $ x ) { $ carry = 0 ; $ size = count ( $ v ) ; for ( $ i = 0 ; $ i < $ size ; $ i ++ ) { $ res = $ carry + $ v [ $ i ] * $ x ; $ v [ $ i ] = $ res % 10 ; $ carry = ( int ) ( $ res / 10 ) ; } while ( $ carry != 0 ) { array_push ( $ v , $ carry % 10 ) ; $ carry = ( int ) ( $ carry / 10 ) ; } } function findSumOfDigits ( $ n ) { for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) multiply ( $ v , $ i ) ; $ sum = 0 ; $ size = count ( $ v ) ; for ( $ i = 0 ; $ i < $ size ; $ i ++ ) $ sum += $ v [ $ i ] ; return $ sum ; } $ n = 1000 ; print ( findSumOfDigits ( $ n ) ) ; ? >