Skip to content
Browse files

Sequences and series

Summary: TODO(alpert): Geometric series

Test Plan: Looked at them.

Reviewers: eater

Reviewed By: eater

Differential Revision: http://phabricator.khanacademy.org/D2044
  • Loading branch information...
1 parent 82e2bd3 commit 25bce65e3563398cf9a66708a9a19db87f46f788 @spicyj spicyj committed Feb 15, 2013
View
36 exercises/arithmetic_sequences_1.html
@@ -0,0 +1,36 @@
+<!DOCTYPE html>
+<html data-require="math math-format">
+<head>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+ <title>Arithmetic sequences 1</title>
+ <script src="../khan-exercise.js"></script>
+</head>
+<body>
+ <div class="exercise">
+ <div class="problems">
+ <div>
+ <div class="vars">
+ <var id="A">randRange(-10, 10)</var>
+ <var id="D">randRangeNonZero(-6, 6)</var>
+ <var id="N">randRange(3, 5)</var>
+ <var id="GIVEN">_.map(_.range(N), function(i) { return A + i * D; })</var>
+ </div>
+
+ <div class="question">
+ <p>The first <var>cardinal(N)</var> terms of an arithmetic sequence are given:</p>
+ <p><code><var>GIVEN.join(",")</var>, \ldots</code></p>
+ <p class="question">What is the <var>ordinal(N + 1)</var> term in the sequence?</p>
+ </div>
+ <div class="solution" data-forms="integer"><var>A + D * N</var></div>
+
+ <div class="hints">
+ <p>In any arithmetic sequence, each term is equal to the previous term plus the common difference.</p>
+ <p>Thus, the second term is equal to the first term plus the common difference. In this sequence, the second term, <code><var>A + D</var></code>, is <code><var>abs(D)</var></code> <var>D &gt; 0 ? "more" : "less"</var> than the first term, <code><var>A</var></code>.</p>
+ <p>Therefore, the common difference is <code><var>D</var></code>.</p>
+ <p>The <var>ordinal(N + 1)</var> term in the sequence is equal to the <var>ordinal(N)</var> term plus the common difference, or <code><var>A + D * (N - 1)</var> + <var>D</var> = <var>A + D * N</var></code>.</p>
+ </div>
+ </div>
+ </div>
+ </div>
+</body>
+</html>
View
62 exercises/arithmetic_sequences_2.html
@@ -0,0 +1,62 @@
+<!DOCTYPE html>
+<html data-require="math math-format">
+<head>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+ <title>Arithmetic sequences 2</title>
+ <script src="../khan-exercise.js"></script>
+</head>
+<body>
+ <div class="exercise">
+ <div class="problems">
+ <div id="explicit">
+ <div class="vars">
+ <var id="A">randRangeNonZero(-10, 10)</var>
+ <var id="D">randFromArray([-1, 1]) * randRange(2, 6)</var>
+ <var id="N">randRange(2, 20)</var>
+ </div>
+
+ <div class="question">
+ <p>The arithmetic sequence <code>(a_i)</code> is defined by the formula:</p>
+ <p id="given-formula"><code>a_i = <var>A</var> + <var>D</var>(i - 1)</code></p>
+ <p class="question">What is <code>a_{<var>N</var>}</code>, the <var>ordinal(N)</var> term in the sequence?</p>
+ </div>
+ <div class="solution" data-forms="integer"><var>A + D * (N - 1)</var></div>
+
+ <div class="hints">
+ <p>From the given formula, we can see that the first term of the sequence is <code><var>A</var></code> and the common difference is <code><var>D</var></code>.</p>
+ <div data-else-if="N === 2" data-unwrap>
+ <p>The second term is simply the first term plus the common difference.</p>
+ <p>Therefore, the second term is equal to <code>a_2 = <var>A</var> + <var>D</var> = <var>A + D</var></code>.</p>
+ </div>
+ <div data-else data-unwrap>
+ <p>To find <code>a_{<var>N</var>}</code>, we can simply substitute <code>i = <var>N</var></code> into the given formula.</p>
+ <p>Therefore, the <var>ordinal(N)</var> term is equal to <code>a_{<var>N</var>} = <var>A</var> + <var>D</var> (<var>N</var> - 1) = <var>A + D * (N - 1)</var></code>.</p>
+ </div>
+ </div>
+ </div>
+ <div id="recursive" data-type="explicit">
+ <div class="question" data-apply="appendContents">
+ <p id="given-formula">
+ <code>a_1 = <var>A</var></code><br>
+ <code>a_i = a_{i-1} + <var>D</var></code><br>
+ </p>
+ </div>
+
+ <div class="hints">
+ <p>From the given formula, we can see that the first term of the sequence is <code><var>A</var></code> and the common difference is <code><var>D</var></code>.</p>
+ <div data-else-if="N === 2" data-unwrap>
+ <p>The second term is simply the first term plus the common difference.</p>
+ <p>Therefore, the second term is equal to <code>a_2 = a_1 + <var>D</var> = <var>A</var> + <var>D</var> = <var>A + D</var></code>.</p>
+ </div>
+ <div data-else data-unwrap>
+ <p>To find the <var>ordinal(N)</var> term, we can rewrite the given recurrence as an explicit formula.</p>
+ <p>The general form for an arithmetic sequence is <code>a_i = a_1 + d(i - 1)</code>. In this case, we have <code>a_i = <var>A</var> + <var>D</var>(i - 1)</code>.</p>
+ <p>To find <code>a_{<var>N</var>}</code>, we can simply substitute <code>i = <var>N</var></code> into the our formula.</p>
+ <p>Therefore, the <var>ordinal(N)</var> term is equal to <code>a_{<var>N</var>} = <var>A</var> + <var>D</var> (<var>N</var> - 1) = <var>A + D * (N - 1)</var></code>.</p>
+ </div>
+ </div>
+ </div>
+ </div>
+ </div>
+</body>
+</html>
View
55 exercises/arithmetic_series.html
@@ -0,0 +1,55 @@
+<!DOCTYPE html>
+<html data-require="math math-format">
+<head>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+ <title>Arithmetic series</title>
+ <script src="../khan-exercise.js"></script>
+</head>
+<body>
+ <div class="exercise">
+ <div class="problems">
+ <div id="explicit" data-calculator>
+ <div class="vars" data-ensure="(D * (N - 1)) % 2 === 0 &amp;&amp; abs(SUM) &lt; 100">
+ <var id="A">randRangeNonZero(-40, 40)</var>
+ <var id="D">randFromArray([-1, 1]) * randRange(2, 6)</var>
+ <var id="N">randRange(5, 20)</var>
+ <var id="SUM">N * (2 * A + D * (N - 1)) / 2</var>
+ </div>
+
+ <div class="question">
+ <p>The arithmetic sequence <code>(a_i)</code> is defined by the formula:</p>
+ <p id="given-formula"><code>a_i = <var>A</var> + <var>D</var>(i - 1)</code></p>
+ <p class="question">What is the sum of the first <var>N &lt; 10 ? cardinal(N) : N</var> terms in the series?</p>
+ </div>
+ <div class="solution" data-forms="integer"><var>SUM</var></div>
+
+ <div class="hints">
+ <p>The sum of an arithmetic series is the number of terms in the series times the average of the first and last terms.</p>
+ <div id="recursive-hints" data-unwrap></div>
+ <p>To find the sum of the first <var>N &lt; 10 ? cardinal(N) : N</var> terms, we'll need the first and <var>ordinal(N)</var> terms of the series.</p>
+ <p>The first term is <code><var>A</var></code> and the <var>ordinal(N)</var> term is equal to <code>a_{<var>N</var>} = <var>A</var> + <var>D</var> (<var>N</var> - 1) = <var>A + D * (N - 1)</var></code>.</p>
+ <div>
+ <p>Therefore, the sum of the first <var>N &lt; 10 ? cardinal(N) : N</var> terms is</p>
+ <p><code>\qquad n\left(\dfrac{a_1 + a_{<var>N</var>}}{2}\right) = <var>N</var> \left(\dfrac{<var>A</var> + <var>A + D * (N - 1)</var>}{2}\right) = <var>SUM</var></code>.
+ </div>
+ </div>
+ </div>
+ <div id="recursive" data-type="explicit" data-calculator>
+ <div class="question" data-apply="appendContents">
+ <p id="given-formula">
+ <code>a_1 = <var>A</var></code><br>
+ <code>a_i = a_{i-1} + <var>D</var></code><br>
+ </p>
+ </div>
+
+ <div class="hints" data-apply="appendContents">
+ <div id="recursive-hints" data-unwrap>
+ <p>First, let's find the explicit formula for the terms of the arithmetic series. We can see that the first term is <code><var>A</var></code> and the common difference is <code><var>D</var></code>.</p>
+ <p>Thus, the explicit formula for this sequence is <code>a_i = <var>A</var> + <var>D</var>(i - 1)</code>.</p>
+ </div>
+ </div>
+ </div>
+ </div>
+ </div>
+</body>
+</html>
View
51 exercises/geometric_sequences_1.html
@@ -0,0 +1,51 @@
+<!DOCTYPE html>
+<html data-require="math math-format">
+<head>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+ <title>Geometric sequences 1</title>
+ <script src="../khan-exercise.js"></script>
+</head>
+<body>
+ <div class="exercise">
+ <div class="problems">
+ <div>
+ <div class="vars" data-ensure="abs(GIVEN[0][0]) &lt; 200 &amp;&amp; abs(GIVEN[0][1]) &lt; 200 &amp;&amp; abs(ANS[0]) &lt; 200 &amp;&amp; abs(ANS[1]) &lt; 200">
+ <var id="A">randRangeNonZero(-8, 8)</var>
+ <var id="RN">randFromArray([-1, 1]) * randRange(1, 4)</var>
+ <var id="RD" data-ensure="abs(RN) !== abs(RD)">randRange(1, 4)</var>
+ <var id="N">randRange(3, 5)</var>
+ <!-- N + 1 + OFFSET >= 0 always -->
+ <var id="OFFSET">randRange(-N - 1, 0)</var>
+ <var id="GIVEN">_.map(_.range(N), function(i) {
+ if (i + OFFSET &gt;= 0) {
+ return reduce(A * pow(RN, i + OFFSET), pow(RD, i + OFFSET));
+ } else {
+ return reduce(A * pow(RD, -i - OFFSET), pow(RN, -i - OFFSET));
+ }
+ })</var>
+ <var id="ANS">reduce(A * pow(RN, N + OFFSET), pow(RD, N + OFFSET))</var>
+
+ <var id="R_TEX">fractionReduce(RN, RD)</var>
+ <var id="GIVEN_TEX">_.map(GIVEN, function(f) {
+ return fractionReduce.apply(KhanUtil, f);
+ })</var>
+ </div>
+
+ <div class="question">
+ <p>The first <var>cardinal(N)</var> terms of a geometric sequence are given:</p>
+ <p><code><var>GIVEN_TEX.join(",")</var>, \ldots</code></p>
+ <p class="question">What is the <var>ordinal(N + 1)</var> term in the sequence?</p>
+ </div>
+ <div class="solution"><var>A * pow(RN / RD, N + OFFSET)</var></div>
+
+ <div class="hints">
+ <p>In any geometric sequence, each term is equal to the previous term times the common ratio.</p>
+ <p>Thus, the second term is equal to the first term times the common ratio. In this sequence, the second term, <code><var>GIVEN_TEX[1]</var></code>, is <code><var>R_TEX</var></code> times the first term, <code><var>GIVEN_TEX[0]</var></code>.</p>
+ <p>Therefore, the common ratio is <code><var>R_TEX</var></code>.</p>
+ <p>The <var>ordinal(N + 1)</var> term in the sequence is equal to the <var>ordinal(N)</var> term times the common ratio, or <code><var>GIVEN_TEX[N - 1]</var> \cdot <var>R_TEX</var> = <var>fractionReduce(A * pow(RN, N + OFFSET), pow(RD, N + OFFSET))</var></code>.</p>
+ </div>
+ </div>
+ </div>
+ </div>
+</body>
+</html>
View
76 exercises/geometric_sequences_2.html
@@ -0,0 +1,76 @@
+<!DOCTYPE html>
+<html data-require="math math-format">
+<head>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+ <title>Geometric sequences 2</title>
+ <script src="../khan-exercise.js"></script>
+</head>
+<body>
+ <div class="exercise">
+ <div class="problems">
+ <div id="explicit">
+ <div class="vars" data-ensure="abs(TERMS[0][0]) &lt; 20 &amp;&amp; abs(TERMS[0][1]) &lt; 20 &amp;&amp; abs(TERMS[N-1][0]) &lt; 40 &amp;&amp; abs(TERMS[N-1][0]) &lt; 40">
+ <var id="A">randRangeNonZero(-8, 8)</var>
+ <var id="RN">randFromArray([-1, 1]) * randRange(1, 4)</var>
+ <var id="RD" data-ensure="abs(RN) !== abs(RD)">randRange(1, 4)</var>
+ <var id="N">randRange(2, 5)</var>
+ <var id="OFFSET">randRange(-N - 1, 0)</var>
+ <var id="TERMS">_.map(_.range(N), function(i) {
+ if (i + OFFSET &gt;= 0) {
+ return reduce(A * pow(RN, i + OFFSET), pow(RD, i + OFFSET));
+ } else {
+ return reduce(A * pow(RD, -i - OFFSET), pow(RN, -i - OFFSET));
+ }
+ })</var>
+
+ <var id="R_TEX">fractionReduce(RN, RD)</var>
+ <var id="TERMS_TEX">_.map(TERMS, function(f) {
+ return fractionReduce.apply(KhanUtil, f);
+ })</var>
+ </div>
+
+ <div class="question">
+ <p>The geometric sequence <code>(a_i)</code> is defined by the formula:</p>
+ <p id="given-formula"><code>a_i = <var>TERMS_TEX[0]</var> \left(<var>R_TEX</var>\right)^{i - 1}</code></p>
+ <p class="question">What is <code>a_{<var>N</var>}</code>, the <var>ordinal(N)</var> term in the sequence?</p>
+ </div>
+ <div class="solution"><var>A * pow(RN / RD, N - 1 + OFFSET)</var></div>
+
+ <div class="hints">
+ <p>From the given formula, we can see that the first term of the sequence is <code><var>TERMS_TEX[0]</var></code> and the common ratio is <code><var>R_TEX</var></code>.</p>
+ <div data-else-if="N === 2" data-unwrap>
+ <p>The second term is simply the first term times the common ratio.</p>
+ <p>Therefore, the second term is equal to <code>a_2 = <var>TERMS_TEX[0]</var> \cdot <var>R_TEX</var> = <var>TERMS_TEX[1]</var></code>.</p>
+ </div>
+ <div data-else data-unwrap>
+ <p>To find <code>a_{<var>N</var>}</code>, we can simply substitute <code>i = <var>N</var></code> into the given formula.</p>
+ <p>Therefore, the <var>ordinal(N)</var> term is equal to <code>a_{<var>N</var>} = <var>TERMS_TEX[0]</var> \left(<var>R_TEX</var>\right)^{<var>N</var> - 1} = <var>TERMS_TEX[N-1]</var></code>.</p>
+ </div>
+ </div>
+ </div>
+ <div id="recursive" data-type="explicit">
+ <div class="question" data-apply="appendContents">
+ <p id="given-formula">
+ <code>a_1 = <var>TERMS_TEX[0]</var></code><br>
+ <code>a_i = <var>R_TEX</var>a_{i-1}</code>
+ </p>
+ </div>
+
+ <div class="hints">
+ <p>From the given formula, we can see that the first term of the sequence is <code><var>TERMS_TEX[0]</var></code> and the common ratio is <code><var>R_TEX</var></code>.</p>
+ <div data-else-if="N === 2" data-unwrap>
+ <p>The second term is simply the first term times the common ratio.</p>
+ <p>Therefore, the second term is equal to <code>a_2 = <var>TERMS_TEX[0]</var> \cdot <var>R_TEX</var> = <var>TERMS_TEX[1]</var></code>.</p>
+ </div>
+ <div data-else data-unwrap>
+ <p>To find the <var>ordinal(N)</var> term, we can rewrite the given recurrence as an explicit formula.</p>
+ <p>The general form for a geometric sequence is <code>a_i = a_1 r^{i - 1}</code>. In this case, we have <code>a_i = <var>TERMS_TEX[0]</var> \left(<var>R_TEX</var>\right)^{i - 1}</code>.</p>
+ <p>To find <code>a_{<var>N</var>}</code>, we can simply substitute <code>i = <var>N</var></code> into the formula.</p>
+ <p>Therefore, the <var>ordinal(N)</var> term is equal to <code>a_{<var>N</var>} = <var>TERMS_TEX[0]</var> \left(<var>R_TEX</var>\right)^{<var>N</var> - 1} = <var>TERMS_TEX[N-1]</var></code>.</p>
+ </div>
+ </div>
+ </div>
+ </div>
+ </div>
+</body>
+</html>
View
17 utils/math-format.js
@@ -314,6 +314,23 @@ $.extend(KhanUtil, {
return card.charAt(0).toUpperCase() + card.slice(1);
},
+ ordinal: function(n) {
+ if (n <= 9) {
+ return ["zeroth", "first", "second", "third", "fourth", "fifth",
+ "sixth", "seventh", "eighth", "ninth"][n];
+ } else if (Math.floor(n / 10) % 10 === 1) {
+ // Teens
+ return n + "th";
+ } else {
+ var lastDigit = n % 10;
+ if (1 <= lastDigit && lastDigit <= 3) {
+ return n + ["st", "nd", "rd"][lastDigit - 1];
+ } else {
+ return n + "th";
+ }
+ }
+ },
+
// Depends on expressions.js for expression formatting
// Returns a string with the expression for the formatted roots of the quadratic
// with coefficients a, b, c
View
16 utils/tmpl.js
@@ -1,5 +1,7 @@
(function() {
+var testMode;
+
// Keep the template variables private, to prevent external access
var VARS = {};
@@ -15,7 +17,12 @@ $.tmpl = {
// False means all templating will be run again, so new values will be chosen
var result = !!(ensure && $.tmpl.getVAR(ensure));
if (!result) {
- ++$.tmpl.DATA_ENSURE_LOOPS;
+ if ($.tmpl.DATA_ENSURE_LOOPS++ > 10000 && testMode) {
+ // Shucks, probably not possible. Just give up in order
+ // to not hang the dev's browser.
+ alert("unsatisfiable data-ensure?");
+ return true;
+ }
}
return result;
};
@@ -320,9 +327,10 @@ $.fn.tmplLoad = function(problem, info) {
VARS = {};
$.tmpl.DATA_ENSURE_LOOPS = 0;
- // Check to see if we're in test mode
- if (info.testMode) {
- // Expose the variables if we're in test mode
+ testMode = info.testMode;
+
+ // Expose the variables if we're in local mode
+ if (testMode) {
$.tmpl.VARS = VARS;
}
};

0 comments on commit 25bce65

Please sign in to comment.
Something went wrong with that request. Please try again.