Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- b)
- class MontyPi {
- public static def main(args:Array[String](1)) {
- val N = Int.parse(args(0));
- val P = Int.parse(args(1));
- var result:Double = 0;
- finish for (1..P) async {
- val r = new Random();
- val res:Double = 0;
- for (1 .. (N/P)) {
- val x = r.nextDouble();
- val y = r.nextDouble();
- if (x*x + y*y <= 1) res++;
- }
- atomic result += res;
- }
- val pi = 4*result/N;
- Console.OUT.println("The value of pi is " + pi);
- }
- }
- c)
- class MontyPi {
- private static def f(loops:Int):Double {
- val r = new Random();
- val res:Double = 0;
- for (1 .. loops) {
- val x = r.nextDouble();
- val y = r.nextDouble();
- if (x*x + y*y <= 1) res++;
- }
- return res;
- }
- public static def main(args:Array[String](1)) {
- val N = Int.parse(args(0));
- val P = Int.parse(args(1));
- var curplace:Place = here;
- var result:Double = 0;
- finish for (1..P) {
- curplace = curplace.next();
- val pl = curplace;
- async {
- val res = at (pl) f(N/P);
- atomic result += res;
- }
- }
- val pi = 4*result/N;
- Console.OUT.println("The value of pi is " + pi);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement