To simplify a fraction, the two numbers must be divided by their GCD. The following script calculates the GCD using the Euclidean Algorithm.

Script to Simplify Fractions

define simplify fraction (numerator) (denominator)
GCD (numerator) (denominator) // calculates GCD of numbers
set [numerator v] to ((numerator) / (GCD)) // divides numerator by GCD
set [denominator v] to ((denominator) / (GCD)) // divides denominator by GCD

define GCD (a) (b) // euclidean algorithm for GCD
if <(b) = (0)> then // if b = 0
set [GCD v] to (a) // stop and return value
else
GCD (b) ((a) mod (b)) // return equal GCD with smaller numbers
end

Example Use Cases

when gf clicked
simplify fraction (36) (48) :: custom
say (join (numerator) (join [/] (denominator))) // 3/4
when gf clicked
simplify fraction (2) (10) :: custom
say (join (numerator) (join [/] (denominator))) // 1/5

Simplify Fraction algorithm

The simplify fraction () () :: custom block simplifies a fraction by calculating the GCDs with GCD () () :: custom and divides the numerator and denominator with the resulting GCD.

GCD algorithm

The GCD uses the Euclidean algorithm. The key insight is that (b :: custom) and ((a::custom) mod (b::custom)) has the same GCD as (a :: custom) and (b :: custom). The numbers produced by (b :: custom) and ((a::custom) mod (b::custom)) get smaller and smaller each time the operation is applied. Eventually (b::custom) will be equal to 0 and the process will stop.

Cookies help us deliver our services. By using our services, you agree to our use of cookies.