Say 3 integers are giving: 3, 4, 16
how does one go about writing a program to find the least common denominator? greatest common divisor? etc..

I was wondering if there is a way to use the multiples and then select the number that matches up, in this case 48

I'm writing my program in processing...a java based language

for (int x = 4; x <= 60; x += 4) {
println(x);
}

for (int y = 3; y <= 60; y += 3) {
println(y);
}

for (int z = 16; z <= 60; z += 16) {
println(z);
}

now what to do?

Say 3 integers are giving: 3, 4, 16
how does one go about writing a program to find the least common denominator? greatest common divisor? etc..

I was wondering if there is a way to use the multiples and then select the number that matches up, in this case 48

## All 4 Replies

Say 3 integers are giving: 3, 4, 16
how does one go about writing a program to find the least common denominator? greatest common divisor? etc..

I was wondering if there is a way to use the multiples and then select the number that matches up, in this case 48

I'm writing my program in processing...a java based language

for (int x = 4; x <= 60; x += 4) {
println(x);
}

for (int y = 3; y <= 60; y += 3) {
println(y);
}

for (int z = 16; z <= 60; z += 16) {
println(z);
}

now what to do?

To find the least common denominator or anything like it I would have a variable of type integer called "leastDeno" or something similar and initialise it to that number.

For example - 16 could be the initial value for it as its the smallest number that could potentially be the LCD, even though we know 3 doesn't multiply into it.

You will have to use an if statement obviously; in order to compare each value together EDIT: Oops. You meant LCM - lowest common denominator. In that case, still, use mod command (%). It will let you see if a number is divisible.

If a%b=0, then a is a multiple of b.

it's actually least common denominator...you could say least common multiple i guess...i'm aware of the modulus % finding a remainder... here is something i found online...but let me know what you think...it's in java but i work in processing so it's similar but different...concepts are the same i believe,

function gcd(a, b){
// Euclidean algorithm
var t;
while (b != 0){
t = b;
b = a % b;
a = t;
}
return a;
}

function lcm(a, b){
return (a * b / gcd(a, b));
}

function lcmm(args){
// Recursively iterate through pairs of arguments
// i.e. lcm(args, lcm(args, lcm(args, args)))

if(args.length == 2){
return lcm(args, args);
} else {
var arg0 = args;
args.shift();
return lcm(arg0, lcmm(args));
}
} Sorry about the LCM acronym confusion. Yeah, I confused myself too.

Yes, what the above code segment is referring to is Euclid's algorithm to find GCF/GCD.

The algorithm is as follows: the GCF of a and b is equivalent to the GCF of b and a%b, which of course is a recursive function. Eventually, a%b=0, in which case b is the GCF.

The LCM can be calculated using the GCF. The LCM of a and b is abs(a times b)/gcf(a,b)

$$gcf(a,b)=gcf(b, a%b) \\ lcm(a,b)=\frac{|a\cdot b|}{gcf(a,b)}$$

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.