Given a positive integer n, find the first digit in its factorial.
Input : n = 5 Output : 1 Factorial of 5 is 120 and first digit is 1. Input : 1000 Output : 4
A simple solution is to compute factorial of number, then find first digit in it.
The above solution causes overflow soon. A better solution is to use the fact that factorial contains trailing 0s and removing trailing 0s does not change first digit. For example, first digit of x * y is same as x * y * 100 for x > 0 and y > 0.
The above code also fails for slightly higher values. The best idea seems to be to find factorial of large number and then find first digit.
- Maximum number with same digit factorial product
- Find the last digit when factorial of A divides factorial of B
- Last non-zero digit of a factorial
- Count of Numbers in Range where first digit is equal to last digit of the number
- Find the remainder when First digit of a number is divided by its Last digit
- Largest number less than N with digit sum greater than the digit sum of N
- Factorial of a large number
- C Program for factorial of a number
- Sum of divisors of factorial of a number
- Program for factorial of a number
- Java Program for factorial of a number
- Smallest number S such that N is a factor of S factorial or S!
- One line function for factorial of a number
- Python Program for factorial of a number
- Find sum of digits in factorial of a number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.