Author Topic: Greatest Common Factor in TorqueScript?  (Read 582 times)

Using my resources, how do I get the Greatest Common Factor?

The function uses 2 arguments.

Here's an untested port from a c++ program a did a while back

Code: [Select]
function gcf(%a,%b)
{
    for(%i=1;%i<=%a;%i++)
    {
        if(%a % %i == 0 && %b % %i == 0)
            %gcf = %i;
    }
    return %gcf;
}
« Last Edit: March 19, 2012, 12:16:55 AM by Headcrab Zombie »

you're missing a few % signs in front of the i's, but besides that it looks like it should work
although i don't think you need the && %i > %gcf in the if statement, it should always return true

Fixed that.
Not sure what I was thinking with the last part