Skip to content

Instantly share code, notes, and snippets.

View jremmen's full-sized avatar

John Remmen jremmen

  • Planning Center
  • San Diego
View GitHub Profile
@jremmen
jremmen / nqueens.js
Last active August 29, 2015 14:03
js: n Queens solver
Queens = function(num) {
function range(n) {
return Array.apply(null, new Array(n)).map(function(_, i) { return i; });
};
function is_safe(q, qs) {
if(qs.length == 0) return true;
return qs.filter(function(x, i) {
return Math.abs(q - x) != Math.abs(qs.length - (qs.length - (i + 1)));
@jremmen
jremmen / gist:ff775efa98e58813fa3b
Last active August 29, 2015 14:16
js: prime factorization examples
prime_factorization = function(n) {
var i = 2;
var factors = [];
while (n > 0 && i <= n) {
while (n % i == 0) {
factors.push(i);
n = n / i;
}
i++;
}
@jremmen
jremmen / set.js
Last active August 29, 2015 14:17
js: recursive 2 set operations
function diff(a, b) {
return _operation(a, b, [], diff, true)
}
function union(a, b) {
return _operation(a, b, b, union, true)
}
function intersect(a, b) {
return _operation(a, b, [], intersect)
@jremmen
jremmen / iterator.js
Last active August 29, 2015 14:19
lazy iterator
var Iterator = function(m, n) {
this.start = m;
this.end = n || Infinity;
this.step = 1;
this.current = this.start;
this.queue = new IteratorQueue();
this.next = function() {
var c = this.current;
this.current += this.step;
@jremmen
jremmen / random_generators.rb
Created April 20, 2015 03:31
Some random generators in ruby based on Coursera reactive programming course
module Generators
module Utils
def self.included(other)
other.extend(ModuleMethods)
end
module ModuleMethods
def arbitrary
@arbitrary ||= {}.tap do |o|
o[Integer] = ->{Random.rand(-1e+10..1e+10).to_i}