Skip to content

Instantly share code, notes, and snippets.

@horitaku1124
Last active August 13, 2019 02:38
Show Gist options
  • Save horitaku1124/c7bf14c5b533479d3b7465bf1ec72f3c to your computer and use it in GitHub Desktop.
Save horitaku1124/c7bf14c5b533479d3b7465bf1ec72f3c to your computer and use it in GitHub Desktop.
import java.math.BigDecimal;
import java.math.RoundingMode;
class CalcPi {
public static void main(String[] args) {
var start = System.currentTimeMillis();
final long RADIUS = 3000000000L;
final long RADIUS_E2 = RADIUS * RADIUS;
long count = 0;
System.out.println("RADIUS_E2=" + RADIUS_E2);
long offsetX = RADIUS;
for (long y = 0;y < RADIUS;y++) {
long y2 = y * y;
for (long x = offsetX;x > 0;x--) {
long distance_e2 = x * x + y2;
if (distance_e2 < RADIUS_E2) {
offsetX = x;
count += offsetX;
break;
}
}
}
BigDecimal counted2 = new BigDecimal(count);
BigDecimal devider = new BigDecimal(RADIUS / 2).multiply(new BigDecimal(RADIUS / 2));
BigDecimal pi = counted2.divide(devider, 50, RoundingMode.HALF_DOWN);
System.out.println("counted2=" + counted2);
System.out.println("pi=" + pi);
// System.out.println(counted / RADIUS / RADIUS);
System.out.println(System.currentTimeMillis() - start + "ms");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment