Given a number n, write code to find the sum of digits in the factorial of the number. Given n <= 5000
Input : 10 Output : 27 Input : 100 Output : 648
It is not possible to store a number as large as 100! under some data types so, idea is to store extremely large number in vector.
1) Create a vector to store factorial digits and initialize it with 1. 2) One by one multiply numbers from 1 to n to the vector. We use school mathematics for this purpose. 3) Sum all the elements in vector and return the sum.
This article is contributed by Sakshi Tiwari . If you like GeeksforGeeks(We know you do!) and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find the last digit when factorial of A divides factorial of B
- Find the last two digits of Factorial of a given Number
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Find the Largest number with given number of digits and sum of digits
- Find smallest number with given number of digits and sum of digits under given constraints
- Smallest number with at least n digits in factorial
- Number of digits in N factorial to the power N
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Minimum digits to be removed to make either all digits or alternating digits same
- Count digits in a factorial | Set 2
- Count digits in a factorial | Set 1
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Check if the sum of digits of number is divisible by all of its digits
- Sum of the digits of square of the given number which has only 1's as its digits
- Find the average of k digits from the beginning and l digits from the end of the given number
- Sum of divisors of factorial of a number
- Check if a subarray of length K with sum equal to factorial of a number exists or not