Skip to main content

Count factorial numbers in given range

To count the number of factorial numbers in a given range, we need to first define what a factorial number is.

A factorial number is a positive integer that is the product of all positive integers less than or equal to itself. For example, 4! (read as "4 factorial") is equal to 4 x 3 x 2 x 1 = 24.

To count the number of factorial numbers in a given range, we can iterate through each integer in the range and check if it is a factorial number. To do this, we can calculate the factorial of each integer less than or equal to the square root of the maximum number in the range, and check if any of those factorials are in the range.

Here given code implementation process.

// 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




Comment

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