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
float PHI = (1 + sqrt(5)) / 2; | |
void goldenSpiral(float h) { | |
// Base case: stop drawing if the height is too small. | |
if (h < 2) { | |
return; | |
} | |
// Draw bounding box and quarter circle. For some reason, using 2 * h - 1 | |
// looks better than using 2 * h. |
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
pragma solidity ^0.4.0; | |
contract ProjectEuler001 { | |
// For the low price of 1000 wei, you can solve Project Euler Problem 1! | |
uint price = 1000; | |
event Solved(uint n, uint result); | |
// Returns the n-th triangular number 1 + 2 + ... + n | |
function t(uint n) private pure returns (uint) { |
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
package main | |
import ( | |
"math" | |
) | |
// We represent a region of space, containing some number of particles, as an | |
// OctTree. Leaves of the tree contain at most 1 particle (but are also allowed | |
// to represent empty regions). | |
// |
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
// Returns a channel that will be closed whenever ANY of the input channels are | |
// closed (or written to). | |
func or(channels ...<-chan struct{}) <-chan struct{} { | |
switch len(channels) { | |
case 0: | |
return nil | |
case 1: | |
return channels[0] | |
} | |
orDone := make(chan struct{}) |
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
unstressed syllables -> dot (short) | |
stressed syllables -> dash (long) | |
a-BOUT .- | |
BOIS-ter-ous-ly -... | |
CARE-less CHIL-dren -.-. | |
DAN-ger-ous -.. | |
eh? . | |
fe-ne-STRA-tion ..-. | |
GOOD GRA-vy --. |
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
functor WBLHeap(Element : ORDERED) : HEAP = | |
struct | |
structure Elem = Element | |
datatype Heap = E | |
| T of int * Elem.T * Heap * Heap | |
val empty = E | |
fun isEmpty E = true |
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
fib :: Int -> Integer | |
fib n = | |
let fibTriplet k | |
| k <= 0 = (0, 0, 1) | |
| k == 1 = (0, 1, 1) | |
| even k = let (a, b, c) = fibTriplet (k `div` 2) | |
in (a^2 + b^2, b * (a + c), b^2 + c^2) | |
| odd k = let (a, b, c) = fibTriplet (k - 1) | |
in (b, c, b + c) | |
snd3 (_, y, _) = y |
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
import Control.Monad (foldM) | |
-- All safe placements of n queens, on a board with n rows and n columns; a | |
-- placement is safe if no two queens are attacking each other | |
queens :: Int -> [[(Int, Int)]] | |
queens n = | |
foldM (extend n) [] [1..n] | |
-- All possible ways to extend, on a board with n rows, an existing safe | |
-- placement of queens by adding an additional queen in column j |
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
hamming :: [Integer] | |
hamming = | |
1 : merge3 | |
(map (*2) hamming) | |
(map (*3) hamming) | |
(map (*5) hamming) | |
where | |
merge (x:xs) (y:ys) = | |
case compare x y of | |
LT -> x : merge xs (y:ys) |
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
func nextPermutation(a []int) { | |
// Find the greatest index i such that a[i] < a[i + 1] | |
i := len(a) - 2 | |
for i >= 0 && a[i] >= a[i+1] { | |
i-- | |
} | |
if i >= 0 { | |
// Find the greatest index j > i such that a[i] < a[j]; such an index | |
// exists because a[i] < a[i + 1] | |
j := len(a) - 1 |
NewerOlder