Skip to content

Instantly share code, notes, and snippets.

@joriki
joriki / CospectralGraphs.java
Created February 8, 2012 07:22
Find all cospectral connected graphs on n vertices
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.Stack;
@joriki
joriki / Question107694.java
Created February 10, 2012 09:28
code for math.stackexchange question 107694 (http://math.stackexchange.com/a/107784): does division allow shorter arithmetic expressions with single digits as operands?
import java.util.Map;
import java.util.TreeMap;
public class Question107694 {
final static int n = 7;
static boolean handle (Map<Integer,String> newExpressions,String ej,String ek,int result,char op) {
boolean isNew = !newExpressions.containsKey (result);
if (isNew)
newExpressions.put (result,"(" + ej + ")" + op + "(" + ek + ")");
@joriki
joriki / Question118794a.java
Created March 11, 2012 15:03
Determine the number of transitions between conjugacy classes induced by scrambling three elements
// This version counts the transitions by tallying the different ways in which the scrambling can rearrange the cycles
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
class Partition {
List<Integer> parts = new ArrayList<Integer> ();
@joriki
joriki / Question118566.java
Created March 15, 2012 15:53
Brownian Backgammon simulation to test doubling strategies
import java.util.Random;
public class Question118566 {
public static void main (String [] args) {
double lambda0 = Double.parseDouble (args [0]);
double k0 = Double.parseDouble (args [1]);
double lambda1 = Double.parseDouble (args [2]);
double k1 = Double.parseDouble (args [3]);
int ntrials = 100000;
@joriki
joriki / Question129580.java
Created April 10, 2012 08:31
Code to count card combinations according to this math.SE question: http://math.stackexchange.com/questions/129580
import java.util.ArrayList;
import java.util.List;
public class Question129580 {
static List<int []> getPermutations (int n) {
return getPermutations (n,n);
}
static List<int []> getPermutations (int n,int k) {
List<int []> permutations = new ArrayList<int[]> ();
@joriki
joriki / Question130758.java
Created April 12, 2012 07:21
Count the number of tilings of an n by n square with n rectangles of integer sides and area n
// Count the number of tilings of an n by n square with n rectangles of integer sides and area n
// See http://math.stackexchange.com/questions/130758
public class Question130758 {
final static int maxn = 100;
static int n;
static int [] divisors = new int [maxn];
static int ndivisors;
static boolean [] [] grid;
@joriki
joriki / Question132847.java
Created April 17, 2012 18:45
Count the number of words over an alphabet of n letters without non-overlapping repeated strings of length 2
import java.util.Set;
import java.util.HashSet;
public class Question132847 {
static int n;
static int count;
static Set<String> pairs = new HashSet<String> ();
public static void main (String [] args) {
@joriki
joriki / Question33227.java
Created May 13, 2012 23:27
Find the optimal strategy in a variant of the best choice problem where the median is to be selected; see http://math.stackexchange.com/questions/33227
import java.util.Random;
public class Question33227 {
public static void main (String [] args) {
int n = Integer.parseInt (args [0]);
int n2 = n / 2;
int [] d = new int [n];
for (int i = 0;i < d.length;i++)
d [i] = -1;
@joriki
joriki / Question177465.java
Created August 1, 2012 09:34
Search for common multiples of Voyager polynomials with few monomials: http://math.stackexchange.com/questions/177465
import java.util.BitSet;
public class Question177465 {
static BitSet [] bits = {new BitSet (),new BitSet ()};
static int [] polynomials = {Integer.parseInt ("1011011",2),Integer.parseInt ("1111001",2)};
public static void main (String [] args) {
bits [0] = new BitSet ();
bits [1] = new BitSet ();
import java.util.List;
import java.util.ArrayList;
import java.util.Random;
public class Question176383 {
public final static Centre origin = new Centre (0,0);
static List<Centre> centres = new ArrayList<Centre> ();
static enum Ternary {