根据任意分数计算不可约分数的分母和分子

根据任意分数计算不可约分数的分母和分子

我想将每个分数简化i/n为其不可重现的形式(假设 i 小于 n)并且不显示为分数,\frac而是像在 latex beamer 中使用的帧号一样显示,即j/m

当使用pgfmath结果有点令人惊讶并且项是通常的分数时,MWE:

\documentclass[border=4pt]{standalone}
\usepackage{pgfmath,fp}
\usetikzlibrary{calc}
\pgfkeys{/pgf/number format/frac}
\newcommand{\fractions}[1]{%
  \foreach \x in {1,...,#1}
    {\pgfmathparse{\x/#1}\pgfmathprintnumber{\pgfmathresult}\hspace{1em}}
}
\begin{document}
  \fractions{16}
\end{document}

结果是 错误的分数

我怎样才能得到它 1/16 1/8 3/16 1/4 5/16 3/8 7/16......?

答案1

您可以使用该gcd()函数来减少分数:

\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}

答案2

分数

\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}

答案3

不知道gcd,自己做 ;-(

在此处输入图片描述

\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}

答案4

使用 CAS 的实现圣人(数学)萨格特克斯

在此处输入图片描述

我用阿拉拉: sagetex用于编译。

% arara: pdflatex
% arara: sagetex
% arara: pdflatex

\documentclass{article}
\usepackage{amsmath}
\usepackage{sagetex}

\begin{document}
\section{In}
\begin{sageblock}
x = 420       # Denominator
MyRange = 222

M = []
for n in range (1,MyRange):
    if x == denominator(n/x):
        M.append('$' + latex(n/x).replace(' ','') + '$')
    else:
        M.append('$' + '\\frac{' + "{}".format(n) + '}{' + "{}".format(x) + '}=' + latex(n/x).replace(' ','') + '$')

MyOut = ', '.join(M)
#print MyOut
\end{sageblock}

\section{Out}
\baselineskip16pt \sagestr{MyOut}
\end{document}

相关内容