http://qs321.pair.com?node_id=1123659


in reply to greatest common factor

I found this function and it seemed reasonable, but some of my users reported that I was somtimes returning the wrong sign. Turns out that the function above has inconsistent behavior on negative numbers:

gcf(-5, 5) = 5 gcf(5, -5) = -5

when they should really both be 5.

14 years late, but here's a fix. I changed my code so that gcf() ends with

return abs $x;