# Count factorial numbers in a given range

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].

**Examples : **

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.

## C++

`// Program to count factorial numbers in given range ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `int` `countFact(` `int` `low, ` `int` `high) ` `{ ` ` ` `// Find the first factorial number 'fact' greater than or ` ` ` `// equal to 'low' ` ` ` `int` `fact = 1, x = 1; ` ` ` `while` `(fact < low) ` ` ` `{ ` ` ` `fact = fact*x; ` ` ` `x++; ` ` ` `} ` ` ` ` ` `// Count factorial numbers in range [low, high] ` ` ` `int` `res = 0; ` ` ` `while` `(fact <= high) ` ` ` `{ ` ` ` `res++; ` ` ` `fact = fact*x; ` ` ` `x++; ` ` ` `} ` ` ` ` ` `// Return the count ` ` ` `return` `res; ` `} ` ` ` `// Driver program to test above function ` `int` `main() ` `{ ` ` ` `cout << ` `"Count is "` `<< countFact(2, 720); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Program to count factorial ` `// numbers in given range ` ` ` `class` `GFG ` `{ ` ` ` `static` `int` `countFact(` `int` `low, ` `int` `high) ` ` ` `{ ` ` ` `// Find the first factorial number ` ` ` `// 'fact' greater than or equal to 'low' ` ` ` `int` `fact = ` `1` `, x = ` `1` `; ` ` ` `while` `(fact < low) ` ` ` `{ ` ` ` `fact = fact * x; ` ` ` `x++; ` ` ` `} ` ` ` ` ` `// Count factorial numbers ` ` ` `// in range [low, high] ` ` ` `int` `res = ` `0` `; ` ` ` `while` `(fact <= high) ` ` ` `{ ` ` ` `res++; ` ` ` `fact = fact * x; ` ` ` `x++; ` ` ` `} ` ` ` ` ` `// Return the count ` ` ` `return` `res; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `System.out.print(` `"Count is "` ` ` `+ countFact(` `2` `, ` `720` `)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python3

`# Program to count factorial ` `# numbers in given range ` ` ` `def` `countFact(low,high): ` ` ` ` ` `# Find the first factorial number ` ` ` `# 'fact' greater than or ` ` ` `# equal to 'low' ` ` ` `fact ` `=` `1` ` ` `x ` `=` `1` ` ` `while` `(fact < low): ` ` ` ` ` `fact ` `=` `fact ` `*` `x ` ` ` `x ` `+` `=` `1` ` ` ` ` ` ` `# Count factorial numbers ` ` ` `# in range [low, high] ` ` ` `res ` `=` `0` ` ` `while` `(fact <` `=` `high): ` ` ` ` ` `res ` `+` `=` `1` ` ` `fact ` `=` `fact ` `*` `x ` ` ` `x ` `+` `=` `1` ` ` ` ` ` ` `# Return the count ` ` ` `return` `res ` ` ` `# Driver code ` ` ` `print` `(` `"Count is "` `, countFact(` `2` `, ` `720` `)) ` ` ` `# This code is contributed ` `# by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# Program to count factorial ` `// numbers in given range ` `using` `System; ` ` ` `public` `class` `GFG ` `{ ` ` ` ` ` `// Function to count factorial ` ` ` `static` `int` `countFact(` `int` `low, ` `int` `high) ` ` ` `{ ` ` ` ` ` `// Find the first factorial number numbers ` ` ` `// 'fact' greater than or equal to 'low' ` ` ` `int` `fact = 1, x = 1; ` ` ` `while` `(fact < low) ` ` ` `{ ` ` ` `fact = fact * x; ` ` ` `x++; ` ` ` `} ` ` ` ` ` `// Count factorial numbers ` ` ` `// in range [low, high] ` ` ` `int` `res = 0; ` ` ` `while` `(fact <= high) ` ` ` `{ ` ` ` `res++; ` ` ` `fact = fact * x; ` ` ` `x++; ` ` ` `} ` ` ` ` ` `// Return the count ` ` ` `return` `res; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `Console.Write(` `"Count is "` `+ countFact(2, 720)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Sam007 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// Program to count factorial ` `// numbers in given range ` `function` `countFact(` `$low` `, ` `$high` `) ` `{ ` ` ` `// Find the first factorial ` ` ` `// number 'fact' greater ` ` ` `// than or equal to 'low' ` ` ` `$fact` `= 1; ` `$x` `= 1; ` ` ` `while` `(` `$fact` `< ` `$low` `) ` ` ` `{ ` ` ` `$fact` `= ` `$fact` `* ` `$x` `; ` ` ` `$x` `++; ` ` ` `} ` ` ` ` ` `// Count factorial numbers ` ` ` `// in range [low, high] ` ` ` `$res` `= 0; ` ` ` `while` `(` `$fact` `<= ` `$high` `) ` ` ` `{ ` ` ` `$res` `++; ` ` ` `$fact` `= ` `$fact` `* ` `$x` `; ` ` ` `$x` `++; ` ` ` `} ` ` ` ` ` `// Return the count ` ` ` `return` `$res` `; ` `} ` ` ` `// Driver Code ` `echo` `"Count is "` `, countFact(2, 720); ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

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.

## Recommended Posts:

- 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 of numbers from range [L, R] whose sum of digits is Y
- Count numbers in range 1 to N which are divisible by X but not by Y
- Count the numbers divisible by 'M' in a given range
- Sum of numbers in a range [L, R] whose count of divisors is prime
- Count numbers from range whose prime factors are only 2 and 3
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3
- Count all the numbers in a range with smallest factor as K
- Numbers in range [L, R] such that the count of their divisors is both even and prime
- Count numbers in range L-R that are divisible by all of its non-zero digits
- Count of common multiples of two numbers in a range
- Count numbers with unit digit k in given range
- Count of distinct remainders when N is divided by all the numbers from the range [1, N]