Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: greatest common factor

by TheAmigo (Initiate)
on Apr 16, 2015 at 19:51 UTC ( [id://1123659]=note: print w/replies, xml ) Need Help??


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;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1123659]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-04-20 10:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found