Skip to content

Instantly share code, notes, and snippets.

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Random;
public class Question4390837 {
final static int a = 2;
final static int b = 2;
final static int ntrials = 100000000;
package games.chess.fide;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
public class WorldRankingExtracter {
public static void main(String [] args) throws IOException {
int rank = 0;
@joriki
joriki / Question3706460.java
Last active June 5, 2020 05:44
Count the binary strings that can be generated by alternatingly adding zeros and ones to either end; see https://math.stackexchange.com/questions/3706460.
import java.util.HashSet;
import java.util.Set;
public class Question3706460 {
public static void main(String [] args) {
Set<Long> strings = new HashSet<>();
strings.add(1L);
long bit = 2;
boolean one = false;
for (int n = 1;;n++,bit <<= 1,one = !one) {
@joriki
joriki / Question3674864.java
Created May 14, 2020 19:17
Simulate how long it takes until six dice that retain a 1 for 4 turns all show a 1; see https://math.stackexchange.com/questions/3674864.
import java.util.Random;
public class Question3674864 {
final static long ntrials = 10000000;
final static Random random = new Random();
public static void main (String [] args) {
long count = 0;
for (long trial = 0;trial < ntrials;trial++) {
int [] state = new int [6];
@joriki
joriki / Question3674784.java
Created May 14, 2020 16:55
Find perfect squares whose difference is a repunit; see https://math.stackexchange.com/questions/3674784.
import java.math.BigInteger;
public class Question3674784 {
public static void main(String [] args) {
for (int n = 1;;n++) {
System.out.println(n);
BigInteger diff = BigInteger.TEN.pow(n).subtract(BigInteger.ONE).divide(BigInteger.TEN.subtract(BigInteger.ONE));
BigInteger x = BigInteger.TWO;
BigInteger x2 = x.multiply(x);
BigInteger y = BigInteger.ONE;
@joriki
joriki / Question3667404.java
Created May 11, 2020 03:59
Simulate the number of uniformly distributed quarter circle sectors required to cover a circle; see https://math.stackexchange.com/questions/3667404.
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
public class Question3667404 {
final static long ntrials = 10000000;
final static Random random = new Random();
static class Range {
@joriki
joriki / Question3664030.java
Created May 8, 2020 06:55
Count intervals of size 1/n hit by the numbers 1/k; see https://math.stackexchange.com/questions/3664030.
public class Question3664030 {
public static void main(String [] args) {
for (int n = 1;n < 2000;n++) {
int count = 0;
boolean [] hit = new boolean [n];
for (int k = 1;k <= n + 1;k++) {
double x = 1. / k;
// to count values on boundaries only towards the lower interval, subtract 1e-12 here ...
int index = (int) (n * x);
if (index < n && !hit [index]) {
@joriki
joriki / Question3629762.java
Created April 17, 2020 19:23
Find all magic 4x4 squares in which the sum in each 2x2 subsquare is the magic sum; see https://math.stackexchange.com/questions/3629762.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Question3629762 {
static class Constraint {
int [] cells;
int sum;
public Constraint(int [] cells) {
@joriki
joriki / Question3629474.java
Created April 17, 2020 05:38
Simulate the angle between two vectors uniformly randomly picked in the unit square; see https://math.stackexchange.com/questions/3629474.
import java.util.Random;
public class Question3629474 {
final static long ntrials = 100000000;
final static Random random = new Random();
public static void main(String [] args) {
double sum = 0;
for (long n = 0;n < ntrials;n++)
sum += Math.abs(angle() - angle());
@joriki
joriki / Question3620483.java
Created April 13, 2020 15:35
Calculate a term in the expansion of the fraction of configurations that don't cause a black hole to form; see https://math.stackexchange.com/questions/3620483.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import BallsInBins; // https://gist.github.com/joriki/d324726fbc3fe4ccdf33628aa02b790c
import BigRational; // https://gist.github.com/joriki/3097452fbb9e1983daf1035f57e72722
import Binomials; // https://gist.github.com/joriki/5a5c03d2c286effa69584e77a064a6e3