Guest User

PI Buffon

a guest
Apr 9th, 2019
174
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

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×