Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
add two more exercises for factoring special polynomials
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffdyer committed Jan 12, 2012
1 parent cbfaf28 commit 13c0194
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 17 deletions.
72 changes: 72 additions & 0 deletions exercises/factoring_special_polynomials_1.html
@@ -0,0 +1,72 @@
<!DOCTYPE html>
<html data-require="math graphie ast math-model">
<head>
<meta charset="UTF-8" />
<title>Factoring Special Product Polynomials with A = 1</title>
<script src="../khan-exercise.js"></script>
</head>
<body>
<div class="exercise">
<div class="problems">
<div>
<div class="vars">
<var id="MATH">MathModel.init()</var>
<var id="b">randRangeNonZero(-9, 9)</var>
<var id="A">1</var>
<var id="B">2*b</var>
<var id="C">b*b</var>
<var id="PROBLEM">MATH.polynomial([A, B, C], "x")</var>
<!-- use parse so that the ast matches the parsed user input -->
<var id="SOLUTION">MATH.parse("(x+"+b+")^2")</var>
</div>
<div>
<p class="question">
Factor the following expression:
</p>
<p>
<code><var>MATH.format(PROBLEM, "large")</var></code>
</p>
</div>
<div class="solution" data-type="custom">
<div class="instruction">
Enter the factored expression here:<br>
<input name="response" type="text" onblur="window._guess=value"></input>
</div>
<div class="guess">window._guess</div>
<div class="validator-function">
return MATH.isEqual(MATH.parse(guess), SOLUTION)
</div>
<div class="show-guess">
</div>
<div class="example">(x+2)^2</div>
</div>
<div class="hints">
<p>
Recognize that the expression is of the form
<code><var>MATH.format("a^2+2ab+b^2", "normalsize", KhanUtil.BLUE)</var></code>
, which can be factored as
<code><var>MATH.format("(a+b)^2", "normalsize", KhanUtil.BLUE)</var></code>.
</p>
<p>
First, determine the values of
<code><var>MATH.format("a", "normalsize", KhanUtil.BLUE)</var></code> and
<code><var>MATH.format("b", "normalsize", KhanUtil.BLUE)</var></code>.
</p>
<p>
<code><var>MATH.format("a="+"x", "normalsize", KhanUtil.BLUE)</var></code><br><br>
<code><var>MATH.format("b="+2*b+"/"+2+"="+b, "normalsize", KhanUtil.BLUE)</var></code>
</p>
<p>
Replace
<code><var>MATH.format("a", "normalsize", KhanUtil.BLUE)</var></code> and
<code><var>MATH.format("b", "normalsize", KhanUtil.BLUE)</var></code> in the factored equation to find the answer.
</p>
<p>
<code><var>MATH.format("(x+"+b+")^2", "large", KhanUtil.ORANGE)</var></code>
</p>
</div>
</div>
</div>
</div>
</body>
</html>
73 changes: 73 additions & 0 deletions exercises/factoring_special_polynomials_2.html
@@ -0,0 +1,73 @@
<!DOCTYPE html>
<html data-require="math graphie ast math-model">
<head>
<meta charset="UTF-8" />
<title>Factoring Special Product Polynomials with A != 1</title>
<script src="../khan-exercise.js"></script>
</head>
<body>
<div class="exercise">
<div class="problems">
<div>
<div class="vars">
<var id="MATH">MathModel.init()</var>
<var id="a">randRange(2, 9)</var>
<var id="b">randRangeNonZero(-9, 9)</var>
<var id="A">a*a</var>
<var id="B">2*a*b</var>
<var id="C">b*b</var>
<var id="PROBLEM">MATH.polynomial([A, B, C], "x")</var>
<!-- use parse so that the ast matches the parsed user input -->
<var id="SOLUTION">MATH.parse("("+a+"x+"+b+")^2")</var>
</div>
<div>
<p class="question">
Factor the following expression:
</p>
<p>
<code><var>MATH.format(PROBLEM, "large")</var></code>
</p>
</div>
<div class="solution" data-type="custom">
<div class="instruction">
Enter the factored expression here:<br>
<input name="response" type="text" onblur="window._guess=value"></input>
</div>
<div class="guess">window._guess</div>
<div class="validator-function">
return MATH.isEqual(MATH.parse(guess), SOLUTION)
</div>
<div class="show-guess">
</div>
<div class="example">(2x+3)^2</div>
</div>
<div class="hints">
<p>
Recognize that the expression is of the form
<code><var>MATH.format("a^2+2ab+b^2", "normalsize", KhanUtil.BLUE)</var></code>
, which can be factored as
<code><var>MATH.format("(a+b)^2", "normalsize", KhanUtil.BLUE)</var></code>.
</p>
<p>
First, determine the values of
<code><var>MATH.format("a", "normalsize", KhanUtil.BLUE)</var></code> and
<code><var>MATH.format("b", "normalsize", KhanUtil.BLUE)</var></code>.
</p>
<p>
<code><var>MATH.format("a="+a+"x", "normalsize", KhanUtil.BLUE)</var></code><br><br>
<code><var>MATH.format("b="+2*b+"/"+2+"="+b, "normalsize", KhanUtil.BLUE)</var></code>
</p>
<p>
Replace
<code><var>MATH.format("a", "normalsize", KhanUtil.BLUE)</var></code> and
<code><var>MATH.format("b", "normalsize", KhanUtil.BLUE)</var></code> in the factored equation to find the answer.
</p>
<p>
<code><var>MATH.format("("+a+"x+"+b+")^2", "large", KhanUtil.ORANGE)</var></code>
</p>
</div>
</div>
</div>
</div>
</body>
</html>
72 changes: 55 additions & 17 deletions utils/math-model.js
Expand Up @@ -494,8 +494,9 @@ jQuery.extend ( KhanUtil, {

jQuery.extend ( this, {

parse : parse,
format : format,
parse: parse,
format: format,
polynomial: polynomial,

dumpAll : function () {
return ast.dumpAll();
Expand Down Expand Up @@ -668,9 +669,43 @@ jQuery.extend ( KhanUtil, {
return n.kind() !== void 0;
}

// This method returns the AST for some computation on the given ast.
// For example, n.compute("factors") might compute the factors of the
// polynomial represented by 'n'.
function polynomial(coeffs, ident, lhs) {
// build left recursive ast

if (coeffs.length===0) {
// we're done
return lhs;
}

if (ident===void 0) {
// default identifier
ident = "x";
}

if (coeffs.length === 1) {
// degree zero
var rhs = coeffs[0]
}
else {
// construct a term of degree given by the size of coeffs
var rhs = {op: "times", args: [coeffs[0], {op: "^", args: [ident, coeffs.length-1]}]};
}

if (coeffs[0]===0) {
// zero coefficient, erase term by not updating lhs
}
else if (lhs===void 0) {
// first term, no lhs to contribute
var lhs = rhs;
}
else {
// normal case
var lhs = {op: "+", args: [lhs, rhs]}
}

// recurse until no more coefficients
return polynomial(coeffs.slice(1), ident, lhs);
}

function compute(options, n) {
if (!isValidAST(n)) {
Expand All @@ -681,9 +716,6 @@ jQuery.extend ( KhanUtil, {
return n;
}

function expand(n) {
}

function parse(str) {
return KhanUtil.parse(str).expr();
}
Expand Down Expand Up @@ -779,16 +811,22 @@ jQuery.extend ( KhanUtil, {
case OpStr.MUL:
var lhs = n.args[0];
var rhs = n.args[1];
if (jQuery.type(lhs)==="number" && lhs===1) {
// elide coefficient when it is 1.
text = format(rhs, textSize, color);
}
else if (jQuery.type(lhs)==="number" && lhs===-1) {
// elide coefficient when it is 1.
text = "\\color{#000}{-}"+format(rhs, textSize, color);
if (jQuery.type(lhs)==="number" && (lhs===1 || lhs===-1)) {
//if (lhs === 0) {
// // elide term if multiplied by 0.
// text = "";
//}
//else
if (lhs === 1) {
// elide coefficient when it is 1.
text = format(rhs, textSize, color);
}
else if (lhs===-1) {
// elide coefficient when it is 1.
text = "\\color{#000}{-}"+format(rhs, textSize, color);
}
}

if ((lhs.args && lhs.args.length===2) || (rhs.args && rhs.args.length===2)) {
else if ((lhs.args && lhs.args.length===2) || (rhs.args && rhs.args.length===2)) {
var lhsText = format(lhs, textSize, color);
var rhsText = format(rhs, textSize, color);
// if subexpr is lower precedence, wrap in parens
Expand Down

0 comments on commit 13c0194

Please sign in to comment.