-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinanceOnAnotherPlanet.js
executable file
·53 lines (43 loc) · 1.46 KB
/
financeOnAnotherPlanet.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
// I need to save some money to buy a gift. I think I can do
// something like that:
// First week (W0) I save nothing on Sunday, 1 on Monday, 2 on
// Tuesday... 6 on Saturday, second week (W1) 2 on Monday... 7
// on Saturday and so on according to the table below where the
// days are numbered from 0 to 6.
// Can you tell me how much I will have for my gift on Saturday
// evening after I have saved 12? (Your function finance(6)
// should return 168 which is the sum of the savings in the table).
// Imagine now that we live on planet XY140Z-n where the days of
// the week are numbered from 0 to n (integer n > 0) and where I
// save from week number 0 to week number n included (in the
// table below n = 6).
// How much money would I have at the end of my financing plan
// on planet XY140Z-n?
// -- Su Mo Tu We Th Fr Sa
// W6 12
// W5 10 11
// W4 8 9 10
// W3 6 7 8 9
// W2 4 5 6 7 8
// W1 2 3 4 5 6 7
// W0 0 1 2 3 4 5 6
// Example:
// finance(5) --> 105
// finance(6) --> 168
// finance(7) --> 252
// finance(5000) --> 62537505000
// Hint:
// try to avoid nested loops!
function finance(n) {
var total = 0;
var start = 0;
var end = n + 1;
while (start !== end) {
for (var i = start; i < end; i++) {
total += i;
}
start += 2;
end++;
}
return total;
}