-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEstimatePi.java
More file actions
31 lines (21 loc) · 738 Bytes
/
Copy pathEstimatePi.java
File metadata and controls
31 lines (21 loc) · 738 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com;
import java.math.BigDecimal;
import java.util.Arrays;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaSparkContext;
public class EstimatePi {
public static void main(String[] args) {
SparkConf sConf = new SparkConf().setAppName("EstimatePi").setMaster("local");
JavaSparkContext sc = new JavaSparkContext(sConf);
int slices = 2;
int n = 100000*slices;
double count = sc.parallelize(Arrays.asList(n), slices)
.map(m ->{
double x = Math.random()*2-1;
double y = Math.random()*2-1;
return(x*x + y*y <1)?1:0;
}).reduce((a,b) -> a+b);
System.out.println("Pi is roughly " + BigDecimal.valueOf(4.0 * count / n).toPlainString());
sc.close();
}
}