Posted on by Kalkicode
Code Mathematics

Count all x divisible number in a range

The problem at hand is to count the numbers within a given range that are divisible by a specific number 'x'. The task is to write a program that takes a starting number, an ending number, and a divisor 'x' as input, and then calculates and outputs the count of numbers within that range that are divisible by 'x'.

Problem Statement

Given a range defined by a starting number 'start' and an ending number 'last', along with a divisor 'x', the goal is to count the numbers within the range [start, last] that are divisible by 'x'. The program should then print the list of such numbers and also display the count of such numbers.

Example

Let's take the first test case from the provided code: x = 4, start = 2, and last = 38. The numbers divisible by 4 in the range [2, 38] are 4, 8, 12, 16, 20, 24, 28, 32, and 36. There are a total of 9 numbers that fulfill this condition. The output from the program confirms this by listing the divisible numbers and displaying the result as 9.

Idea to Solve the Problem

To solve this problem, we can iterate through the range of numbers from 'start' to 'last' and check whether each number is divisible by 'x'. If it is divisible, we increment a counter and also store the divisible number in a list. At the end of the iteration, we print the list of divisible numbers and the counter.

Pseudocode

Here is a standard pseudocode representation of the algorithm:

count_divisible(start, last, x):
    if start > last:
        swap start and last
    num = start
    counter = 0
    divisible_numbers = empty list
    
    while num <= last:
        if num % x == 0:
            add num to divisible_numbers
            increment counter
        increment num by x (if counter > 0) or by 1
        
    print "Divisible by", x, "in range of [", start, "-", last, "] are :", divisible_numbers
    print "Result:", counter

Algorithm Explanation

  1. If the starting number is greater than the ending number, we swap their values to ensure 'start' is always less than or equal to 'last'.
  2. Initialize 'num' with 'start', 'counter' with 0, and 'divisible_numbers' as an empty list.
  3. Enter a loop that iterates while 'num' is less than or equal to 'last'.
  4. Check if 'num' is divisible by 'x'. If true, add 'num' to the 'divisible_numbers' list and increment 'counter'.
  5. Depending on whether 'counter' is greater than 0 or not, either increment 'num' by 'x' or by 1.
  6. After the loop, print the list of divisible numbers and the calculated counter.

Code Solution

// C program
// Count all x divisible number in a range
#include <stdio.h>

// Count the divisible number in the given range
void count_divisible(int start, int last, int x)
{
	if (start > last)
	{
		//Change sequence
		count_divisible(last, start, x);
		return;
	}
	else
	{
		int num = start;
		int counter = 0;
		printf("\n Divisible by %d in range of [%d-%d] are : [", x, start, last);
		while (num <= last)
		{
			if (num % x == 0)
			{
				// When x, is divisible by num
				printf(" %d ", num);
				//When get divisible number then increase counter value
				counter++;
			}
			if (counter > 0)
			{
				//visit to next divisible number
				num += x;
			}
			else
			{
				num++;
			}
		}
		// Display calculated result
		printf("]\n Result : %d\n", counter);
	}
}
int main()
{
	//Test case
	int x = 4;
	count_divisible(2, 38, x);
	x = 3;
	count_divisible(1, 10, x);
	x = 13;
	count_divisible(21, 33, x);
	x = 7;
	count_divisible(7, 29, x);
	x = 2;
	count_divisible(0, 0, x);
	x = 6;
	count_divisible(50, 120, x);
	return 0;
}

Output

 Divisible by 4 in range of [2-38] are : [ 4  8  12  16  20  24  28  32  36 ]
 Result : 9

 Divisible by 3 in range of [1-10] are : [ 3  6  9 ]
 Result : 3

 Divisible by 13 in range of [21-33] are : [ 26 ]
 Result : 1

 Divisible by 7 in range of [7-29] are : [ 7  14  21  28 ]
 Result : 4

 Divisible by 2 in range of [0-0] are : [ 0 ]
 Result : 1

 Divisible by 6 in range of [50-120] are : [ 54  60  66  72  78  84  90  96  102  108  114  120 ]
 Result : 12
/* 
  Java program 
  Count all x divisible number in a range
*/
class DivisibleNumbers
{
	// Count the divisible number in the given range
	public void count_divisible(int start, int last, int x)
	{
		if (start > last)
		{
			//Change sequence
			count_divisible(last, start, x);
			return;
		}
		else
		{
			int num = start;
			int counter = 0;
			System.out.print("\n Divisible by " + x + " in range of [" + start + "-" + last + "] are : [");
			while (num <= last)
			{
				if (num % x == 0)
				{
					// When x, is divisible by num
					System.out.print(" " + num + " ");
					//When get divisible number then increase counter value
					counter++;
				}
				if (counter > 0)
				{
					//visit to next divisible number
					num += x;
				}
				else
				{
					num++;
				}
			}
			// Display calculated result
			System.out.print("]\n Result : " + counter + "\n");
		}
	}
	public static void main(String[] args)
	{
		DivisibleNumbers obj = new DivisibleNumbers();
		//Test case
		int x = 4;
		obj.count_divisible(2, 38, x);
		x = 3;
		obj.count_divisible(1, 10, x);
		x = 13;
		obj.count_divisible(21, 33, x);
		x = 7;
		obj.count_divisible(7, 29, x);
		x = 2;
		obj.count_divisible(0, 0, x);
		x = 6;
		obj.count_divisible(50, 120, x);
	}
}

Output

 Divisible by 4 in range of [2-38] are : [ 4  8  12  16  20  24  28  32  36 ]
 Result : 9

 Divisible by 3 in range of [1-10] are : [ 3  6  9 ]
 Result : 3

 Divisible by 13 in range of [21-33] are : [ 26 ]
 Result : 1

 Divisible by 7 in range of [7-29] are : [ 7  14  21  28 ]
 Result : 4

 Divisible by 2 in range of [0-0] are : [ 0 ]
 Result : 1

 Divisible by 6 in range of [50-120] are : [ 54  60  66  72  78  84  90  96  102  108  114  120 ]
 Result : 12
//Include header file
#include <iostream>
using namespace std;

/*
  C++ program 
  Count all x divisible number in a range
*/

class DivisibleNumbers
{
	public:
		// Count the divisible number in the given range
		void count_divisible(int start, int last, int x)
		{
			if (start > last)
			{
				//Change sequence
				this->count_divisible(last, start, x);
				return;
			}
			else
			{
				int num = start;
				int counter = 0;
				cout << "\n Divisible by " << x << " in range of [" << start << "-" << last << "] are : [";
				while (num <= last)
				{
					if (num % x == 0)
					{
						// When x, is divisible by num
						cout << " " << num << " ";
						//When get divisible number then increase counter value
						counter++;
					}
					if (counter > 0)
					{
						//visit to next divisible number
						num += x;
					}
					else
					{
						num++;
					}
				}
				// Display calculated result
				cout << "]\n Result : " << counter << "\n";
			}
		}
};
int main()
{
	DivisibleNumbers obj = DivisibleNumbers();
	//Test case
	int x = 4;
	obj.count_divisible(2, 38, x);
	x = 3;
	obj.count_divisible(1, 10, x);
	x = 13;
	obj.count_divisible(21, 33, x);
	x = 7;
	obj.count_divisible(7, 29, x);
	x = 2;
	obj.count_divisible(0, 0, x);
	x = 6;
	obj.count_divisible(50, 120, x);
	return 0;
}

Output

 Divisible by 4 in range of [2-38] are : [ 4  8  12  16  20  24  28  32  36 ]
 Result : 9

 Divisible by 3 in range of [1-10] are : [ 3  6  9 ]
 Result : 3

 Divisible by 13 in range of [21-33] are : [ 26 ]
 Result : 1

 Divisible by 7 in range of [7-29] are : [ 7  14  21  28 ]
 Result : 4

 Divisible by 2 in range of [0-0] are : [ 0 ]
 Result : 1

 Divisible by 6 in range of [50-120] are : [ 54  60  66  72  78  84  90  96  102  108  114  120 ]
 Result : 12
//Include namespace system
using System;
/* 
  C# program 
  Count all x divisible number in a range
*/
class DivisibleNumbers
{
	// Count the divisible number in the given range
	public void count_divisible(int start, int last, int x)
	{
		if (start > last)
		{
			//Change sequence
			count_divisible(last, start, x);
			return;
		}
		else
		{
			int num = start;
			int counter = 0;
			Console.Write("\n Divisible by " + x + " in range of [" + start + "-" + last + "] are : [");
			while (num <= last)
			{
				if (num % x == 0)
				{
					// When x, is divisible by num
					Console.Write(" " + num + " ");
					//When get divisible number then increase counter value
					counter++;
				}
				if (counter > 0)
				{
					//visit to next divisible number
					num += x;
				}
				else
				{
					num++;
				}
			}
			// Display calculated result
			Console.Write("]\n Result : " + counter + "\n");
		}
	}
	public static void Main(String[] args)
	{
		DivisibleNumbers obj = new DivisibleNumbers();
		//Test case
		int x = 4;
		obj.count_divisible(2, 38, x);
		x = 3;
		obj.count_divisible(1, 10, x);
		x = 13;
		obj.count_divisible(21, 33, x);
		x = 7;
		obj.count_divisible(7, 29, x);
		x = 2;
		obj.count_divisible(0, 0, x);
		x = 6;
		obj.count_divisible(50, 120, x);
	}
}

Output

 Divisible by 4 in range of [2-38] are : [ 4  8  12  16  20  24  28  32  36 ]
 Result : 9

 Divisible by 3 in range of [1-10] are : [ 3  6  9 ]
 Result : 3

 Divisible by 13 in range of [21-33] are : [ 26 ]
 Result : 1

 Divisible by 7 in range of [7-29] are : [ 7  14  21  28 ]
 Result : 4

 Divisible by 2 in range of [0-0] are : [ 0 ]
 Result : 1

 Divisible by 6 in range of [50-120] are : [ 54  60  66  72  78  84  90  96  102  108  114  120 ]
 Result : 12
<?php

/* 
  Php program 
  Count all x divisible number in a range
*/

class DivisibleNumbers
{
	// Count the divisible number in the given range
	public	function count_divisible($start, $last, $x)
	{
		if ($start > $last)
		{
			//Change sequence
			$this->count_divisible($last, $start, $x);
			return;
		}
		else
		{
			$num = $start;
			$counter = 0;
			echo "\n Divisible by ". $x ." in range of [". $start ."-". $last ."] are : [";
			while ($num <= $last)
			{
				if ($num % $x == 0)
				{
					// When x, is divisible by num
					echo " ". $num ." ";
					//When get divisible number then increase counter value
					$counter++;
				}
				if ($counter > 0)
				{
					//visit to next divisible number
					$num += $x;
				}
				else
				{
					$num++;
				}
			}
			// Display calculated result
			echo "]\n Result : ". $counter ."\n";
		}
	}
}

function main()
{
	$obj = new DivisibleNumbers();
	//Test case
	$x = 4;
	$obj->count_divisible(2, 38, $x);
	$x = 3;
	$obj->count_divisible(1, 10, $x);
	$x = 13;
	$obj->count_divisible(21, 33, $x);
	$x = 7;
	$obj->count_divisible(7, 29, $x);
	$x = 2;
	$obj->count_divisible(0, 0, $x);
	$x = 6;
	$obj->count_divisible(50, 120, $x);
}
main();

Output

 Divisible by 4 in range of [2-38] are : [ 4  8  12  16  20  24  28  32  36 ]
 Result : 9

 Divisible by 3 in range of [1-10] are : [ 3  6  9 ]
 Result : 3

 Divisible by 13 in range of [21-33] are : [ 26 ]
 Result : 1

 Divisible by 7 in range of [7-29] are : [ 7  14  21  28 ]
 Result : 4

 Divisible by 2 in range of [0-0] are : [ 0 ]
 Result : 1

 Divisible by 6 in range of [50-120] are : [ 54  60  66  72  78  84  90  96  102  108  114  120 ]
 Result : 12
/* 
  Node Js program 
  Count all x divisible number in a range
*/
class DivisibleNumbers
{
	// Count the divisible number in the given range
	count_divisible(start, last, x)
	{
		if (start > last)
		{
			//Change sequence
			this.count_divisible(last, start, x);
			return;
		}
		else
		{
			var num = start;
			var counter = 0;
			process.stdout.write("\n Divisible by " + x + " in range of [" + start + "-" + last + "] are : [");
			while (num <= last)
			{
				if (num % x == 0)
				{
					// When x, is divisible by num
					process.stdout.write(" " + num + " ");
					//When get divisible number then increase counter value
					counter++;
				}
				if (counter > 0)
				{
					//visit to next divisible number
					num += x;
				}
				else
				{
					num++;
				}
			}
			// Display calculated result
			process.stdout.write("]\n Result : " + counter + "\n");
		}
	}
}

function main()
{
	var obj = new DivisibleNumbers();
	//Test case
	var x = 4;
	obj.count_divisible(2, 38, x);
	x = 3;
	obj.count_divisible(1, 10, x);
	x = 13;
	obj.count_divisible(21, 33, x);
	x = 7;
	obj.count_divisible(7, 29, x);
	x = 2;
	obj.count_divisible(0, 0, x);
	x = 6;
	obj.count_divisible(50, 120, x);
}
main();

Output

 Divisible by 4 in range of [2-38] are : [ 4  8  12  16  20  24  28  32  36 ]
 Result : 9

 Divisible by 3 in range of [1-10] are : [ 3  6  9 ]
 Result : 3

 Divisible by 13 in range of [21-33] are : [ 26 ]
 Result : 1

 Divisible by 7 in range of [7-29] are : [ 7  14  21  28 ]
 Result : 4

 Divisible by 2 in range of [0-0] are : [ 0 ]
 Result : 1

 Divisible by 6 in range of [50-120] are : [ 54  60  66  72  78  84  90  96  102  108  114  120 ]
 Result : 12
#   Python 3 program 
#   Count all x divisible number in a range

class DivisibleNumbers :
	#  Count the divisible number in the given range
	def count_divisible(self, start, last, x) :
		if (start > last) :
			# Change sequence
			self.count_divisible(last, start, x)
			return
		else :
			num = start
			counter = 0
			print("\n Divisible by ", x ," in range of [", start ,"-", last ,"] are : [", end = "")
			while (num <= last) :
				if (num % x == 0) :
					#  When x, is divisible by num
					print(" ", num ," ", end = "")
					# When get divisible number then increase counter value
					counter += 1
				
				if (counter > 0) :
					# visit to next divisible number
					num += x
				else :
					num += 1
				
			
			#  Display calculated result
			print("]\n Result : ", counter ,"\n", end = "")
		
	

def main() :
	obj = DivisibleNumbers()
	# Test case
	x = 4
	obj.count_divisible(2, 38, x)
	x = 3
	obj.count_divisible(1, 10, x)
	x = 13
	obj.count_divisible(21, 33, x)
	x = 7
	obj.count_divisible(7, 29, x)
	x = 2
	obj.count_divisible(0, 0, x)
	x = 6
	obj.count_divisible(50, 120, x)

if __name__ == "__main__": main()

Output

 Divisible by  4  in range of [ 2 - 38 ] are : [  4    8    12    16    20    24    28    32    36  ]
 Result :  9

 Divisible by  3  in range of [ 1 - 10 ] are : [  3    6    9  ]
 Result :  3

 Divisible by  13  in range of [ 21 - 33 ] are : [  26  ]
 Result :  1

 Divisible by  7  in range of [ 7 - 29 ] are : [  7    14    21    28  ]
 Result :  4

 Divisible by  2  in range of [ 0 - 0 ] are : [  0  ]
 Result :  1

 Divisible by  6  in range of [ 50 - 120 ] are : [  54    60    66    72    78    84    90    96    102    108    114    120  ]
 Result :  12
#   Ruby program 
#   Count all x divisible number in a range

class DivisibleNumbers 
	#  Count the divisible number in the given range
	def count_divisible(start, last, x) 
		if (start > last) 
			# Change sequence
			self.count_divisible(last, start, x)
			return
		else 
			num = start
			counter = 0
			print("\n Divisible by ", x ," in range of [", start ,"-", last ,"] are : [")
			while (num <= last) 
				if (num % x == 0) 
					#  When x, is divisible by num
					print(" ", num ," ")
					# When get divisible number then increase counter value
					counter += 1
				end

				if (counter > 0) 
					# visit to next divisible number
					num += x
				else 
					num += 1
				end

			end

			#  Display calculated result
			print("]\n Result : ", counter ,"\n")
		end

	end

end

def main() 
	obj = DivisibleNumbers.new()
	# Test case
	x = 4
	obj.count_divisible(2, 38, x)
	x = 3
	obj.count_divisible(1, 10, x)
	x = 13
	obj.count_divisible(21, 33, x)
	x = 7
	obj.count_divisible(7, 29, x)
	x = 2
	obj.count_divisible(0, 0, x)
	x = 6
	obj.count_divisible(50, 120, x)
end

main()

Output

 Divisible by 4 in range of [2-38] are : [ 4  8  12  16  20  24  28  32  36 ]
 Result : 9

 Divisible by 3 in range of [1-10] are : [ 3  6  9 ]
 Result : 3

 Divisible by 13 in range of [21-33] are : [ 26 ]
 Result : 1

 Divisible by 7 in range of [7-29] are : [ 7  14  21  28 ]
 Result : 4

 Divisible by 2 in range of [0-0] are : [ 0 ]
 Result : 1

 Divisible by 6 in range of [50-120] are : [ 54  60  66  72  78  84  90  96  102  108  114  120 ]
 Result : 12
