Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

calculate pi in java

    private static double calcPi(final int iterations){
        double x;
        double y;
        int successCount = 0;
        for (int i = 0; i <= iterations; i++){
            x = Math.random();
            y = Math.random();
            if ((Math.pow(x, 2) + Math.pow(y, 2)) <= 1){
                successCount++;
            }
        }
	    return (double) (4 * successCount) / iterations;
    }
 
PREVIOUS NEXT
Tagged: #calculate #pi #java
ADD COMMENT
Topic
Name
3+7 =