|
I tried attacking the integrals directly. If you think about it, you only need to handle the angles from -45° to +45° due to the fourfold symmetry of the problem. Furthermore, counting the white squares from -45° to 0° is the same as counting the black squares from 0° to 45°, so the problem is the can be solved by counting all squares from 0° to 45° and dividing by 2. Divide up the angles from 0° to 45° into 22 subdomains (atan(0),atan(1/8)),(atan(1/8),atan(1/7)),..., (atan(7/8),atan(1)) separated by the angles of the lines that can intersect more than one lattice point. For each angle in a given subdomain we can divide up the chessboard into areas of constant number of total squares intersected by at line of that slope. Then we add up area*number of squares intersected and integrate over all angles in the subdomain. Add up results for all subdomains and divide by the area of chessboard*90° and we have the average value. 'Value' below is my purported result; 'value1' is a check (should be 1/2.)
program buffon implicit none integer, parameter :: dp = selected_real_kind(15,300) real(dp) :: num(23) = (/0,1,1,1,1,1,2,1,3,2,3,1,4,3,5,2,5,3,4,5,6,7,1/) real(dp) :: den(23) = (/1,8,7,6,5,4,7,3,8,5,7,2,7,5,8,3,7,4,5,6,7,8,1/) real(dp) theta(23) integer n real(dp) phi real(dp) value real(dp) value1 integer i0, j0 real(dp) frac(0:8) logical top logical bottom real(dp) a1, a2, a3 real(dp) b1, b2, b3 integer nsq integer i, j
value = 0 value1 = 0 theta = atan(num/den) do n = 1, 22 top = .TRUE. bottom = .FALSE. a1 = 0 a2 = 0 a3 = 0 nsq = 0 phi = (theta(n)+theta(n+1))/2 i0 = 0 j0 = 0 do frac = i0-((/(j,j=0,8)/)-j0)*tan(phi) j = maxloc(modulo(frac, 1.0_dp), 1, frac > -1 .AND. frac < 8)-1 i = ceiling(i0-(j-j0)*tan(phi)) if(top) then b1 = i*j b2 = 0.5_dp*i**2 b3 = 0.5_dp*j**2 if(j0
|