Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. b)
  2. class MontyPi {
  3. public static def main(args:Array[String](1)) {
  4. val N = Int.parse(args(0));
  5. val P = Int.parse(args(1));
  6.  
  7. var result:Double = 0;
  8. finish for (1..P) async {
  9. val r = new Random();
  10. val res:Double = 0;
  11. for (1 .. (N/P)) {
  12. val x = r.nextDouble();
  13. val y = r.nextDouble();
  14. if (x*x + y*y <= 1) res++;
  15. }
  16. atomic result += res;
  17. }
  18. val pi = 4*result/N;
  19. Console.OUT.println("The value of pi is " + pi);
  20. }
  21. }
  22.  
  23. c)
  24. class MontyPi {
  25. private static def f(loops:Int):Double {
  26. val r = new Random();
  27. val res:Double = 0;
  28. for (1 .. loops) {
  29. val x = r.nextDouble();
  30. val y = r.nextDouble();
  31. if (x*x + y*y <= 1) res++;
  32. }
  33.  
  34. return res;
  35. }
  36.  
  37. public static def main(args:Array[String](1)) {
  38. val N = Int.parse(args(0));
  39. val P = Int.parse(args(1));
  40. var curplace:Place = here;
  41. var result:Double = 0;
  42. finish for (1..P) {
  43. curplace = curplace.next();
  44. val pl = curplace;
  45. async {
  46. val res = at (pl) f(N/P);
  47. atomic result += res;
  48. }
  49. }
  50. val pi = 4*result/N;
  51. Console.OUT.println("The value of pi is " + pi);
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement