Count of common multiples of two numbers in a range
Here given code implementation process.
// C program
// Count of common multiples of two numbers in a range
#include <stdio.h>
// Count all multiples numbers of x and y in given range
void count_multiples(int start, int last, int x, int y)
{
if (start > last)
{
//Change sequence
count_multiples(last, start, x, y);
}
else
{
//Display calculated result
printf("\n Multiples by (%d,%d) in range of [%d-%d] are \n [", x, y, start, last);
int common = x * y;
int num = 0;
int counter = 0;
if (common > start)
{
//When multiplier is higher to first element of range
num = common;
}
else if (start % common == 0)
{
//When range first element is multiplier
num = start;
}
else
{
//Find the closest multiplier of (x and y) to starting number
num = common + ((start / common) * (common));
}
while (num <= last)
{
if (num % common == 0)
{
//When x*y are multiples of num
printf(" %d ", num);
counter++;
}
// visit to next multiplier
num += common;
}
//Display calculated result
printf("]\n Counter : %d\n", counter);
}
}
int main()
{
//Test case
int x = 5;
int y = 5;
count_multiples(50, 250, x, y);
x = 4;
y = 3;
count_multiples(0, 50, x, y);
x = 3;
y = 2;
count_multiples(50, 150, x, y);
x = 2;
y = 5;
count_multiples(1, 100, x, y);
x = 4;
y = 3;
count_multiples(1, 12, x, y);
return 0;
}
Output
Multiples by (5,5) in range of [50-250] are
[ 50 75 100 125 150 175 200 225 250 ]
Counter : 9
Multiples by (4,3) in range of [0-50] are
[ 12 24 36 48 ]
Counter : 4
Multiples by (3,2) in range of [50-150] are
[ 54 60 66 72 78 84 90 96 102 108 114 120 126 132 138 144 150 ]
Counter : 17
Multiples by (2,5) in range of [1-100] are
[ 10 20 30 40 50 60 70 80 90 100 ]
Counter : 10
Multiples by (4,3) in range of [1-12] are
[ 12 ]
Counter : 1
/*
Java program
Count of common multiples of two numbers in a range
*/
class Multiplier
{
// Count all multiples numbers of x and y in given range
public void count_multiples(int start, int last, int x, int y)
{
if (start > last)
{
//Change sequence
count_multiples(last, start, x, y);
}
else
{
//Display calculated result
System.out.print("\n Multiples by (" + x + "," + y + ") in range of [" + start + "-" + last + "] are \n [");
int common = x * y;
int num = 0;
int counter = 0;
if (common > start)
{
//When multiplier is higher to first element of range
num = common;
}
else if (start % common == 0)
{
//When range first element is multiplier
num = start;
}
else
{
//Find the closest multiplier of (x and y) to starting number
num = common + ((start / common) * (common));
}
while (num <= last)
{
if (num % common == 0)
{
//When x*y are multiples of num
System.out.print(" " + num + " ");
counter++;
}
// visit to next multiplier
num += common;
}
//Display calculated result
System.out.print("]\n Counter : " + counter + "\n");
}
}
public static void main(String[] args)
{
Multiplier obj = new Multiplier();
//Test case
int x = 5;
int y = 5;
obj.count_multiples(50, 250, x, y);
x = 4;
y = 3;
obj.count_multiples(0, 50, x, y);
x = 3;
y = 2;
obj.count_multiples(50, 150, x, y);
x = 2;
y = 5;
obj.count_multiples(1, 100, x, y);
x = 4;
y = 3;
obj.count_multiples(1, 12, x, y);
}
}
Output
Multiples by (5,5) in range of [50-250] are
[ 50 75 100 125 150 175 200 225 250 ]
Counter : 9
Multiples by (4,3) in range of [0-50] are
[ 12 24 36 48 ]
Counter : 4
Multiples by (3,2) in range of [50-150] are
[ 54 60 66 72 78 84 90 96 102 108 114 120 126 132 138 144 150 ]
Counter : 17
Multiples by (2,5) in range of [1-100] are
[ 10 20 30 40 50 60 70 80 90 100 ]
Counter : 10
Multiples by (4,3) in range of [1-12] are
[ 12 ]
Counter : 1
//Include header file
#include <iostream>
using namespace std;
/*
C++ program
Count of common multiples of two numbers in a range
*/
class Multiplier
{
public:
// Count all multiples numbers of x and y in given range
void count_multiples(int start, int last, int x, int y)
{
if (start > last)
{
//Change sequence
this->count_multiples(last, start, x, y);
}
else
{
//Display calculated result
cout << "\n Multiples by (" << x << "," << y << ") in range of [" << start << "-" << last << "] are \n [";
int common = x *y;
int num = 0;
int counter = 0;
if (common > start)
{
//When multiplier is higher to first element of range
num = common;
}
else if (start % common == 0)
{
//When range first element is multiplier
num = start;
}
else
{
//Find the closest multiplier of (x and y) to starting number
num = common + ((start / common) *(common));
}
while (num <= last)
{
if (num % common == 0)
{
//When x*y are multiples of num
cout << " " << num << " ";
counter++;
}
// visit to next multiplier
num += common;
}
//Display calculated result
cout << "]\n Counter : " << counter << "\n";
}
}
};
int main()
{
Multiplier obj = Multiplier();
//Test case
int x = 5;
int y = 5;
obj.count_multiples(50, 250, x, y);
x = 4;
y = 3;
obj.count_multiples(0, 50, x, y);
x = 3;
y = 2;
obj.count_multiples(50, 150, x, y);
x = 2;
y = 5;
obj.count_multiples(1, 100, x, y);
x = 4;
y = 3;
obj.count_multiples(1, 12, x, y);
return 0;
}
Output
Multiples by (5,5) in range of [50-250] are
[ 50 75 100 125 150 175 200 225 250 ]
Counter : 9
Multiples by (4,3) in range of [0-50] are
[ 12 24 36 48 ]
Counter : 4
Multiples by (3,2) in range of [50-150] are
[ 54 60 66 72 78 84 90 96 102 108 114 120 126 132 138 144 150 ]
Counter : 17
Multiples by (2,5) in range of [1-100] are
[ 10 20 30 40 50 60 70 80 90 100 ]
Counter : 10
Multiples by (4,3) in range of [1-12] are
[ 12 ]
Counter : 1
//Include namespace system
using System;
/*
C# program
Count of common multiples of two numbers in a range
*/
class Multiplier
{
// Count all multiples numbers of x and y in given range
public void count_multiples(int start, int last, int x, int y)
{
if (start > last)
{
//Change sequence
count_multiples(last, start, x, y);
}
else
{
//Display calculated result
Console.Write("\n Multiples by (" + x + "," + y + ") in range of [" + start + "-" + last + "] are \n [");
int common = x * y;
int num = 0;
int counter = 0;
if (common > start)
{
//When multiplier is higher to first element of range
num = common;
}
else if (start % common == 0)
{
//When range first element is multiplier
num = start;
}
else
{
//Find the closest multiplier of (x and y) to starting number
num = common + ((start / common) * (common));
}
while (num <= last)
{
if (num % common == 0)
{
//When x*y are multiples of num
Console.Write(" " + num + " ");
counter++;
}
// visit to next multiplier
num += common;
}
//Display calculated result
Console.Write("]\n Counter : " + counter + "\n");
}
}
public static void Main(String[] args)
{
Multiplier obj = new Multiplier();
//Test case
int x = 5;
int y = 5;
obj.count_multiples(50, 250, x, y);
x = 4;
y = 3;
obj.count_multiples(0, 50, x, y);
x = 3;
y = 2;
obj.count_multiples(50, 150, x, y);
x = 2;
y = 5;
obj.count_multiples(1, 100, x, y);
x = 4;
y = 3;
obj.count_multiples(1, 12, x, y);
}
}
Output
Multiples by (5,5) in range of [50-250] are
[ 50 75 100 125 150 175 200 225 250 ]
Counter : 9
Multiples by (4,3) in range of [0-50] are
[ 12 24 36 48 ]
Counter : 4
Multiples by (3,2) in range of [50-150] are
[ 54 60 66 72 78 84 90 96 102 108 114 120 126 132 138 144 150 ]
Counter : 17
Multiples by (2,5) in range of [1-100] are
[ 10 20 30 40 50 60 70 80 90 100 ]
Counter : 10
Multiples by (4,3) in range of [1-12] are
[ 12 ]
Counter : 1
<?php
/*
Php program
Count of common multiples of two numbers in a range
*/
class Multiplier
{
// Count all multiples numbers of x and y in given range
public function count_multiples($start, $last, $x, $y)
{
if ($start > $last)
{
//Change sequence
$this->count_multiples($last, $start, $x, $y);
}
else
{
//Display calculated result
echo "\n Multiples by (". $x .",". $y .") in range of [". $start ."-". $last ."] are \n [";
$common = $x * $y;
$num = 0;
$counter = 0;
if ($common > $start)
{
//When multiplier is higher to first element of range
$num = $common;
}
else if ($start % $common == 0)
{
//When range first element is multiplier
$num = $start;
}
else
{
//Find the closest multiplier of (x and y) to starting number
$num = $common + ((intval($start / $common)) * ($common));
}
while ($num <= $last)
{
if ($num % $common == 0)
{
//When x*y are multiples of num
echo " ". $num ." ";
$counter++;
}
// visit to next multiplier
$num += $common;
}
//Display calculated result
echo "]\n Counter : ". $counter ."\n";
}
}
}
function main()
{
$obj = new Multiplier();
//Test case
$x = 5;
$y = 5;
$obj->count_multiples(50, 250, $x, $y);
$x = 4;
$y = 3;
$obj->count_multiples(0, 50, $x, $y);
$x = 3;
$y = 2;
$obj->count_multiples(50, 150, $x, $y);
$x = 2;
$y = 5;
$obj->count_multiples(1, 100, $x, $y);
$x = 4;
$y = 3;
$obj->count_multiples(1, 12, $x, $y);
}
main();
Output
Multiples by (5,5) in range of [50-250] are
[ 50 75 100 125 150 175 200 225 250 ]
Counter : 9
Multiples by (4,3) in range of [0-50] are
[ 12 24 36 48 ]
Counter : 4
Multiples by (3,2) in range of [50-150] are
[ 54 60 66 72 78 84 90 96 102 108 114 120 126 132 138 144 150 ]
Counter : 17
Multiples by (2,5) in range of [1-100] are
[ 10 20 30 40 50 60 70 80 90 100 ]
Counter : 10
Multiples by (4,3) in range of [1-12] are
[ 12 ]
Counter : 1
/*
Node Js program
Count of common multiples of two numbers in a range
*/
class Multiplier
{
// Count all multiples numbers of x and y in given range
count_multiples(start, last, x, y)
{
if (start > last)
{
//Change sequence
this.count_multiples(last, start, x, y);
}
else
{
//Display calculated result
process.stdout.write("\n Multiples by (" + x + "," + y + ") in range of [" + start + "-" + last + "] are \n [");
var common = x * y;
var num = 0;
var counter = 0;
if (common > start)
{
//When multiplier is higher to first element of range
num = common;
}
else if (start % common == 0)
{
//When range first element is multiplier
num = start;
}
else
{
//Find the closest multiplier of (x and y) to starting number
num = common + ((parseInt(start / common)) * (common));
}
while (num <= last)
{
if (num % common == 0)
{
//When x*y are multiples of num
process.stdout.write(" " + num + " ");
counter++;
}
// visit to next multiplier
num += common;
}
//Display calculated result
process.stdout.write("]\n Counter : " + counter + "\n");
}
}
}
function main()
{
var obj = new Multiplier();
//Test case
var x = 5;
var y = 5;
obj.count_multiples(50, 250, x, y);
x = 4;
y = 3;
obj.count_multiples(0, 50, x, y);
x = 3;
y = 2;
obj.count_multiples(50, 150, x, y);
x = 2;
y = 5;
obj.count_multiples(1, 100, x, y);
x = 4;
y = 3;
obj.count_multiples(1, 12, x, y);
}
main();
Output
Multiples by (5,5) in range of [50-250] are
[ 50 75 100 125 150 175 200 225 250 ]
Counter : 9
Multiples by (4,3) in range of [0-50] are
[ 12 24 36 48 ]
Counter : 4
Multiples by (3,2) in range of [50-150] are
[ 54 60 66 72 78 84 90 96 102 108 114 120 126 132 138 144 150 ]
Counter : 17
Multiples by (2,5) in range of [1-100] are
[ 10 20 30 40 50 60 70 80 90 100 ]
Counter : 10
Multiples by (4,3) in range of [1-12] are
[ 12 ]
Counter : 1
# Python 3 program
# Count of common multiples of two numbers in a range
class Multiplier :
# Count all multiples numbers of x and y in given range
def count_multiples(self, start, last, x, y) :
if (start > last) :
# Change sequence
self.count_multiples(last, start, x, y)
else :
# Display calculated result
print("\n Multiples by (", x ,",", y ,") in range of [", start ,"-", last ,"] are \n [", end = "")
common = x * y
num = 0
counter = 0
if (common > start) :
# When multiplier is higher to first element of range
num = common
elif(start % common == 0) :
# When range first element is multiplier
num = start
else :
# Find the closest multiplier of (x and y) to starting number
num = common + ((int(start / common)) * (common))
while (num <= last) :
if (num % common == 0) :
# When x*y are multiples of num
print(" ", num ," ", end = "")
counter += 1
# visit to next multiplier
num += common
# Display calculated result
print("]\n Counter : ", counter ,"\n", end = "")
def main() :
obj = Multiplier()
# Test case
x = 5
y = 5
obj.count_multiples(50, 250, x, y)
x = 4
y = 3
obj.count_multiples(0, 50, x, y)
x = 3
y = 2
obj.count_multiples(50, 150, x, y)
x = 2
y = 5
obj.count_multiples(1, 100, x, y)
x = 4
y = 3
obj.count_multiples(1, 12, x, y)
if __name__ == "__main__": main()
Output
Multiples by ( 5 , 5 ) in range of [ 50 - 250 ] are
[ 50 75 100 125 150 175 200 225 250 ]
Counter : 9
Multiples by ( 4 , 3 ) in range of [ 0 - 50 ] are
[ 12 24 36 48 ]
Counter : 4
Multiples by ( 3 , 2 ) in range of [ 50 - 150 ] are
[ 54 60 66 72 78 84 90 96 102 108 114 120 126 132 138 144 150 ]
Counter : 17
Multiples by ( 2 , 5 ) in range of [ 1 - 100 ] are
[ 10 20 30 40 50 60 70 80 90 100 ]
Counter : 10
Multiples by ( 4 , 3 ) in range of [ 1 - 12 ] are
[ 12 ]
Counter : 1
# Ruby program
# Count of common multiples of two numbers in a range
class Multiplier
# Count all multiples numbers of x and y in given range
def count_multiples(start, last, x, y)
if (start > last)
# Change sequence
self.count_multiples(last, start, x, y)
else
# Display calculated result
print("\n Multiples by (", x ,",", y ,") in range of [", start ,"-", last ,"] are \n [")
common = x * y
num = 0
counter = 0
if (common > start)
# When multiplier is higher to first element of range
num = common
elsif(start % common == 0)
# When range first element is multiplier
num = start
else
# Find the closest multiplier of (x and y) to starting number
num = common + ((start / common) * (common))
end
while (num <= last)
if (num % common == 0)
# When x*y are multiples of num
print(" ", num ," ")
counter += 1
end
# visit to next multiplier
num += common
end
# Display calculated result
print("]\n Counter : ", counter ,"\n")
end
end
end
def main()
obj = Multiplier.new()
# Test case
x = 5
y = 5
obj.count_multiples(50, 250, x, y)
x = 4
y = 3
obj.count_multiples(0, 50, x, y)
x = 3
y = 2
obj.count_multiples(50, 150, x, y)
x = 2
y = 5
obj.count_multiples(1, 100, x, y)
x = 4
y = 3
obj.count_multiples(1, 12, x, y)
end
main()
Output
Multiples by (5,5) in range of [50-250] are
[ 50 75 100 125 150 175 200 225 250 ]
Counter : 9
Multiples by (4,3) in range of [0-50] are
[ 12 24 36 48 ]
Counter : 4
Multiples by (3,2) in range of [50-150] are
[ 54 60 66 72 78 84 90 96 102 108 114 120 126 132 138 144 150 ]
Counter : 17
Multiples by (2,5) in range of [1-100] are
[ 10 20 30 40 50 60 70 80 90 100 ]
Counter : 10
Multiples by (4,3) in range of [1-12] are
[ 12 ]
Counter : 1
/*
Scala program
Count of common multiples of two numbers in a range
*/
class Multiplier
{
// Count all multiples numbers of x and y in given range
def count_multiples(start: Int, last: Int, x: Int, y: Int): Unit = {
if (start > last)
{
//Change sequence
count_multiples(last, start, x, y);
}
else
{
//Display calculated result
print("\n Multiples by (" + x + "," + y + ") in range of [" + start + "-" + last + "] are \n [");
var common: Int = x * y;
var num: Int = 0;
var counter: Int = 0;
if (common > start)
{
//When multiplier is higher to first element of range
num = common;
}
else if (start % common == 0)
{
//When range first element is multiplier
num = start;
}
else
{
//Find the closest multiplier of (x and y) to starting number
num = common + (((start / common).toInt) * (common));
}
while (num <= last)
{
if (num % common == 0)
{
//When x*y are multiples of num
print(" " + num + " ");
counter += 1;
}
// visit to next multiplier
num += common;
}
//Display calculated result
print("]\n Counter : " + counter + "\n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: Multiplier = new Multiplier();
//Test case
var x: Int = 5;
var y: Int = 5;
obj.count_multiples(50, 250, x, y);
x = 4;
y = 3;
obj.count_multiples(0, 50, x, y);
x = 3;
y = 2;
obj.count_multiples(50, 150, x, y);
x = 2;
y = 5;
obj.count_multiples(1, 100, x, y);
x = 4;
y = 3;
obj.count_multiples(1, 12, x, y);
}
}
Output
Multiples by (5,5) in range of [50-250] are
[ 50 75 100 125 150 175 200 225 250 ]
Counter : 9
Multiples by (4,3) in range of [0-50] are
[ 12 24 36 48 ]
Counter : 4
Multiples by (3,2) in range of [50-150] are
[ 54 60 66 72 78 84 90 96 102 108 114 120 126 132 138 144 150 ]
Counter : 17
Multiples by (2,5) in range of [1-100] are
[ 10 20 30 40 50 60 70 80 90 100 ]
Counter : 10
Multiples by (4,3) in range of [1-12] are
[ 12 ]
Counter : 1
/*
Swift 4 program
Count of common multiples of two numbers in a range
*/
class Multiplier
{
// Count all multiples numbers of x and y in given range
func count_multiples(_ start: Int, _ last: Int, _ x: Int, _ y: Int)
{
if (start > last)
{
//Change sequence
self.count_multiples(last, start, x, y);
}
else
{
//Display calculated result
print("\n Multiples by (", x ,",", y ,") in range of [", start ,"-", last ,"]are \n [", terminator: "");
let common: Int = x * y;
var num: Int = 0;
var counter: Int = 0;
if (common > start)
{
//When multiplier is higher to first element of range
num = common;
}
else if (start % common == 0)
{
//When range first element is multiplier
num = start;
}
else
{
//Find the closest multiplier of (x and y) to starting number
num = common + ((start / common) * (common));
}
while (num <= last)
{
if (num % common == 0)
{
//When x*y are multiples of num
print(" ", num ," ", terminator: "");
counter += 1;
}
// visit to next multiplier
num += common;
}
//Display calculated result
print("]\n Counter : ", counter ,"\n", terminator: "");
}
}
}
func main()
{
let obj: Multiplier = Multiplier();
//Test case
var x: Int = 5;
var y: Int = 5;
obj.count_multiples(50, 250, x, y);
x = 4;
y = 3;
obj.count_multiples(0, 50, x, y);
x = 3;
y = 2;
obj.count_multiples(50, 150, x, y);
x = 2;
y = 5;
obj.count_multiples(1, 100, x, y);
x = 4;
y = 3;
obj.count_multiples(1, 12, x, y);
}
main();
Output
Multiples by ( 5 , 5 ) in range of [ 50 - 250 ]are
[ 50 75 100 125 150 175 200 225 250 ]
Counter : 9
Multiples by ( 4 , 3 ) in range of [ 0 - 50 ]are
[ 12 24 36 48 ]
Counter : 4
Multiples by ( 3 , 2 ) in range of [ 50 - 150 ]are
[ 54 60 66 72 78 84 90 96 102 108 114 120 126 132 138 144 150 ]
Counter : 17
Multiples by ( 2 , 5 ) in range of [ 1 - 100 ]are
[ 10 20 30 40 50 60 70 80 90 100 ]
Counter : 10
Multiples by ( 4 , 3 ) in range of [ 1 - 12 ]are
[ 12 ]
Counter : 1
// Rust program
// Count of common multiples of two numbers in a range
fn main()
{
//Test case
let mut x: i32 = 5;
let mut y: i32 = 5;
count_multiples(50, 250, x, y);
x = 4;
y = 3;
count_multiples(0, 50, x, y);
x = 3;
y = 2;
count_multiples(50, 150, x, y);
x = 2;
y = 5;
count_multiples(1, 100, x, y);
x = 4;
y = 3;
count_multiples(1, 12, x, y);
}
fn count_multiples(start: i32, last: i32, x: i32, y: i32)
{
if start > last
{
//Change sequence
count_multiples(last, start, x, y);
}
else
{
//Display calculated result
print!("\n Multiples by ({},{}) in range of [{}-{}] are \n [", x, y, start, last);
let common: i32 = x * y;
let mut num: i32 ;
let mut counter: i32 = 0;
if common > start
{
//When multiplier is higher to first element of range
num = common;
}
else if start % common == 0
{
//When range first element is multiplier
num = start;
}
else
{
//Find the closest multiplier of (x and y) to starting number
num = common + ((start / common) * (common));
}
while num <= last
{
if num % common == 0
{
//When x*y are multiples of num
print!(" {} ", num);
counter += 1;
}
// visit to next multiplier
num += common;
}
//Display calculated result
print!("]\n Counter : {}\n", counter);
}
}
Output
Multiples by (5,5) in range of [50-250] are
[ 50 75 100 125 150 175 200 225 250 ]
Counter : 9
Multiples by (4,3) in range of [0-50] are
[ 12 24 36 48 ]
Counter : 4
Multiples by (3,2) in range of [50-150] are
[ 54 60 66 72 78 84 90 96 102 108 114 120 126 132 138 144 150 ]
Counter : 17
Multiples by (2,5) in range of [1-100] are
[ 10 20 30 40 50 60 70 80 90 100 ]
Counter : 10
Multiples by (4,3) in range of [1-12] are
[ 12 ]
Counter : 1
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