/* 
  Scala program 
  Count all x divisible number in a range
*/
class DivisibleNumbers
{
	// Count the divisible number in the given range
	def count_divisible(start: Int, last: Int, x: Int): Unit = {
		if (start > last)
		{
			//Change sequence
			count_divisible(last, start, x);
			return;
		}
		else
		{
			var num: Int = start;
			var counter: Int = 0;
			print("\n Divisible by " + x + " in range of [" + start + "-" + last + "] are : [");
			while (num <= last)
			{
				if (num % x == 0)
				{
					// When x, is divisible by num
					print(" " + num + " ");
					//When get divisible number then increase counter value
					counter += 1;
				}
				if (counter > 0)
				{
					//visit to next divisible number
					num += x;
				}
				else
				{
					num += 1;
				}
			}
			// Display calculated result
			print("]\n Result : " + counter + "\n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: DivisibleNumbers = new DivisibleNumbers();
		//Test case
		var x: Int = 4;
		obj.count_divisible(2, 38, x);
		x = 3;
		obj.count_divisible(1, 10, x);
		x = 13;
		obj.count_divisible(21, 33, x);
		x = 7;
		obj.count_divisible(7, 29, x);
		x = 2;
		obj.count_divisible(0, 0, x);
		x = 6;
		obj.count_divisible(50, 120, x);
	}
}

Output

 Divisible by 4 in range of [2-38] are : [ 4  8  12  16  20  24  28  32  36 ]
 Result : 9

 Divisible by 3 in range of [1-10] are : [ 3  6  9 ]
 Result : 3

 Divisible by 13 in range of [21-33] are : [ 26 ]
 Result : 1

 Divisible by 7 in range of [7-29] are : [ 7  14  21  28 ]
 Result : 4

 Divisible by 2 in range of [0-0] are : [ 0 ]
 Result : 1

 Divisible by 6 in range of [50-120] are : [ 54  60  66  72  78  84  90  96  102  108  114  120 ]
 Result : 12
/* 
  Swift 4 program 
  Count all x divisible number in a range
*/
class DivisibleNumbers
{
	// Count the divisible number in the given range
	func count_divisible(_ start: Int, _ last: Int, _ x: Int)
	{
		if (start > last)
		{
			//Change sequence
			self.count_divisible(last, start, x);
			return;
		}
		else
		{
			var num: Int = start;
			var counter: Int = 0;
			print("\n Divisible by ", x ," in range of [", start ,"-", last ,"]are : [", terminator: "");
			while (num <= last)
			{
				if (num % x == 0)
				{
					// When x, is divisible by num
					print(" ", num ," ", terminator: "");
					//When get divisible number then increase counter value
					counter += 1;
				}
				if (counter > 0)
				{
					//visit to next divisible number
					num += x;
				}
				else
				{
					num += 1;
				}
			}
			// Display calculated result
			print("]\n Result : ", counter ,"\n", terminator: "");
		}
	}
}
func main()
{
	let obj: DivisibleNumbers = DivisibleNumbers();
	//Test case
	var x: Int = 4;
	obj.count_divisible(2, 38, x);
	x = 3;
	obj.count_divisible(1, 10, x);
	x = 13;
	obj.count_divisible(21, 33, x);
	x = 7;
	obj.count_divisible(7, 29, x);
	x = 2;
	obj.count_divisible(0, 0, x);
	x = 6;
	obj.count_divisible(50, 120, x);
}
main();

Output

 Divisible by  4  in range of [ 2 - 38 ]are : [  4    8    12    16    20    24    28    32    36  ]
 Result :  9

 Divisible by  3  in range of [ 1 - 10 ]are : [  3    6    9  ]
 Result :  3

 Divisible by  13  in range of [ 21 - 33 ]are : [  26  ]
 Result :  1

 Divisible by  7  in range of [ 7 - 29 ]are : [  7    14    21    28  ]
 Result :  4

 Divisible by  2  in range of [ 0 - 0 ]are : [  0  ]
 Result :  1

 Divisible by  6  in range of [ 50 - 120 ]are : [  54    60    66    72    78    84    90    96    102    108    114    120  ]
 Result :  12

Result Explanation

For the given test cases, the program correctly identifies the numbers divisible by 'x' within the specified range and outputs both the list of divisible numbers and the count. The output explanation has been provided in the initial response.

Time Complexity

The time complexity of the provided code is O((last - start) / x), where 'last' is the ending number, 'start' is the starting number, and 'x' is the divisor. This is because the loop iterates through the range of numbers from 'start' to 'last', but in increments of 'x'. The number of iterations depends on how many numbers in this range are divisible by 'x'. The division factor 'x' reduces the number of iterations compared to iterating through every number in the range.

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