Last active
March 17, 2018 10:56
-
-
Save ff8c00/c9df8b136920bffb0872ad417be058f4 to your computer and use it in GitHub Desktop.
Reddit Daily Programmer 354 Intermediate
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
<html> | |
<head> | |
<script> | |
var map = []; | |
map[1] = 1; | |
function multiply(n) { | |
var A, i, j, k; | |
A = []; | |
i = n + 1; | |
for (j = 2; j < i; j++) { | |
k = n / j; | |
if (k != 1) { | |
if ((n % j) == 0) { | |
A.push([j, k]); | |
} | |
} | |
if (k < j) { | |
break; | |
} | |
} | |
return A; | |
} | |
function find(n) { | |
var A, B, i; | |
if (map[n] == undefined) { | |
A = multiply(n); | |
if (A.length != 0) { | |
B = []; | |
for (i = 0; i < A.length; i++) { | |
B.push(map[A[i][0]] + map[A[i][1]]); | |
} | |
B.push(1 + map[n - 1]); | |
map[n] = B.reduce(function(a, b) { | |
return Math.min(a, b); | |
}); | |
} else { | |
map[n] = 1 + map[n - 1]; | |
} | |
} | |
return map[n]; | |
} | |
function run() { | |
var n, i; | |
n = 0; | |
for (i = 1; i < 1000001; i++) { | |
n = n + find(i); | |
} | |
return n; | |
} | |
function stopwatch() { | |
var a; | |
var b; | |
a = new Date(); | |
run(); | |
b = new Date(); | |
return (b - a) / 1000; | |
} | |
</script> | |
</head> | |
<body onload="document.write(run())"> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment