🧠
Brute Force (Iterative GCD & LCM Calculation)
💡 This approach exists to build intuition about what GCD and LCM mean by directly checking divisors and multiples. It is slow but conceptually straightforward.
Intuition
To find the GCD, check all numbers from min(a, b) down to 1 to find the largest divisor of both. For LCM, check multiples of max(a, b) upwards until one is divisible by both.
Algorithm
- Find the minimum of a and b.
- Iterate downwards from min(a, b) to 1 to find the largest number dividing both a and b (GCD).
- Find the maximum of a and b.
- Iterate upwards from max(a, b) to a*b to find the smallest number divisible by both a and b (LCM).
💡 This approach is easy to understand but inefficient, especially for large inputs, because it checks many unnecessary candidates.
def gcd_brute_force(a, b):
gcd = 1
for i in range(min(a, b), 0, -1):
if a % i == 0 and b % i == 0:
gcd = i
break
return gcd
def lcm_brute_force(a, b):
lcm = max(a, b)
while True:
if lcm % a == 0 and lcm % b == 0:
return lcm
lcm += 1
if __name__ == '__main__':
a, b = 12, 18
print("GCD =", gcd_brute_force(a, b))
print("LCM =", lcm_brute_force(a, b))
Line Notes
for i in range(min(a, b), 0, -1):Iterate downwards from the smaller number to find the largest common divisor
if a % i == 0 and b % i == 0:Check if i divides both numbers exactly
while True:Start an infinite loop to find the LCM by checking multiples
if lcm % a == 0 and lcm % b == 0:Check if current lcm candidate is divisible by both numbers
public class GcdLcmBruteForce {
public static int gcd(int a, int b) {
int gcd = 1;
for (int i = Math.min(a, b); i >= 1; i--) {
if (a % i == 0 && b % i == 0) {
gcd = i;
break;
}
}
return gcd;
}
public static int lcm(int a, int b) {
int lcm = Math.max(a, b);
while (true) {
if (lcm % a == 0 && lcm % b == 0) {
return lcm;
}
lcm++;
}
}
public static void main(String[] args) {
int a = 12, b = 18;
System.out.println("GCD = " + gcd(a, b));
System.out.println("LCM = " + lcm(a, b));
}
}
Line Notes
for (int i = Math.min(a, b); i >= 1; i--) {Loop downwards from smaller number to find GCD
if (a % i == 0 && b % i == 0) {Check if i divides both numbers
while (true) {Infinite loop to find LCM by checking multiples
if (lcm % a == 0 && lcm % b == 0) {Check if current candidate is divisible by both
#include <iostream>
#include <algorithm>
using namespace std;
int gcd_brute_force(int a, int b) {
int gcd = 1;
for (int i = min(a, b); i >= 1; i--) {
if (a % i == 0 && b % i == 0) {
gcd = i;
break;
}
}
return gcd;
}
int lcm_brute_force(int a, int b) {
int lcm = max(a, b);
while (true) {
if (lcm % a == 0 && lcm % b == 0) {
return lcm;
}
lcm++;
}
}
int main() {
int a = 12, b = 18;
cout << "GCD = " << gcd_brute_force(a, b) << endl;
cout << "LCM = " << lcm_brute_force(a, b) << endl;
return 0;
}
Line Notes
for (int i = min(a, b); i >= 1; i--) {Loop downwards from smaller number to find GCD
if (a % i == 0 && b % i == 0) {Check if i divides both numbers
while (true) {Infinite loop to find LCM by checking multiples
if (lcm % a == 0 && lcm % b == 0) {Check if current candidate is divisible by both
function gcdBruteForce(a, b) {
let gcd = 1;
for (let i = Math.min(a, b); i >= 1; i--) {
if (a % i === 0 && b % i === 0) {
gcd = i;
break;
}
}
return gcd;
}
function lcmBruteForce(a, b) {
let lcm = Math.max(a, b);
while (true) {
if (lcm % a === 0 && lcm % b === 0) {
return lcm;
}
lcm++;
}
}
// Example usage
const a = 12, b = 18;
console.log("GCD =", gcdBruteForce(a, b));
console.log("LCM =", lcmBruteForce(a, b));
Line Notes
for (let i = Math.min(a, b); i >= 1; i--) {Loop downwards from smaller number to find GCD
if (a % i === 0 && b % i === 0) {Check if i divides both numbers
while (true) {Infinite loop to find LCM by checking multiples
if (lcm % a === 0 && lcm % b === 0) {Check if current candidate is divisible by both
TimeO(min(a,b) + (a*b - max(a,b)))
SpaceO(1)
GCD loop runs up to min(a,b), LCM loop can run up to a*b in worst case, which is very large.
💡 For a=12 and b=18, this means up to 12 iterations for GCD and potentially many for LCM, which is inefficient for large inputs.
Interview Verdict: TLE for large inputs
This approach is too slow for large inputs but helps understand the problem and verify correctness on small inputs.