A number F is a factorial number if there exists some integer I >= 0 such that F = I! (that is, F is factorial of I). Examples of factorial numbers are 1, 2, 6, 24, 120, ….
Write a program that takes as input two long integers ‘low’ and ‘high’ where 0 < low < high and finds count of factorial numbers in the closed interval [low, high].
Input: 0 1 Output: 1 //Reason: Only factorial number is 1 Input: 12 122 Output: 2 // Reason: factorial numbers are 24, 120 Input: 2 720 Output: 5 // Factorial numbers are: 2, 6, 24, 120, 720
1) Find the first factorial that is greater than or equal to low. Let this factorial be x! (factorial of x) and value of this factorial be ‘fact’
2) Keep incrementing x, and keep updating ‘fact’ while fact is smaller than or equal to high. Count the number of times, this loop runs.
3) Return the count computed in step 2.
Below is implementation of above algorithm. Thanks to Kartik for suggesting below solution.
Count is 5
This article is contributed by Shivam. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Count of numbers having only 1 set bit in the range [0, n]
- Count Odd and Even numbers in a range from L to R
- Count numbers in range 1 to N which are divisible by X but not by Y
- Count of numbers from range [L, R] whose sum of digits is Y
- Count the numbers divisible by 'M' in a given range
- Count of common multiples of two numbers in a range
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3
- Count numbers in range L-R that are divisible by all of its non-zero digits
- Count numbers from range whose prime factors are only 2 and 3
- Count all the numbers in a range with smallest factor as K
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Count numbers with unit digit k in given range
- Numbers in range [L, R] such that the count of their divisors is both even and prime
- Count of Numbers in a Range where digit d occurs exactly K times
- Program to find count of numbers having odd number of divisors in given range