# Khan/khan-exercises

### Subversion checkout URL

You can clone with HTTPS or Subversion.

Working integer sum exercise

commit 323cdd766a7379afc9cf56485926160572452ed2 1 parent 5055c92
petercollingridge authored
Showing with 41 additions and 18 deletions.
1. +41 −18 exercises/integer_sums.html
59 exercises/integer_sums.html
 @@ -9,44 +9,67 @@
randRange(1, 100) - randRange(2, 5) + randRange(2, 6) randRange(1, 2) + randFromArray(["smallest", "largest"]) + DIFFERENCE * SEQ_LENGTH * (SEQ_LENGTH - 1) / 2 + FIRST_NUM * SEQ_LENGTH + C + + (function() { + if (DIFFERENCE === 1) { + return "integer"; + } else if (isOdd(FIRST_NUM)) { + return "odd number"; + } else { + return "even number"; + } + })() + (function() { - var sequence = [FIRST_NUM]; + var sequence = "x"; for (var i = 1; i <SEQ_LENGTH; i++) { - sequence.push(i, FIRST_NUM + i * DIFFERENCE); + sequence += "+ (x + " + (i * DIFFERENCE) + ")"; } return sequence; })() - FIRST_NUM * SEQ_LENGTH + DIFFERENCE * SEQ_LENGTH * (SEQ_LENGTH - 1) / 2 + + (function() { + if (TARGET_NUMBER === "smallest") { + return FIRST_NUM + } else { + return FIRST_NUM + (SEQ_LENGTH - 1) * DIFFERENCE + } + })() +
-
-

The sum of SEQ_LENGTH consecutive NUMBER_TYPE is SUM.

-
-

What is the smallest of the SEQ_LENGTH NUMBER_TYPE?

-

FIRST_NUM

+

The sum of SEQ_LENGTH consecutive SEQ_TYPEs is SUM.

+

What is the TARGET_NUMBER of the SEQ_LENGTH numbers?

+

-

The greatest common divisor is the largest number that is a factor (or divisor) of both A and B.

+

x = first SEQ_TYPE in the sequence

+

x + DIFFERENCE = next SEQ_TYPE in the sequence

-

The only factor (divisor) of 1 is 1.

-

The factors (divisors) of A are A_FACTORS.

+

The sum of consecutive SEQ_TYPEs is:

+

SEQUENCE = SUM

-
-

The only factor (divisor) of 1 is 1.

-

The factors (divisors) of B are B_FACTORS.

+

SEQ_LENGTHx + C= SUM

+

SEQ_LENGTHx = SUM-C

+

x = FIRST_NUM

+
+

Thus, the TARGET_NUMBER number is ANSWER.

-
-

Thus, the greatest common divisor of A and B is GCD.

-

\gcd(A, B) = GCD

+
+

The TARGET_NUMBER number is x + (SEQ_LENGTH - 1) * DIFFERENCE

+

Thus, the TARGET_NUMBER number is ANSWER.