Skip to content

Instantly share code, notes, and snippets.

@mmloveaa
Last active December 27, 2015 19:17
Show Gist options
  • Save mmloveaa/d0c14bd33e68364e5c3a to your computer and use it in GitHub Desktop.
Save mmloveaa/d0c14bd33e68364e5c3a to your computer and use it in GitHub Desktop.
Arithmetic Sequence Sum
// 12/22/2015
// In Your class You have started lessons about Arithmetic Progression.
// Because You are also a programmer, You have decided to write
// a function arithmetic_sequence_sum(a, r, n), that will print
// SUM of the first n elementh of the sequence with the given
// constant r and first elementh a
// For example arithmetic_sequence_sum(2, 3, 5)
// Should return: 40
// My Solution:
function ArithmeticSequenceSum(a, r, n) {
var total=0;
for (var i=0; i<n; i++){
total+=a+i*r
}
return total;
}
ArithmeticSequenceSum(2,3,5)
// Test.assertEquals(ArithmeticSequenceSum(3, 2, 20), 440);
// Test.assertEquals(ArithmeticSequenceSum(2, 2, 10), 110);
// Test.assertEquals(ArithmeticSequenceSum(1, -2, 10), -80);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment