Skip to content

Instantly share code, notes, and snippets.

View robot-dreams's full-sized avatar

Elliott Jin robot-dreams

  • San Francisco, CA
View GitHub Profile
@robot-dreams
robot-dreams / goldenSpiral.pde
Last active May 3, 2023 09:56
Draw a golden spiral with Processing
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 March 18, 2018 05:39
Project Euler #1 as an Ethereum contract (written in Solidity)
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 December 6, 2018 19:50
Fast approximation of n-body simulation via Barnes-Hut algorithm, O(n log n)
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 February 16, 2018 10:31
Useful pattern from "Concurrency in Go" for merging "done" channels
// 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 February 16, 2018 10:34
Morse Code Mnemonics (from Mind Performance Hacks)
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 January 16, 2017 22:31
Weight-biased leftist heap (exercise 3.3 of Purely Functional Data Structures)
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 January 17, 2017 00:43
Fibonacci in logarithmic time
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 December 29, 2016 19:49
Generate safe placements of n queens on a chessboard with n rows and n columns
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 January 15, 2017 05:46
Hamming's problem (enumerate all positive integers divisible by no primes other than 2, 3, or 5)
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 January 15, 2017 05:47
Next lexicographic permutation (iterative)
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