Last active
August 29, 2015 14:17
-
-
Save TimothyGu/1d05f4b02959c367d2ee to your computer and use it in GitHub Desktop.
This file contains 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
Day Due ($) Total ($) | |
0 0.00 1000.00 | |
1 100.00 1900.00 | |
2 200.00 3600.00 | |
3 400.00 6800.00 | |
4 800.00 12800.00 | |
5 1600.00 24000.00 | |
6 3200.00 44800.00 | |
7 6400.00 83200.00 | |
8 12800.00 153600.00 | |
9 25600.00 281600.00 | |
10 51200.00 512000.00 | |
11 102400.00 921600.00 | |
12 204800.00 1638400.00 | |
13 409600.00 2867200.00 | |
14 819200.00 4915200.00 | |
15 1638400.00 8192000.00 | |
16 3276800.00 13107200.00 | |
17 6553600.00 19660800.00 | |
18 13107200.00 26214400.00 | |
19 26214400.00 26214400.00 | |
20 52428800.00 0.00 | |
21 104857600.00 -104857600.00 | |
22 209715200.00 -419430400.00 | |
23 419430400.00 -1258291200.00 | |
24 838860800.00 -3355443200.00 | |
25 1677721600.00 -8388608000.00 | |
26 3355443200.00 -20132659200.00 | |
27 6710886400.00 -46976204800.00 | |
28 13421772800.00 -107374182400.00 | |
29 26843545600.00 -241591910400.00 | |
30 53687091200.00 -536870912000.00 |
This file contains 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
var PERCENT = 0.1 | |
, INITIAL = 1000 | |
, GROWTH = 2 | |
// get Devil's due | |
function getDue (day) { | |
if (day === 0) return 0 | |
return INITIAL * PERCENT * Math.pow(2, day - 1) | |
} | |
// Rather than using a recursive function, a loop is used to achieve O(n) | |
// Also saves stack space. | |
function getTotal (day) { | |
var total = INITIAL | |
// main loop for the integer part of the number | |
for (var i = 1; i <= day; i ++) { | |
total = total * GROWTH - getDue(i) | |
} | |
// fractional part of the number (part of the day) | |
var frac = day % 1 | |
if (frac) { | |
total += total * (GROWTH - 1) * frac - getDue(day) | |
} | |
return total | |
} | |
// binary search to find out what time exactly did it reach $1000. | |
var TARGET = INITIAL | |
, ITE = 25 | |
, cur = 0 | |
, min = 19 // (19, 26214400) | |
, max = 20 // (20, 0) | |
for (var i = 0; i < ITE; i ++) { | |
var avg = (min + max) / 2 | |
, out = getTotal(avg) | |
if (out > TARGET) min = avg | |
else max = avg | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment