tags:

views:

54

answers:

2

whats the logic for calculating HCF of given numbers?

+3  A: 

The usual way to calculate the highest common factor, more commonly called the greatest common divisor, is Euclid's algorithm.

If you want to calculate the HCF of more than two numbers, say i1, i2, i3, ..., in, one algorithm is:

res = gcd(i[1], i[2])
for j = 3..n do
    res = gcd(res, i[j])
end
return res
Daniel Trebbien
+2  A: 

Here's an implementation of Euclid's algorithm in C++:

unsigned int hcf(unsigned int a, unsigned int b) {
   if (b == 0) {
      return a;
   } else {
      return hcf(b, a % b);
   }
}
jchl