Question: Please fix this code so it compiles import java.math.BigDecimal; import java.lang.Math; public class Sequence { public static BigDecimal fact( int n ) { BigDecimal result

Please fix this code so it compiles

import java.math.BigDecimal;

import java.lang.Math;

public class Sequence {

public static BigDecimal fact( int n ) {

BigDecimal result = BigDecimal.ONE;

while (n > 0) {

result = result.multiply(new BigDecimal(n + ""));

n--;

}

return result;

}

public static BigDecimal pow( BigDecimal a, int b ) {

BigDecimal result = a;

if(b == 0) {

return BigDecimal.ONE;

}

while ( b > 1 ) {

result = result.multiply(new BigDecimal(a + ""));

b--;

System.out.print(" " + result + " ");

}

return result;

}

public static BigDecimal Ramanujan( int n ) {

BigDecimal coefficient = BigDecimal.valueOf(Math.sqrt(8.0)).divide(BigDecimal.valueOf(9801)); //will multiply the sum by sqrt(8)/9801

BigDecimal pi_R = BigDecimal.ONE;

BigDecimal sum = BigDecimal.ZERO;

BigDecimal quotient;

BigDecimal numer;

BigDecimal denom;

for(int i = 0; i <= n; i++ ) {

BigDecimal bigN = BigDecimal.valueOf(n); //Now, when we need to do arithmetic with ints we can do that, and

//when we need arithmetic with BigDecimals we can do that.

numer = (BigDecimal.valueOf(26390).multiply(bigN));

numer = numer.add(BigDecimal.valueOf(1103));

numer = numer.multiply(fact( 4 * n ));

denom = pow( fact(n), 4 );

denom.multiply(pow(BigDecimal.valueOf(396), (4 * n)) );

quotient = numer.divide(denom);

sum.add(quotient);

}

pi_R = BigDecimal.ONE.divide(coefficient.multiply(sum));

return pi_R;

}

public static BigDecimal Chudnovsky( int n ) {

BigDecimal coefficient = BigDecimal.ONE.divide(BigDecimal.valueOf(53360.0).multiply(BigDecimal.valueOf(Math.sqrt(640320.0)))); //will multiply the sum by 1/53360sqrt(640320)

BigDecimal pi_C = BigDecimal.ONE;

BigDecimal sum = BigDecimal.ZERO;

BigDecimal quotient;

BigDecimal numer;

BigDecimal denom;

for(int i = 0; i <= n; i++ ) {

BigDecimal bigN = BigDecimal.valueOf(n);

numer = BigDecimal.valueOf(545140134).multiply(bigN);

numer = numer.add(BigDecimal.valueOf(13591409.0));

numer = numer.multiply(pow( BigDecimal.valueOf(-1), n ));

numer = numer.multiply(fact( 6 * n ));

denom = pow( fact(n), 3 );

denom = denom.multiply( fact( 3 * n ));

denom = denom.multiply( pow( BigDecimal.valueOf(340320), (3*n) ));

quotient = numer.divide(denom);

sum.add(quotient);

}

pi_C = BigDecimal.ONE.divide(coefficient.multiply(sum));

return pi_C;

}

public static void main(String[] args) {

BigDecimal ramanPi = Ramanujan(10000);

BigDecimal chudPi = Chudnovsky(10000);

System.out.print("Ramanujan's algorithm for pi, for n = 10,000: " + ramanPi + " ");

System.out.print("Chudnovsky brothers' algorithm for pi, for n = 10,000: " + chudPi);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!