-
Notifications
You must be signed in to change notification settings - Fork 0
/
p06_sumSquareDiff.js
62 lines (53 loc) · 1.59 KB
/
p06_sumSquareDiff.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*The sum of the squares of the first ten natural numbers is,
*
* 1^2 + 2^2 + ... + 10^2 = 385
* The square of the sum of the first ten natural numbers is,
*
* (1 + 2 + ... + 10)2 = 55^2 = 3025
*
* Hence the difference between the sum of the squares of the
* first ten natural numbers and the square of the sum is
* 3025 - 385 = 2640.
*
* Find the difference between the sum of the squares of the
* first one hundred natural numbers and the square of the sum.
*/
/*
* sumOfExponentials will take a maximum value. It will take all
* numbers from 1 to this value, apply the power operation to
* the power passed in (default 2), and sum those values.
*
* @param the maximum value to find the power of
* @param the exponent (optional, default 2)
*
* @return the sum
*/
function sumOfExponentials(maxValue, power) {
var index, sum;
if (power === undefined) power = 2;
sum = 0;
for (index = 1; index <= maxValue; index++) {
sum += Math.pow(index, power);
}
return sum;
}
/*
* exponentialOfSum takes in a maximum value and sums up the
* numbers in range(1, maxValue). It then applies an exponential
* function to the power of the 2nd argument (optional, default is 2)
*
* @param the maximum value to sum up, from 1 to maxValue
* @param the exponent (optional, default 2)
*
* @return the exponential of the sum
*/
function exponentialOfSum(maxValue, power) {
var index, sum;
if (power === undefined) power = 2;
sum = 0;
for (index = 1; index <= maxValue; index++) {
sum += index;
}
return Math.pow(sum, power);
}
console.log(exponentialOfSum(100) - sumOfExponentials(100));