import java.awt.geom.Point2D; /* Scott R. Shannon. March 4th 2019. Code for producing numerical approximation to A306648 . Note that the basic Java Math.random isn't very good, so don't expect perfect convergence */ public class SAWStep3 { public static void main(String[] args) { long totSamples = 0; double sumEtoE = 0.0; Point2D b1 = new Point2D.Double(0.0,0.0); // First circle - not needed, but add for completion Point2D b2 = new Point2D.Double(1.0,0.0); // Just fix the second circle at (1,0) as other angles b/n 0 to 2Pi are equivalent by rotational symmetry while (true) { // Choose an angle b/n +2Pi/3 to -2Pi/3 so the third circle does not collide with the first ... double b3Angle = Math.random()*4.0*Math.PI/3.0-2.0*Math.PI/3.0; Point2D b3 = new Point2D.Double(b2.getX()+Math.cos(b3Angle),b2.getY()+Math.sin(b3Angle)); // Fourth circle goes in random position around the third, but chose angle b/n +2Pi/3 to -2Pi/3 from angle 'b3Angle' so the fourth circle does not collide with the second circle... double b4Angle = b3Angle+Math.random()*4.0*Math.PI/3.0-2.0*Math.PI/3.0; Point2D b4 = new Point2D.Double(b3.getX()+Math.cos(b4Angle),b3.getY()+Math.sin(b4Angle)); // As we started at (0,0) this will just be the square of the distance of the centre of fourth circle to the centre of the first circle... double b4DistSq = b4.getX()*b4.getX()+b4.getY()*b4.getY(); if (b4DistSq<1.0) continue; // Fourth circle collided/overlaps with the first circle - as we are using 'simple sampling' throw the walk away and try again // Else we have a SAW of 3 steps (4 circle chain)... sumEtoE+=b4DistSq; ++totSamples; if (totSamples%10000000==0) System.out.println(totSamples+": "+sumEtoE/totSamples); // running mean of the square end-to-end distance } } }