Question: Consider the following code:

```
\documentclass{article}

\begin{document}

\noindent
Can I make \LaTeX{} reduce a fraction automatically?\\[\baselineskip]
For example, I would like the fraction
\[ \frac{278\,922}{74\,088} \]
to be reduced to
\[ \frac{6641}{1764} \]

\end{document}

```

![output](https://i.stack.imgur.com/wUiA2.jpg)

P.S. The numerator and denominator are always both natural numbers in my case.

Answer: 
An option using Lua+LaTeX.

Made small improvement. Made a Lua function to be called as a LaTeX command, with the numerator and denominator passed as arguments, instead of hardcoding the values in as before. The command is `\simplify{a}{b}`:

```
\documentclass{article}
\usepackage{luacode}
\usepackage{amsmath}    
%------------------------
\begin{luacode}
function simplify(a,b)     
  local function gcd(a,b)   
    if b ~= 0 then
        return gcd(b, a % b)
    else
        return math.abs(a)
    end
  end

  t = gcd(a, b)
  tex.print("\\frac{"..a/t.."}{"..b/t.."}")
end    
\end{luacode}
\newcommand\simplify[2]{\directlua{simplify(#1,#2) }}%
%-------------------    
\begin{document}    
\noindent Can I make \LaTeX{} reduce a fraction automatically?\\[\baselineskip]
For example, I would like the fraction
\begin{equation*}
  \frac{278\,922}{74\,088}
\end{equation*}

to be reduced to

\begin{equation*}
  \simplify{278922}{74088}
\end{equation*}    
\end{document}

```

**Original answer**

```
\documentclass{article}
\usepackage{luacode}
\usepackage{amsmath}

\begin{document}

\noindent Can I make \LaTeX{} reduce a fraction automatically?\\[\baselineskip]
For example, I would like the fraction
\begin{equation*}
  \frac{278\,922}{74\,088}
\end{equation*}
to be reduced to
%------------------------------------
\begin{luacode*}
function gcd(a,b)
    if b ~= 0 then
        return gcd(b, a % b)
    else
        return math.abs(a)
    end
end
u = 278922
v = 74088
t = gcd(v, u)
tex.print("\\begin{equation*}")
tex.print("  \\frac{"..u/t.."}{"..v/t.."}")
tex.print("\\end{equation*}")
\end{luacode*}
%------------------------------------

\end{document}

```

`lualatex foo.tex` gives

![Mathematica graphics](https://i.stack.imgur.com/ya9hd.png)

references <http://rosettacode.org/wiki/Greatest_common_divisor#Lua> and <http://www.lua.org/manual/5.3/manual.html>
There's already another LuaTeX answer, but IMO it doesn't handle properly with locality and integer division. So here, and taking advantage of the fact Lua 5.3+ [includes bitwise operators](https://www.lua.org/manual/5.3/manual.html#3.4.2), a different approach using a [binary GCD algorithm](https://xlinux.nist.gov/dads/HTML/binaryGCD.html):

```
\documentclass{standalone}
%\usepackage{amsmath}
\usepackage{luacode}
\begin{luacode*}
userdata = userdata or {}

--https://xlinux.nist.gov/dads/HTML/binaryGCD.html
userdata.gcd = function (u,v)
    --To handle with negative values
    local u, v, g = math.abs(u), math.abs(v), 1
    --Nice error message
    assert(v~=0, "Denominator cannot be zero")
    while u&1==0 and v&1==0 do
        u=u>>1
        v=v>>1
        g=g<<1
    end
    while u>0 do
        if u&1==0 then 
            u=u>>1
        elseif v&1==0 then
            v=v>>1
        else 
            local t = math.abs(u-v)>>1
            if u<v then v=t else u=t end
        end
    end
    return v*g
end

userdata.simplified = function(u,v)
    local gcd = userdata.gcd(u,v)
    tex.sprint(("\\frac{%d}{%d}")
    :format(u//gcd, v//gcd))
end

\end{luacode*}
\newcommand\simplified[2]{\directlua{userdata.simplified(#1,#2)}}
\begin{document}
$\displaystyle\simplified{278922}{74088}$
\end{document}

```

[![enter image description here](https://i.stack.imgur.com/MuxMU.png)](https://i.stack.imgur.com/MuxMU.png)

The binary algorithm is slightly faster, but that's only noticeable when simplified fractions are extensively used in a document.