Compute denominator and numerator of irreducible fraction from arbitrary fraction

You could use the gcd() function to reduce the fractions:

\documentclass[border=4pt]{standalone}
\usepackage{pgfmath,pgffor}

\newcommand{\reducedfractions}[1]{%
    \foreach \x in {1,...,#1} {%
        \pgfmathtruncatemacro{\numerator}{\x/gcd(\x,#1)}%
        \pgfmathtruncatemacro{\denominator}{#1/gcd(\x,#1)}%
        $\frac{\numerator}{\denominator}\hspace{1em}$%
    }%
}

\begin{document}
  \reducedfractions{16}
\end{document}

Did't know about gcd, make it by myself ;-(

enter image description here

\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\newcommand{\PGCD}[2]{%
\edef\Num{#1}%
\edef\Den{#2}%
\loop%
    \pgfmathtruncatemacro\Mod{mod(\Num,\Den)}%
    \ifnum\Mod>0%
    \edef\Num{\Den}%
    \edef\Den{\Mod}%
    \repeat%
    \edef\Pgcd{\Den}%       
    \pgfmathtruncatemacro\Num{#1/\Pgcd}%
    \pgfmathtruncatemacro\Den{#2/\Pgcd}%    
}

\newcommand{\fractions}[1]{%
    \foreach \x in {1,...,#1} {%
        \PGCD{\x}{#1}%
        $\frac{\Num}{\Den}\hspace{1em}$%
    }
}

\begin{document}
\fractions{16}
\end{document}

fractions

\documentclass [a4paper]{article}
\usepackage [margin=1cm]{geometry}
\usepackage{xintfrac}
\usepackage{xintexpr}

\newcommand{\fractions}[1]{%
    \xintFor ##1 in {\xintintegers} \do {%
        \xintIrr {##1/#1}\ \ \ 
        \ifnum#1=##1\expandafter\xintBreakFor\fi
    }
}
% Note to the techies: ##1 from \xintintegers is in fact a \numexpr thing;
% generally a \numexpr needs to be prefixed by \the to be used in the xintfrac
% macros, but it is ok if it contains only at most eight tokens. As the macro is
% unlikely to be called as is with #1>99999999, no need to bother, and it is a
% bit more efficient to not do \the.

\newcommand{\mathfractions}[1]{%
    \xintFor ##1 in {\xintintegers} \do {%
        \xintFrac{\xintIrr {##1/#1}}\allowbreak\ 
        \ifnum#1=##1\expandafter\xintBreakFor\fi
    }
}

\begin{document}
\noindent\fractions {420}

\noindent\baselineskip14pt $\mathfractions {420}$\par

\bigskip
\edef\Result{\xinttheexpr reduce (40!*60!*80!/(100!*30!*50!))\relax }
$$\frac{40!*60!*80!}{100!*30!*50!}=\Result=\xintFrac{\Result}$$
\end{document}

Tags:

Pgfmath