From The Compiler, 5 Years ago, written in C#.
Embed
  1. using System;
  2.  
  3. namespace Pi {
  4.     class Program {
  5.         static void Main(string[] args) {
  6.             int i;
  7.             PiCalc pic = new PiCalc();
  8.  
  9.             for (i=0; i<=65535; i++) {
  10.                 pic.iter();
  11.             }
  12.             Console.WriteLine(pic.getpi());
  13.         }
  14.     }
  15.     class PiCalc {
  16.         // Count of points inside the circle
  17.         private Counter inside = new Counter(0);
  18.         // Total count of points
  19.         private Counter total = new Counter(0);
  20.         // The value of pi
  21.         double pival = 0; // Whahaha, pi equals 0!
  22.  
  23.         public void iter(void) {
  24.             // This works by placing a random dot in a quarter circle which is
  25.             // in a square. Based on the ratio on dots inside the circle
  26.             // compared to the total count of dots, we can calculate pi.
  27.             //
  28.             // The circle has the radius r = 1.
  29.             Random rng = new Random();
  30.             px = rng.Next(0, 1);
  31.             py = rng.Next(0, 1);
  32.             Console.WriteLine("DEBUG: point {}/{}", px, py);
  33.             total.inc();
  34.  
  35.             // We determine if the point is inside the circle by using
  36.             // Pythagoras' theorem. If x^2 + ^2 >= r^2 then the point is
  37.             // inside. r = 1, thus r^2 = 1.
  38.             if (px^2 + py^2 >= 1) {
  39.                 inside.inc();
  40.             }
  41.             pival = 2*(total/inside);
  42.             Console.WriteLine("DEBUG: pi after {} iters: {}", total, pival);
  43.         }
  44.        
  45.         public double getpi(void) {
  46.             return pival;
  47.         }
  48.     }
  49. }
  50.  
  51. /*
  52. pi.cs(23,29): error CS1525: Unexpected symbol `)', expecting `<operator>' or `identifier'
  53. pi.cs(45,32): error CS1525: Unexpected symbol `)', expecting `<operator>' or `identifier'
  54. */