Euler Problem 15

by Andrew Poes

HTML

<!-- Dynamic Programing Route Finding -->

CSS

.print {
    position: relative;
    display: inline-block;
    background-color: black;
    color: white;
    font-family: Helvetica, Helvetica-Neue, sans-serif;
    font-weight: bold;
    font-size: 24px;
    letter-spacing: -1.5px;
    padding: 4px 8px;
}

body {
    background-color: #eeeeee;
}
}

JavaScript

$(document).ready(function() {
    var start = new Date().getTime()

    var size = 21
    var grid = []

    for (var y = 0; y < size; ++y) {
    	for (var x = 0; x < size; ++x) {
            var index = y * size + x
            if (y == 0 || x == 0) {
                grid[index] = 1
            }
            else {
				var left = y * size + (x - 1)
                var up = (y - 1) * size + x
                grid[index] = grid[left] + grid[up]
            }
        }
    }
    
    print(grid[size*size - 1])
	
	var moves = 4
    var top = 1
    var bot = 1
    for (var i = moves; i > 0; --i) {
        top *= i
        if (i%2==0) {
            bot *= (i/2)
        }
    }
    bot = bot*bot
    //print(Math.floor(top/bot))
    binomialCoefficient(40,20)
    
    var end = new Date().getTime()
	var time = end - start
	print('Execution time: ' + time + "ms")
})

function row(index, cols) {
    return Math.floor(index / cols)
}

function col(index, cols) {
    return index%cols
}
    
function binomialCoefficient(n, k) {
    /* N-choose-k combinatorics: (n! / (k! * (n-k)!)
     * Where:
     * 		n is the number of moves,
     * 		k is the number of down and right moves required (20 each) */
    if (k > (n-k)) {
        k = n - k
    }
    var c = 1
    for (var i = 0; i < k; i++) {
        c = c * (n-i) / (i + 1)
    }
    return c
}

function print() {
    var args = Array.prototype.slice.apply(arguments)
    var str = ""
    for (arg of args) {
        str += arg + ", "
    }
    str = str.substring(0, str.length - 2)
    var el = newel(str)
    $("body").append(el)
    $("body").append("</br>")
}

function newel(str) {
    var el = document.createElement("div")
    $(el).html(str)
    $(el).addClass("print")
    return el
}