Count factorial numbers in given range
The problem at hand is to count the number of factorial numbers within a given range. A factorial of a nonnegative integer 'n' is the product of all positive integers from 1 to 'n'. Mathematically, it's denoted as 'n!' and calculated as follows:
n!=n×(n−1)×(n−2)×…×2×1
For example, 5!=5×4×3×2×1=120.
The task is to find how many factorial numbers exist within a specified range [front, back] (both inclusive).
Problem Statement and Description
Given a range [front, back], the problem is to count how many factorial numbers exist within this range. A factorial number is a number that can be expressed as 'n!', where 'n' is a nonnegative integer.
For example, in the range [0, 200], there are 5 factorial numbers: 0!, 1!, 2!, 3!, and 4!. In the range [300, 1000], there is only one factorial number: 720 (which is 6!). In the range [1000, 2000], there are no factorial numbers. In the range [1, 2], there are two factorial numbers: 1! and 2!. And so on.
Idea to Solve the Problem
To solve this problem, we need to iterate through the given range [front, back] and check if each number within this range is a factorial number or not. We can do this by calculating the factorial of each number and checking if it falls within the range.
Algorithm

Start by defining the function
count_factorial
which takes two integers 'front' and 'back' as input. 
Check if 'front' is greater than 'back'. If it is, swap the values of 'front' and 'back' to ensure a valid range.

Initialize a variable 'result' to store the count of factorial numbers in the given range.

Initialize a loop controlling variable 'counter' and a variable 'factorial' both to 1.

Enter a while loop that continues as long as 'factorial' is less than or equal to 'back'.

Inside the loop, calculate the factorial by multiplying 'factorial' with 'counter'.

Check if 'factorial' is greater than or equal to 'front' and less than or equal to 'back'. If it is, increment 'result' by 1.

Increment 'counter' by 1.

Continue the loop until the condition in step 5 is satisfied.

After exiting the loop, print the value of 'result' along with the range [front, back].

