Reducing fractions automatically using LaTeX 3

Here is a flat LaTeX2e implementation.

\documentclass{article}
\usepackage{amsmath}

\newcount{\numerator}
\newcount{\denominator}
\newcount{\gcd}

% compute \gcd and returns reduced \numerator and \denominator
\newcommand{\reduce}[2]% #1=numerator, #2=denominator
{\numerator=#1\relax
 \denominator=#2\relax
 \loop
 \ifnum\numerator<\denominator
   \advance\denominator by -\numerator
   \gcd=\denominator
 \else
   \advance\numerator by -\denominator
   \gcd=\numerator% swap
 \fi
 \ifnum\gcd>1 \repeat
 \ifnum\gcd=0 \gcd=\denominator\fi
 \numerator=#1\relax
 \divide\numerator by \gcd
 \denominator=#2\relax
 \divide\denominator by \gcd
}

\begin{document}

For example, I would like the fraction
\begin{equation*}
  \frac{278922}{74088}
\end{equation*}
to be reduced to\reduce{278922}{74088}
\begin{equation*}
  \frac{\the\numerator}{\the\denominator} =
  \frac{6641}{1764}
\end{equation*}

\end{document}

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

references http://rosettacode.org/wiki/Greatest_common_divisor#Lua and http://www.lua.org/manual/5.3/manual.html


If you are not bound to expl3 (in which case you “just” need to implement the algorithm):

\documentclass{scrartcl}
\usepackage{xintgcd,xintfrac}

\newcommand*\reducedfrac[2]
  {\begingroup
     \edef\gcd{\xintGCD{#1}{#2}}%
     \frac{\xintNum{\xintDiv{#1}{\gcd}}}{\xintNum{\xintDiv{#2}{\gcd}}}%
   \endgroup}

\begin{document}
\[
  \frac{278922}{74088} = \reducedfrac{278922}{74088}
\]
\end{document}