SHARE
TWEET

PI Buffon

a guest Apr 9th, 2019 108 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. // Config
  3. $times = 1000000; // Lanzamientos de aguja
  4. $size = 30; // Tamaño de aguja
  5. $lines = 40; // Cantidad de líneas
  6. $separation = $size; // Separación entre líneas (tiene que ser igual al tamaño)
  7.  
  8. $gridsize = $lines * $separation;
  9.  
  10. $count = 0;
  11.  
  12. for($i = 0; $i < $times; $i++)
  13. {
  14.     // Generamos aguja
  15.     $position = rand(0, $gridsize);
  16.     $angle = rand(0, 90);
  17.    
  18.     // Detectamos las líneas de arriba y abajo
  19.     $upper = floor($position / $separation) * $separation;
  20.     $bottom = ceil($position / $separation) * $separation;
  21.    
  22.     // Borde arriba y abajo de la aguja
  23.     $up = $position - sin(deg2rad($angle)) * $size/2;
  24.     $down = $position + sin(deg2rad($angle)) * $size/2;
  25.    
  26.     if($up <= $upper || $down >= $bottom)   // Si tocan o pasan por encima...
  27.         $count++;                           // Sumamos 1 al contador
  28.    
  29.     //echo 'Pos: ', $position, ' | a: ', $angle, ' | Upper: ', $upper, ' | Bottom: ', $bottom, ' | Up: ', $up, ' | Down: ', $down, "\n"; // DEBUG
  30. }
  31.  
  32. echo 'Colisiones: ', $count, '/', $times, "\n";
  33.  
  34. // Calculemos PI
  35. echo 'PI = ', 2*$times/$count; // Aplicamos la fórmula
  36.  
  37. while(true){} // Mantengamos la consola abierta
  38. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top