我正在尝试创建一个包含随机数的基本因式分解工作表。到目前为止,如果所有整数都是正数,那么这很容易。
也就是说,用随机数创建 x^2+10x+24=(x+6)(x+4) 很容易。
但我还需要一些负整数。我甚至不知道从哪里开始处理以下问题:
- 负随机数错误。
- 有时删除加法符号并用减法符号代替。
- 如何不是让 0 成为随机整数之一。
这是我的代码。
\documentclass{article}
\usepackage{ifthen}
\usepackage{pgf}
\usepackage{pgffor}
\setlength{\parindent}{0pt}
\pgfmathsetseed{\number\pdfrandomseed}
\newcommand{\InitVariables}
{
\pgfmathsetmacro{\m}{int(random(1,10))}
\pgfmathsetmacro{\n}{int(random(1,10))}
\pgfmathsetmacro{\b}{int(\m+\n)}
\pgfmathsetmacro{\c}{int(\m*\n)}
\pgfmathtruncatemacro{\Structure}{random(1,4)}
}
\newcommand{\expanded}
{
\InitVariables
\(x^2+{\b}x+{\c}\)
}
\newcommand{\factored}
{\((x+\m)(x+\n)\)}
\newcommand{\manysimpletrinomials}
{
\foreach \x in {1,2,3,...,10}
{\expanded \(=\) \factored\\}
}
\pagestyle{empty}
\title{Factoring Simple Trinomials, Generated by Random Numbers}
\author{}
\date{}
\begin{document}
\maketitle
With all positive integers, it's quite easy.
\manysimpletrinomials
I also want to incorporate negative integers to generate the following randomly.
I'm not sure how to do it.
\(x^2-4x-12=(x-6)(x+2)\)
\end{document}
编辑:以及如何避免\m
+ \n
= \b
=0,生成如下输出:
答案1
您可以使用 0-9 范围内的随机数并加 1 来避免 0(实际上可以只使用范围 1-9:-),然后使用 0-1 范围内的一些随机数作为开关来翻转符号。
\documentclass{article}
\usepackage{ifthen}
\usepackage{pgf}
\usepackage{pgffor}
\setlength{\parindent}{0pt}
\pgfmathsetseed{\number\pdfrandomseed}
\newcommand{\InitVariables}
{
\pgfmathsetmacro{\m}{int(random(1,9))}
\pgfmathsetmacro{\n}{int(random(1,9))}
\pgfmathsetmacro{\msign}{int(random(0,1))}
\pgfmathsetmacro{\nsign}{int(random(0,1))}
\pgfmathsetmacro{\b}{int(\ifodd\msign+\else-\fi\m\ifodd\nsign+\else-\fi\n)}
\pgfmathsetmacro{\c}{int((\ifodd\msign+\else-\fi\m)*(\ifodd\nsign+\else-\fi\n))}
\pgfmathtruncatemacro{\Structure}{random(1,4)}
}
\newcommand{\expanded}
{
\InitVariables
\(x^2{\ifnum\b>0+\fi\b}x{\ifnum\c>0+\fi\c}\)
}
\newcommand{\factored}
{\((x\ifodd\msign+\else-\fi\m)(x\ifodd\nsign+\else-\fi\n)\)}
\newcommand{\manysimpletrinomials}
{
\foreach \x in {1,2,3,...,10}
{\expanded \(=\) \factored\\}
}
\pagestyle{empty}
\title{Factoring Simple Trinomials, Generated by Random Numbers}
\author{}
\date{}
\begin{document}
\maketitle
With all positive integers, it's quite easy.
\manysimpletrinomials
I also want to incorporate negative integers to generate the following randomly.
I'm not sure how to do it.
\(x^2-4x-12=(x-6)(x+2)\)
\end{document}