Skip to content

Instantly share code, notes, and snippets.

Avatar

Elliott Jin robot-dreams

  • San Francisco, CA
View GitHub Profile
@robot-dreams
robot-dreams / goldenSpiral.pde
Last active Oct 7, 2022
Draw a golden spiral with Processing
View goldenSpiral.pde
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.
@robot-dreams
robot-dreams / project-euler-001.sol
Last active Mar 18, 2018
Project Euler #1 as an Ethereum contract (written in Solidity)
View project-euler-001.sol
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) {
@robot-dreams
robot-dreams / barnes_hut.go
Last active Dec 6, 2018
Fast approximation of n-body simulation via Barnes-Hut algorithm, O(n log n)
View barnes_hut.go
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).
//
@robot-dreams
robot-dreams / or_chan.go
Last active Feb 16, 2018
Useful pattern from "Concurrency in Go" for merging "done" channels
View or_chan.go
// 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{})
@robot-dreams
robot-dreams / morse.txt
Last active Feb 16, 2018
Morse Code Mnemonics (from Mind Performance Hacks)
View morse.txt
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 --.
@robot-dreams
robot-dreams / wbl_heap.sml
Created Jan 16, 2017
Weight-biased leftist heap (exercise 3.3 of Purely Functional Data Structures)
View wbl_heap.sml
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
@robot-dreams
robot-dreams / fib.hs
Last active Jan 17, 2017
Fibonacci in logarithmic time
View fib.hs
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
@robot-dreams
robot-dreams / queens.hs
Last active Dec 29, 2016
Generate safe placements of n queens on a chessboard with n rows and n columns
View queens.hs
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
@robot-dreams
robot-dreams / hamming.hs
Last active Jan 15, 2017
Hamming's problem (enumerate all positive integers divisible by no primes other than 2, 3, or 5)
View hamming.hs
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)
@robot-dreams
robot-dreams / next_permutation.go
Last active Jan 15, 2017
Next lexicographic permutation (iterative)
View next_permutation.go
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