Skip to content

Instantly share code, notes, and snippets.

@3ki5tj
3ki5tj / mword.py
Created November 18, 2014 23:04
Symbolic dynamics: list median words shorter than a certain length
#!/usr/bin/env python
''' list median words shorter than a certain length
cf. Chapter 3.3 of ``Elementary Symbolic Dynamics'' B-l, Hao '''
def ncom(a, b):
''' length of the common leading part of two given words '''
n = min(len(a), len(b))
for i in range(n):
if a[i] != b[i]: return i
@3ki5tj
3ki5tj / invsqrt.c
Created November 18, 2014 22:52
Inverse square root function
/*
* http://www.beyond3d.com/content/articles/8/
* http://www.beyond3d.com/content/articles/15/
*/
#include <stdio.h>
#include <math.h>
float inv_sqrt(float x)
{
float xh = 0.5f*x;
@3ki5tj
3ki5tj / 24all.c
Created November 18, 2014 22:49
The 24 game, enumerate ways of reaching 24 from combinations of four numbers from 1 to 10
#include <stdio.h>
#include <string.h>
#define N 20 /* max length of expression >= 2*ncard = 8 */
#define MAXDIG 10 /* max digit: note the digit should less than 42 */
int ncard = 4; /* number of cards */
double target = 24; /* target number */
#define zero(x) ((x) < 1e-5 && (x) > -1e-5)
#define clear(a) memset(a, 0, sizeof(a))
@3ki5tj
3ki5tj / lightsout.html
Created November 18, 2014 21:25
Lightsout puzzle
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" >
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>Lights out</title>
<style type="text/css">
/*<![CDATA[*/
body {
font: 90% "MS Trebunchet", "Lucida Grande", 'Verdana', 'Tahoma', 'Calibri', sans-serif;
width: 600px;
@3ki5tj
3ki5tj / bmpgray.c
Created November 18, 2014 21:21
Write a grayscale bitmap
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef unsigned char BYTE; /* one byte (0-255) */
typedef unsigned short WORD; /* two bytes */
typedef unsigned int DWORD; /* four bytes */
/* Bitmap format cf.
* http://en.wikipedia.org/wiki/BMP_file_format
@3ki5tj
3ki5tj / sortbybits.c
Created November 18, 2014 21:18
Enumerate 1..2^n by the number of bits
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int n; /* number of integers */
int m; /* current number of bits */
int top; /* current top */
int *st; /* current stack */
@3ki5tj
3ki5tj / mcpi.html
Created November 18, 2014 21:16
Estimate Pi by Monte Carlo sampling
<!DOCTYPE html>
<html>
<head>
<script>
function mcgetPi() {
var c = document.getElementById("mcpi");
var ctx = c.getContext("2d");
var w = c.width;
var h = c.height;
var r = Math.min(w/2, h/2) - 1;
@3ki5tj
3ki5tj / minesweep.html
Created November 18, 2014 21:14
Minesweep
<!DOCTYPE html>
<html>
<head>
<style>
body {
font: 90% "MS Trebunchet", "Lucida Grande", 'Verdana', 'Tahoma', 'Calibri', sans-serif;
width: 600px;
margin: auto;
}
@3ki5tj
3ki5tj / ljeos.html
Created November 18, 2014 21:11
Lennard-Jones equations of state (needs dygraph-combined.js for graphics)
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Lennard-Jones equation of state</title>
<script type="text/javascript" src="dygraph-combined.js"></script>
<script type="text/javascript">
//<![CDATA[
/* compute reference thermal dynamics variables
@3ki5tj
3ki5tj / salign.html
Last active August 29, 2015 14:09
Align two strings by dynamic programming (HTML/CSS/JavaScript)
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
.score_table {
background-color: #ffffd0;
font-family: "Courier New", monospace;
border-collapse: collapse;
border: 5px #ffffff groove;
}