Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace CalculatePi
- {
- struct Point
- {
- public double x;
- public double y;
- }
- class Program
- {
- static void Main(string[] args)
- {
- var startdate = DateTime.Now;
- double n = 20000000;
- Point p;
- Random rand = new Random();
- int count = 0;
- for (int i = 0; i < n; i++)
- {
- p.x = rand.NextDouble() * 2 - 1;
- p.y = rand.NextDouble() * 2 - 1;
- if (IsInCircle(p)) count++;
- }
- var enddate = DateTime.Now;
- Console.WriteLine("In case of {0} numbers, result came in {1} seconds", n, (enddate-startdate).TotalSeconds);
- Console.WriteLine("Result is: " + 4.0 * count / n);
- Console.WriteLine("Math.PI: " + Math.PI);
- Console.ReadLine();
- }
- static bool IsInCircle(Point p)
- {
- return Math.Sqrt(p.x * p.x + p.y * p.y) < 1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment