A positive integer d is a common divisor of a and b when it divides both numbers with remainder 0. The largest common divisor is the GCD (greatest common divisor).
Remember
d is a common divisor of a and b
⇔ a % d == 0 and b % d == 0
Key fact:
common divisors of (a, b) = all divisors of gcd(a, b)
Example: a = 12, b = 18
gcd = 6
common divisors = 1, 2, 3, 6
a
b
Common divisors
GCD
12
18
1, 2, 3, 6
6
7
13
1
1
24
36
1, 2, 3, 4, 6, 12
12
Approach
How to Find Common Divisors in JavaScript
Four clear steps — then pick a basic loop, a GCD scan, or sqrt pairs.
Take absolute values of a and b (divisors are about magnitude).
Reject the case where both values are 0 (no finite list).
Either scan 1 … min(a, b), or compute gcd and list its divisors.
Collect and print the common divisors (and optionally the GCD).
Method
Idea
Time
Best for
Naive loop
Test every i from 1 to min(a, b)
O(min)
Interviews, small inputs
GCD + divisors
Find g = gcd(a, b), then list divisors of g
O(log min + g)
Clearer math
GCD + sqrt pairs
Walk to √g; collect factor pairs
O(log min + √g)
Larger GCDs
Pseudocode
Pseudocode
function CommonDivisors(a, b):
a = abs(a)
b = abs(b)
if a == 0 and b == 0:
reject input
limit = min(a, b) // if one is 0, use the other
if limit == 0:
limit = max(a, b)
for i from 1 to limit:
if a mod i == 0 and b mod i == 0:
print i
Reference
Cheat Sheet
Keep these facts handy while writing the program.
Topic
Detail
Test
a % d == 0 && b % d == 0
Upper bound
Scan only to min(|a|, |b|)
GCD shortcut
Common divisors = divisors of gcd(a, b)
Always included
1 (unless both are 0)
Largest
gcd(a, b) itself
Classic check
12, 18 → 1, 2, 3, 6
Try it
Live Preview
Enter two integers to see their GCD and every common divisor.
Whole numbers (absolute values used). Tap a chip or type a pair.
Result
a = 12, b = 18
gcd(12, 18) = 6
Common divisors: 1, 2, 3, 6
Count = 4
Code
JavaScript Programs
Start with the readable loop to min(a, b). Then use GCD plus a divisor scan, and finish with sqrt factor pairs. Each example has a Try it Yourself editor.
Example 1 — Simple Loop
Test every integer from 1 to min(a, b) and collect those that divide both.
JavaScript
function commonDivisorsNaive(a, b) {
a = Math.abs(a);
b = Math.abs(b);
if (a === 0 && b === 0) {
return [];
}
let limit = Math.min(a, b);
if (limit === 0) {
limit = Math.max(a, b);
}
const ans = [];
for (let i = 1; i <= limit; i++) {
if (a % i === 0 && b % i === 0) {
ans.push(i);
}
}
return ans;
}
const a = 24;
const b = 36;
const list = commonDivisorsNaive(a, b);
console.log("Common divisors of " + a + " and " + b + " are: " + list.join(", "));
Common divisors of 24 and 36 are: 1, 2, 3, 4, 6, 12
How It Works
Take absolute values with Math.abs so negative inputs behave like positives.
If both are 0, return an empty list — there is no finite set of common divisors.
Set limit to the smaller magnitude (or the nonzero value if one input is 0).
For each i from 1 to limit, push i only when both remainders are 0.
For 24 and 36, the kept values are 1, 2, 3, 4, 6, 12.
Example 2 — GCD Method
Compute gcd(a, b), then list every divisor of that GCD.
JavaScript
function gcdNonneg(x, y) {
while (y !== 0) {
const t = x % y;
x = y;
y = t;
}
return x;
}
function commonDivisorsViaGcd(a, b) {
a = Math.abs(a);
b = Math.abs(b);
if (a === 0 && b === 0) {
return [];
}
const g = gcdNonneg(a, b);
const ans = [];
for (let i = 1; i <= g; i++) {
if (g % i === 0) {
ans.push(i);
}
}
return ans;
}
const a = -12;
const b = 18;
const list = commonDivisorsViaGcd(a, b);
console.log("Common divisors of " + a + " and " + b + " are: " + list.join(", "));
gcdNonneg uses the Euclidean algorithm until the remainder is 0.
For 12 and 18, that yields g = 6 (signs ignored via Math.abs).
Every common divisor must divide 6, so it is enough to list divisors of g.
Loop i from 1 to g and push i when g % i === 0.
Result is sorted automatically: 1, 2, 3, 6.
Example 3 — Sqrt Factor Pairs
After GCD, walk only to √g and collect each factor pair — faster for larger GCDs.
JavaScript
function gcdNonneg(x, y) {
while (y !== 0) {
const t = x % y;
x = y;
y = t;
}
return x;
}
function commonDivisorsSqrt(a, b) {
a = Math.abs(a);
b = Math.abs(b);
if (a === 0 && b === 0) {
return [];
}
const g = gcdNonneg(a, b);
const small = [];
const large = [];
let i = 1;
while (i * i <= g) {
if (g % i === 0) {
small.push(i);
const other = Math.floor(g / i);
if (other !== i) {
large.push(other);
}
}
i += 1;
}
return small.concat(large.reverse());
}
const a = 24;
const b = 36;
const list = commonDivisorsSqrt(a, b);
console.log("Common divisors of " + a + " and " + b + " are: " + list.join(", "));
Common divisors of 24 and 36 are: 1, 2, 3, 4, 6, 12
How It Works
Compute g = gcd(|a|, |b|) as before.
Loop i while i * i <= g — only up to the square root.
When i divides g, push i into small and g / i into large (skip duplicates when equal).
Concatenate small with large reversed for ascending order.
Same answer as the full scan, in O(√g) after the GCD.
Edge Cases & Pitfalls
Check these before calling the solution done.
Both 0
No finite list
Reject or return empty. Do not loop forever.
One 0
Use the nonzero value
Common divisors are exactly the divisors of the nonzero number.
Negatives
Take Math.abs
Positive divisors depend on magnitude, not sign.
Coprime
Only 1
Pairs like 7 and 13 share only 1.
Analysis
Time and Space Complexity
Program
Time
Extra space
Naive loop to min(a, b)
O(min(a, b))
O(1) beyond the answer list
GCD + scan 1…g
O(log min + g)
O(1) beyond the answer list
GCD + sqrt pairs
O(log min + √g)
O(1) beyond the answer list
g is gcd(|a|, |b|).
Remember
Key Takeaways
Definition:d divides both a and b.
GCD shortcut: common divisors = all divisors of gcd(a, b).
Three loops: naive to min, scan 1…g, or sqrt pairs.
Test set:12, 18 → 1 2 3 6; 7, 13 → 1.
One line: values that divide both numbers — or simply every divisor of their GCD.
Frequently Asked Questions
A positive integer d is a common divisor of a and b when a % d == 0 and b % d == 0. For 12 and 18, the common divisors are 1, 2, 3, and 6.
Loop from 1 to min(a, b) and collect values that divide both, or compute gcd(a, b) and list the divisors of that gcd.
Every common divisor of a and b divides gcd(a, b), and every divisor of gcd(a, b) is a common divisor. So listing divisors of the gcd is enough.
Yes for any pair that is not both zero. The number 1 divides every integer.
The naive loop is O(min(a, b)). Computing gcd is O(log min), then listing its divisors is O(g) with a simple 1..g scan, or O(sqrt(g)) with the pair method.
Use the Try it Yourself links under each code sample — they open an in-browser editor with the same logic so you can edit the input and Run.