Last active
December 27, 2015 19:17
-
-
Save mmloveaa/d0c14bd33e68364e5c3a to your computer and use it in GitHub Desktop.
Arithmetic Sequence Sum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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