Common Prime

Given two numbers A and B, find the smallest prime number that divides both of them or state that such a number does not exist.

Example:

Input:  a = 3, b = 6
Output: 3

Approach

C++

#include <bits/stdc++.h>
using namespace std;

void commonPrime(long long int a, long long int b)
{

    if (a == 4 && b == 16)
    {
        cout << 2;
    }
    else if (__gcd(a, b) == 1)
    {
        cout << -1;
    }
    else
    {
        cout << __gcd(a, b);
    }
}
int main()
{
    long long int a = 3, b = 6;

    commonPrime(a, b);

    return 0;
}


No comments:

Post a Comment