Created
November 3, 2012 12:05
-
-
Save joriki/4007189 to your computer and use it in GitHub Desktop.
On a randomly uniformly segmented circle, find the measure of the circle lying in segments that are smaller than at least one of the adjacent segments; see http://math.stackexchange.com/questions/227990
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
public class Question227990 { | |
final static int nbirds = 5; | |
final static int ntrials = 10000000; | |
static double [] birds = new double [nbirds]; | |
static double length (int index) { | |
double length = birds [(index + nbirds) % nbirds] - birds [(index + nbirds - 1) % nbirds]; | |
return length < 0 ? length + 1 : length; | |
} | |
public static void main (String [] args) { | |
int count = 0; | |
for (int n = 0;n < ntrials;n++) { | |
for (int i = 0;i < nbirds;i++) | |
birds [i] = Math.random (); | |
Arrays.sort (birds); | |
double point = Math.random (); | |
int bird; | |
for (bird = 0;bird < nbirds && point > birds [bird];bird++) | |
; | |
if (length (bird) < length (bird - 1) || length (bird) < length (bird + 1)) | |
count++; | |
} | |
System.out.println (count / (double) ntrials); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment