# Khan/khan-exercises

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 @@ + + + + Prime Numbers + + + +
+ getPrime() + getFactors( PRIME ).join( ", " ) + + + getOddComposite() + getFactors( WRONG_1 ).join( ", " ) + + getOddComposite() + getFactors( WRONG_2 ).join( ", " ) + + getOddComposite() + getFactors( WRONG_3 ).join( ", " ) + + getEvenComposite() + getFactors( WRONG_4 ).join( ", " ) + +
+ +
+
+

Which of these numbers is prime?

+

PRIME

+ +
+
• WRONG_1
• +
• WRONG_2
• +
• WRONG_3
• +
• WRONG_4
• +
+
+
+ +
+

A prime number has exactly two factors: 1 and itself.

+

The factors of WRONG_1 are WRONG_1_FACTORS.

+

The factors of WRONG_2 are WRONG_2_FACTORS.

+

The factors of WRONG_3 are WRONG_3_FACTORS.

+

The factors of WRONG_4 are WRONG_4_FACTORS.

+

The factors of PRIME are PRIME_FACTORS.

+

Thus, PRIME is a prime number.

+ +
+ +
 @@ -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;