यहां हम देखेंगे कि दो संख्याओं के सामान्य भाजकों की संख्या कैसे प्राप्त की जा सकती है। हम सभी सामान्य भाजक नहीं ढूंढने जा रहे हैं, लेकिन हम गिनेंगे कि कितने सामान्य भाजक हैं। यदि दो संख्याएं 12 और 24 जैसी हैं, तो सामान्य भाजक 1, 2, 3, 4, 6, 12 हैं। तो 6 सामान्य भाजक हैं, इसलिए उत्तर 6 होगा।
एल्गोरिदम
countCommonDivisor(a, b)
begin
count := 0
gcd := gcd of a and b
for i := 1 to square root of gcd, do
if gcd is divisible by i, then
if gcd / i = i, then
count := count + 1
else
count := count + 2
end if
end if
done
return count
end
उदाहरण
#include<iostream>
#include<cmath>
using namespace std;
int gcd(int a, int b) {
if (a == 0)
return b;
return gcd(b%a, a);
}
int countCommonDivisors(int a,int b) {
int gcd_val = gcd(a, b); //a और b का gcd प्राप्त करें
int count = 0;
for (int i=1; i<=sqrt(gcd_val); i++) {
if (gcd_val%i==0) { // जब 'i' n का गुणनखंड है
if (gcd_val/i == i) //यदि दो संख्याएं समान हैं
count += 1;
else
count += 2;
}
}
return count;
}
main() {
int a = 12, b = 24;
cout << "Total common divisors: " << countCommonDivisors(a, b);
}
आउटपुट
Total common divisors: 6