#### JFitch

##### Member

**GM Version**: GM Studio 1.4

**Target Platform**: ALL

**Download**: N/A

**Links**: N/A

**Summary**:

This tutorial gives a script for calculating the Greatest Common Factor of two numbers.

**Tutorial**:

The simplest way to calculate the greatest common factor of two numbers is to replace the larger number with the remainder when it's divided by the smaller number, and continue to do that until there's no remainder. With this script, you can just use gcf(a,b) to get the greatest common factor of a and b.

Code:

```
var a=abs(argument[0]);
var b=abs(argument[1]);
var new_a;
var new_b;
while (a!=b && a!=0 && b!=0)
{
new_a=a mod b;
new_b=b mod a;
a=new_a;
b=new_b;
}
return max(a,b);
```

Last edited by a moderator: