Skip to content

Instantly share code, notes, and snippets.

@3ki5tj
3ki5tj / madelung.c
Created November 18, 2014 20:39
Compute the Madelung constant by Ewald sum
#define N 8
double unitcell[3] = {2.0, 2.0, 2.0};
double x[N][3] = {{0,0,0},{0,0,1},{0,1,0},{0,1,1},{1,0,0},{1,0,1},{1,1,0},{1,1,1}};
double q[N] = {1,-1,-1,1,-1,1,1,-1};
static double dot(double a[3], double b[3])
{
return a[0]*b[0] + a[1]*b[1] + a[2]*b[2];
}
@3ki5tj
3ki5tj / mtrand.h
Last active August 6, 2020 06:48
Mersenne Twister random number generator
#ifndef MTRAND_H__
#define MTRAND_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
/* Mersenne Twister was developed by Makoto Matsumoto and Takuji Nishimura */
#define MT_N 624
@3ki5tj
3ki5tj / salign.c
Created November 18, 2014 20:49
Align two strings by dynamic programming
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define xnew(x, n) \
if ((x = calloc(n, sizeof(*(x)))) == NULL) { \
fprintf(stderr, "no memory for %s x %u\n", #x, (unsigned) (n)); \
exit(1); }
/* return the best match of two strings */
@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;
}
@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 / 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 / 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 / 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 / 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 / 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;