Permalink
Browse files

Add prime numbers

  • Loading branch information...
1 parent 7711efc commit af417c353eea29f484174a07bf8b6270d5eec90d Marcia Lee committed May 17, 2011
Showing with 73 additions and 7 deletions.
  1. +53 −0 exercises/prime_numbers.html
  2. +20 −7 utils/math.js
@@ -0,0 +1,53 @@
+<!DOCTYPE html>
+<html data-require="math">
+<head>
+ <title>Prime Numbers</title>
+ <script src="../khan-exercise.js"></script>
+</head>
+<body>
+ <div id="vars">
+ <var id="PRIME">getPrime()</var>
+ <var id="PRIME_FACTORS">getFactors( PRIME ).join( ", " )</var>
+
+ <!-- TODO: Pass in function that generates wrong answers,
+ and keep calling until have enough unique answers -->
+ <var id="WRONG_1">getOddComposite()</var>
+ <var id="WRONG_1_FACTORS">getFactors( WRONG_1 ).join( ", " )</var>
+
+ <var id="WRONG_2">getOddComposite()</var>
+ <var id="WRONG_2_FACTORS">getFactors( WRONG_2 ).join( ", " )</var>
+
+ <var id="WRONG_3">getOddComposite()</var>
+ <var id="WRONG_3_FACTORS">getFactors( WRONG_3 ).join( ", " )</var>
+
+ <var id="WRONG_4">getEvenComposite()</var>
+ <var id="WRONG_4_FACTORS">getFactors( WRONG_4 ).join( ", " )</var>
+
+ </div>
+
+ <div id="problems">
+ <div class="problem">
+ <p class="question">Which of these numbers is prime?</p>
+ <p class="solution"><var>PRIME</var></p>
+ <!-- TODO: no data-none doesn't seem to work -->
+ <ul class="choices" data-show="5" data-none="true">
+ <li><var>WRONG_1</var></li>
+ <li><var>WRONG_2</var></li>
+ <li><var>WRONG_3</var></li>
+ <li><var>WRONG_4</var></li>
+ </ul>
+ </div>
+ </div>
+
+ <div id="hints">
+ <p>A prime number has exactly two factors: 1 and itself.</p>
+ <p>The factors of <var>WRONG_1</var> are <var>WRONG_1_FACTORS</var>.</p>
+ <p>The factors of <var>WRONG_2</var> are <var>WRONG_2_FACTORS</var>.</p>
+ <p>The factors of <var>WRONG_3</var> are <var>WRONG_3_FACTORS</var>.</p>
+ <p>The factors of <var>WRONG_4</var> are <var>WRONG_4_FACTORS</var>.</p>
+ <p>The factors of <var>PRIME</var> are <var>PRIME_FACTORS</var>.</p>
+ <p>Thus, <var>PRIME</var> is a prime number.</p>
+
+ </div>
+</body>
+</html>
View
@@ -15,8 +15,26 @@ $.extend(KhanUtil, {
return a;
},
- allprimes: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97],
-
+ getPrime: function() {
+ var primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43];
+ primes = primes.concat([47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]);
+ return primes[ this.rand( primes.length ) ];
+ },
+
+ getOddComposite: function() {
+ var oddComposites = [9, 15, 21, 25, 27, 33, 35, 39, 45, 49, 51, 55];
+ oddComposites = oddComposites.concat([57, 63, 65, 69, 75, 77, 81, 85, 87, 91, 93, 95, 99]);
+ return oddComposites[ this.rand( oddComposites.length ) ];
+ },
+
+ getEvenComposite: function() {
+ var evenComposites = [4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26];
+ evenComposites = evenComposites.concat([28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48]);
+ evenComposites = evenComposites.concat([50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72]);
+ evenComposites = evenComposites.concat([74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98]);
+ return evenComposites[ this.rand( evenComposites.length ) ];
+ },
+
getPrimeFactorization: function( number ) {
if ( $.inArray(number, allprimes) != -1) {
return [number];
@@ -44,11 +62,6 @@ $.extend(KhanUtil, {
});
},
- roundToPosition: function( num, pos ) {
- var factor = Math.pow( 10, -1 * pos ).toFixed( 5 );
- return Math.round( (num * factor).toFixed(5) ) / factor;
- },
-
// Get a random integer between min and max, inclusive
randRange: function( min, max ) {
return Math.floor(Math.random() * (max - min + 1)) + min;

0 comments on commit af417c3

Please sign in to comment.