In the main function, call the
count_factorial
function for various test cases.
Pseudocode
function count_factorial(front, back):
if front > back:
swap(front, back)
result = 0
counter = 1
factorial = 1
while factorial <= back:
factorial = factorial * counter
if factorial >= front and factorial <= back:
result = result + 1
counter = counter + 1
print("Factorial in range [", front, ",", back, "] is :", result)
Explanation
The provided code first defines the function count_factorial
as described in the pseudocode. It
correctly counts the number of factorial numbers within the given range for each test case. The main function calls
the count_factorial
function with different ranges.
Code Solution
// C program
// Count factorial numbers in given range
#include <stdio.h>
//Count the number of factorials in given range
void count_factorial(int front, int back)
{
if (front > back)
{
//Transform into a valid range
count_factorial(back, front);
}
else
{
int result = 0;
//Define loop controlling variables
int counter = 1;
int factorial = 1;
//Execute the loop until the resulting factorial not exceeds the given limit
while (factorial <= back)
{
//Calculate factorial
factorial = factorial * counter;
if (factorial >= front && factorial <= back)
{
//When factorial exist in range (front to back)
result++;
}
counter++;
}
printf("\n Factorial in range [%d,%d] is : %d", front, back, result);
}
}
int main()
{
//Test case
count_factorial(0, 200);
count_factorial(300, 1000);
count_factorial(1000, 2000);
count_factorial(0, 0);
count_factorial(1, 2);
count_factorial(6000, 0);
return 0;
}
Output
Factorial in range [0,200] is : 5
Factorial in range [300,1000] is : 1
Factorial in range [1000,2000] is : 0
Factorial in range [0,0] is : 0
Factorial in range [1,2] is : 2
Factorial in range [0,6000] is : 7
/*
Java program
Count factorial numbers in given range
*/
class Factorials
{
//Count the number of factorials in given range
public void count_factorial(int front, int back)
{
if (front > back)
{
//Transform into a valid range
count_factorial(back, front);
}
else
{
int result = 0;
//Define loop controlling variables
int counter = 1;
int factorial = 1;
//Execute the loop until the resulting factorial not exceeds the given limit
while (factorial <= back)
{
//Calculate factorial
factorial = factorial * counter;
if (factorial >= front && factorial <= back)
{
//When factorial exist in range (front to back)
result++;
}
counter++;
}
System.out.print("\n Factorial in range [" + front + "," + back + "] is : " + result);
}
}
public static void main(String[] args)
{
Factorials obj = new Factorials();
//Test case
obj.count_factorial(0, 200);
obj.count_factorial(300, 1000);
obj.count_factorial(1000, 2000);
obj.count_factorial(0, 0);
obj.count_factorial(1, 2);
obj.count_factorial(6000, 0);
}
}
Output
Factorial in range [0,200] is : 5
Factorial in range [300,1000] is : 1
Factorial in range [1000,2000] is : 0
Factorial in range [0,0] is : 0
Factorial in range [1,2] is : 2
Factorial in range [0,6000] is : 7
//Include header file
#include <iostream>
using namespace std;
/*
C++ program
Count factorial numbers in given range
*/
class Factorials
{
public:
//Count the number of factorials in given range
void count_factorial(int front, int back)
{
if (front > back)
{
//Transform into a valid range
this>count_factorial(back, front);
}
else
{
int result = 0;
//Define loop controlling variables
int counter = 1;
int factorial = 1;
//Execute the loop until the resulting factorial not exceeds the given limit
while (factorial <= back)
{
//Calculate factorial
factorial = factorial *counter;
if (factorial >= front && factorial <= back)
{
//When factorial exist in range (front to back)
result++;
}
counter++;
}
cout << "\n Factorial in range [" << front << "," << back << "] is : " << result;
}
}
};
int main()
{
Factorials obj = Factorials();
//Test case
obj.count_factorial(0, 200);
obj.count_factorial(300, 1000);
obj.count_factorial(1000, 2000);
obj.count_factorial(0, 0);
obj.count_factorial(1, 2);
obj.count_factorial(6000, 0);
return 0;
}
Output
Factorial in range [0,200] is : 5
Factorial in range [300,1000] is : 1
Factorial in range [1000,2000] is : 0
Factorial in range [0,0] is : 0
Factorial in range [1,2] is : 2
Factorial in range [0,6000] is : 7
//Include namespace system
using System;
/*
C# program
Count factorial numbers in given range
*/
class Factorials
{
//Count the number of factorials in given range
public void count_factorial(int front, int back)
{
if (front > back)
{
//Transform into a valid range
count_factorial(back, front);
}
else
{
int result = 0;
//Define loop controlling variables
int counter = 1;
int factorial = 1;
//Execute the loop until the resulting factorial not exceeds the given limit
while (factorial <= back)
{
//Calculate factorial
factorial = factorial * counter;
if (factorial >= front && factorial <= back)
{
//When factorial exist in range (front to back)
result++;
}
counter++;
}
Console.Write("\n Factorial in range [" + front + "," + back + "] is : " + result);
}
}
public static void Main(String[] args)
{
Factorials obj = new Factorials();
//Test case
obj.count_factorial(0, 200);
obj.count_factorial(300, 1000);
obj.count_factorial(1000, 2000);
obj.count_factorial(0, 0);
obj.count_factorial(1, 2);
obj.count_factorial(6000, 0);
}
}
Output
Factorial in range [0,200] is : 5
Factorial in range [300,1000] is : 1
Factorial in range [1000,2000] is : 0
Factorial in range [0,0] is : 0
Factorial in range [1,2] is : 2
Factorial in range [0,6000] is : 7
<?php
/*
Php program
Count factorial numbers in given range
*/
class Factorials
{
//Count the number of factorials in given range
public function count_factorial($front, $back)
{
if ($front > $back)
{
//Transform into a valid range
$this>count_factorial($back, $front);
}
else
{
$result = 0;
//Define loop controlling variables
$counter = 1;
$factorial = 1;
//Execute the loop until the resulting factorial not exceeds the given limit
while ($factorial <= $back)
{
//Calculate factorial
$factorial = $factorial * $counter;
if ($factorial >= $front && $factorial <= $back)
{
//When factorial exist in range (front to back)
$result++;
}
$counter++;
}
echo "\n Factorial in range [". $front .",". $back ."] is : ". $result;
}
}
}
function main()
{
$obj = new Factorials();
//Test case
$obj>count_factorial(0, 200);
$obj>count_factorial(300, 1000);
$obj>count_factorial(1000, 2000);
$obj>count_factorial(0, 0);
$obj>count_factorial(1, 2);
$obj>count_factorial(6000, 0);
}
main();
Output
Factorial in range [0,200] is : 5
Factorial in range [300,1000] is : 1
Factorial in range [1000,2000] is : 0
Factorial in range [0,0] is : 0
Factorial in range [1,2] is : 2
Factorial in range [0,6000] is : 7
/*
Node Js program
Count factorial numbers in given range
*/
class Factorials
{
//Count the number of factorials in given range
count_factorial(front, back)
{
if (front > back)
{
//Transform into a valid range
this.count_factorial(back, front);
}
else
{
var result = 0;
//Define loop controlling variables
var counter = 1;
var factorial = 1;
//Execute the loop until the resulting factorial not exceeds the given limit
while (factorial <= back)
{
//Calculate factorial
factorial = factorial * counter;
if (factorial >= front && factorial <= back)
{
//When factorial exist in range (front to back)
result++;
}
counter++;
}
process.stdout.write("\n Factorial in range [" + front + "," + back + "] is : " + result);
}
}
}
function main()
{
var obj = new Factorials();
//Test case
obj.count_factorial(0, 200);
obj.count_factorial(300, 1000);
obj.count_factorial(1000, 2000);
obj.count_factorial(0, 0);
obj.count_factorial(1, 2);
obj.count_factorial(6000, 0);
}
main();
Output
Factorial in range [0,200] is : 5
Factorial in range [300,1000] is : 1
Factorial in range [1000,2000] is : 0
Factorial in range [0,0] is : 0
Factorial in range [1,2] is : 2
Factorial in range [0,6000] is : 7
# Python 3 program
# Count factorial numbers in given range
class Factorials :
# Count the number of factorials in given range
def count_factorial(self, front, back) :
if (front > back) :
# Transform into a valid range
self.count_factorial(back, front)
else :
result = 0
# Define loop controlling variables
counter = 1
factorial = 1
# Execute the loop until the resulting factorial not exceeds the given limit
while (factorial <= back) :
# Calculate factorial
factorial = factorial * counter
if (factorial >= front and factorial <= back) :
# When factorial exist in range (front to back)
result += 1
counter += 1
print("\n Factorial in range [", front ,",", back ,"] is : ", result, end = "")
def main() :
obj = Factorials()
# Test case
obj.count_factorial(0, 200)
obj.count_factorial(300, 1000)
obj.count_factorial(1000, 2000)
obj.count_factorial(0, 0)
obj.count_factorial(1, 2)
obj.count_factorial(6000, 0)
if __name__ == "__main__": main()
Output
Factorial in range [ 0 , 200 ] is : 5
Factorial in range [ 300 , 1000 ] is : 1
Factorial in range [ 1000 , 2000 ] is : 0
Factorial in range [ 0 , 0 ] is : 0
Factorial in range [ 1 , 2 ] is : 2
Factorial in range [ 0 , 6000 ] is : 7
# Ruby program
# Count factorial numbers in given range
class Factorials
# Count the number of factorials in given range
def count_factorial(front, back)
if (front > back)
# Transform into a valid range
self.count_factorial(back, front)
else
result = 0
# Define loop controlling variables
counter = 1
factorial = 1
# Execute the loop until the resulting factorial not exceeds the given limit
while (factorial <= back)
# Calculate factorial
factorial = factorial * counter
if (factorial >= front && factorial <= back)
# When factorial exist in range (front to back)
result += 1
end
counter += 1
end
print("\n Factorial in range [", front ,",", back ,"] is : ", result)
end
end
end
def main()
obj = Factorials.new()
# Test case
obj.count_factorial(0, 200)
obj.count_factorial(300, 1000)
obj.count_factorial(1000, 2000)
obj.count_factorial(0, 0)
obj.count_factorial(1, 2)
obj.count_factorial(6000, 0)
end
main()
Output
Factorial in range [0,200] is : 5
Factorial in range [300,1000] is : 1
Factorial in range [1000,2000] is : 0
Factorial in range [0,0] is : 0
Factorial in range [1,2] is : 2
Factorial in range [0,6000] is : 7
/*
Scala program
Count factorial numbers in given range
*/
class Factorials
{
//Count the number of factorials in given range
def count_factorial(front: Int, back: Int): Unit = {
if (front > back)
{
//Transform into a valid range
count_factorial(back, front);
}
else
{
var result: Int = 0;
//Define loop controlling variables
var counter: Int = 1;
var factorial: Int = 1;
//Execute the loop until the resulting factorial not exceeds the given limit
while (factorial <= back)
{
//Calculate factorial
factorial = factorial * counter;
if (factorial >= front && factorial <= back)
{
//When factorial exist in range (front to back)
result += 1;
}
counter += 1;
}
print("\n Factorial in range [" + front + "," + back + "] is : " + result);
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: Factorials = new Factorials();
//Test case
obj.count_factorial(0, 200);
obj.count_factorial(300, 1000);
obj.count_factorial(1000, 2000);
obj.count_factorial(0, 0);
obj.count_factorial(1, 2);
obj.count_factorial(6000, 0);
}
}
Output
Factorial in range [0,200] is : 5
Factorial in range [300,1000] is : 1
Factorial in range [1000,2000] is : 0
Factorial in range [0,0] is : 0
Factorial in range [1,2] is : 2
Factorial in range [0,6000] is : 7
/*
Swift 4 program
Count factorial numbers in given range
*/
class Factorials
{
//Count the number of factorials in given range
func count_factorial(_ front: Int, _ back: Int)
{
if (front > back)
{
//Transform into a valid range
self.count_factorial(back, front);
}
else
{
var result: Int = 0;
//Define loop controlling variables
var counter: Int = 1;
var factorial: Int = 1;
//Execute the loop until the resulting factorial not exceeds the given limit
while (factorial <= back)
{
//Calculate factorial
factorial = factorial * counter;
if (factorial >= front && factorial <= back)
{
//When factorial exist in range (front to back)
result += 1;
}
counter += 1;
}
print("\n Factorial in range [", front ,",", back ,"]is : ", result, terminator: "");
}
}
}
func main()
{
let obj: Factorials = Factorials();
//Test case
obj.count_factorial(0, 200);
obj.count_factorial(300, 1000);
obj.count_factorial(1000, 2000);
obj.count_factorial(0, 0);
obj.count_factorial(1, 2);
obj.count_factorial(6000, 0);
}
main();
Output
Factorial in range [ 0 , 200 ]is : 5
Factorial in range [ 300 , 1000 ]is : 1
Factorial in range [ 1000 , 2000 ]is : 0
Factorial in range [ 0 , 0 ]is : 0
Factorial in range [ 1 , 2 ]is : 2
Factorial in range [ 0 , 6000 ]is : 7
Output Explanation
The output of the code matches the expected results. It provides the count of factorial numbers within the specified ranges.
 In the range [0, 200], there are 5 factorial numbers: 0!, 1!, 2!, 3!, and 4!.
 In the range [300, 1000], there is only one factorial number: 720 (which is 6!).
 In the range [1000, 2000], there are no factorial numbers.
 In the range [1, 2], there are two factorial numbers: 1! and 2!.
 In the range [0, 0], there are no factorial numbers.
 In the range [6000, 0], there are seven factorial numbers: 0!, 1!, 2!, 3!, 4!, 5!, and 6!.
Time Complexity
The time complexity of the given algorithm mainly depends on the loop that iterates while calculating factorials. In the worst case, the loop will run until the factorial becomes greater than 'back'. The maximum value of 'back' can be considered as 'n'. So, the worstcase time complexity of this algorithm is O(n).
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment