Skip to content

Instantly share code, notes, and snippets.

View biesnecker's full-sized avatar

biesnecker

View GitHub Profile
@biesnecker
biesnecker / chakravala.clj
Created January 28, 2012 02:34
Project Euler 66
(defn natural?
"Returns true if n is a natural number (greater than zero, and with no
fractional component), else false."
[n]
(and (> n 0) (zero? (rem n 1))))
(defn nth-sgonal
"For a given s-gonal number P(s,n) = x, returns n (n is a natural number if
x is a term of the given s-gonal sequence)."
^double [^long x ^long s]
@biesnecker
biesnecker / stirling-dynamic.clj
Created January 31, 2012 03:59
Stirling numbers
(defn- stirling-loop-internal
[^long n ^long k compfunc]
{ :pre [(<= k n)] }
(let [workitems (for [n' (range 1 (inc n)) k' (range 0 (inc (- n k)))
:when (and (>= (- n' k') 0) (>= k (- n' k')))] [n' (- n' k')])]
(loop [current (first workitems) items (rest workitems) results {}]
(cond
(= current [n k]) ((compfunc current results) current)
(= (current 0) (current 1))
(recur (first items) (rest items)
@biesnecker
biesnecker / bell.clj
Created February 7, 2012 03:01
Bell numbers
(defn bell
"Returns the nth Bell number"
[n]
(cond
(= n 0) 1
(= n 1) 1
:else (reduce + (map #(abs (stirling-2 n %)) (range 0 (inc n))))))
(server/add-middleware
friend/authenticate
{ :credential-fn (partial creds/bcrypt-credential-fn users)
:workflows [(workflows/interactive-form)]
:login-uri "/login"
:unauthorized-redirect-uri "/login"
:default-landing-uri "/" })
(pre-route [:any "/app/*"] { :as req } (let [id (friend/identity req)]
(when-not (friend/authorized? [::user] id) (friend/throw-unauthorized id {}))))
@biesnecker
biesnecker / rflick.rb
Created December 12, 2012 09:28
Because sometimes you need to get pictures of kittens into your browser from the command line, fast. Usage: rflick.rb "search query" It will open the image in your browser (on OSX)
#!/usr/bin/env ruby
require 'flickraw'
FlickRaw.api_key = "YOUR FLICKR API KEY"
FlickRaw.shared_secret = "YOUR FLICKR SHARED SECRET"
ARGV[0] ||= "kittens"
search_query = ARGV[0]
@biesnecker
biesnecker / gist:4313816
Created December 16, 2012 22:45
Putting characters to the screen without a carriage return
def put (s)
print s
STDOUT.flush
end
@biesnecker
biesnecker / config.rb
Created February 17, 2013 09:31
Wiki-like automatic links for Middleman
# add this to your config.rb
ready do
wikitargets = Hash.new
wikiwords = Hash.new
sitemap.resources.select {|p| p.ext == ".html" }.each do |p|
unless p.data['wikitag'].nil?
wikitargets[p.data['wikitag']] = p.url
end
from math import sqrt, ceil, trunc
# Sieve of Eratosthenes
def seive(max):
nums = range(max + 1)
nums[0] = None
nums[1] = None
for i in range(2, max + 1):
if nums[i]:
c = i * 2
print(sum([x for x in range(1000) if x % 3 == 0 or x % 5 == 0]))
/** @jsx React.DOM */
var ReadingTimeWidget = React.createClass({
getInitialState: function() {
return ({
secondsRequired: null,
});
},
componentWillMount: function() {
var cdiv = this.props.contentDiv;