Open In App

Check if Two Numbers are Coprime using JavaScript

Last Updated : 27 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

We are going to learn how we can Check if the given two numbers are coprime or not using JavaScript. Co-prime are two integers that have no common factors other than 1. Their greatest common divisor (GCD) is 1. Coprime numbers are also known as relatively prime numbers.

Example:

Input: a = 15 and b = 4 
Output: True (as 15 and 4 have only one common factor 1)

Input: a = 28 and b = 14
Output: False (as 28 and 14 have more than one common factor)

These are the following methods:

Iterative Method

Create the function. Use a for loop to iterate from 2 up to the minimum of a and b. This loop checks all possible divisors starting from 2. Check if both a and b are divisible by the current iteration number i. If they are, it means i is a common factor of a and b, so return false i.e. they are not coprime. If loop finishes without finding any common factors the return true.

Example: To demonstrate checking if two numbers are coprime or not using iterative method.

JavaScript
// The iterative approach

function CoprimeIterative(a, b) {
    
    for (let i = 2; i <= Math.min(a, b); i++) {
        // If both a and b are divisible by i
        // means they have a common factor
        if (a % i === 0 && b % i === 0) {
            return false; 
        }
    }
    return true;
}

const num1 = 21;
const num2 = 28;

console.log(`Are ${num1} and ${num2} coprime?
            ${CoprimeIterative(num1, num2)}`);

Output
Are 21 and 28 coprime?
            false

Time Complexity: O(min(a, b))

Space Complexity: O(1)

Euclidean Algorithm

In this approach, we are using Euclidean Algorithm. Create a function GCD and pass a and b as input parameter. Check if b is equal to 0 then return a. else recursively call GCD with b and the remainder of a divided by b. Create another function to check co-prime and pass a and b as input parameter. Call the gcd function with a and b. Return true if the result of gcd(a, b) is 1, indicating that a and b are coprime. Else return false.

Example: To demonstrate checking if two numbers are coprime or not using Euclidean Algorithm.

JavaScript
// Euclidean algorithm

function gcd(a, b) {
     if (b === 0) {
        return a;
    } else {
        return gcd(b, a % b);
    }
}

function areCoprime(a, b) {
    // If the GCD of two numbers is 1, they are coprime
    return gcd(a, b) === 1;
}


const num1 = 3;
const num2 = 7;

if (areCoprime(num1, num2)) {
    console.log(`${num1} and ${num2} are coprime.`);
} else {
    console.log(`${num1} and ${num2} are not coprime.`);
}

Output
3 and 7 are coprime.

Time Complexity: O(min(a, b))

Space Complexity: O(1)

Using Prime Factorization

In this approach, we find the prime factors of both numbers and check if they have any common prime factors other than 1. If they don't, the numbers are coprime.

Example: This JavaScript code demonstrates how to check if two numbers are coprime using prime factorization.

JavaScript
// Function to get prime factors of a number
function getPrimeFactors(n) {
    const factors = new Set();
    while (n % 2 === 0) {
        factors.add(2);
        n = n / 2;
    }
    for (let i = 3; i <= Math.sqrt(n); i += 2) {
        while (n % i === 0) {
            factors.add(i);
            n = n / i;
        }
    }
    if (n > 2) {
        factors.add(n);
    }
    return factors;
}

// Function to check if two numbers are coprime
function areCoprime(a, b) {
    const factorsA = getPrimeFactors(a);
    const factorsB = getPrimeFactors(b);

    for (let factor of factorsA) {
        if (factorsB.has(factor)) {
            return false; // Common factor found
        }
    }
    return true; // No common factor other than 1
}


const a = 15;
const b = 4;
const result = areCoprime(a, b);
console.log(`Are ${a} and ${b} coprime? ${result}`);


const a2 = 28;
const b2 = 14;
const result2 = areCoprime(a2, b2);
console.log(`Are ${a2} and ${b2} coprime? ${result2}`);

Output
Are 15 and 4 coprime? true
Are 28 and 14 coprime? false

Next Article

Similar Reads