1

I'm working on implementing this algorithm:

The Algorithm

Which should return Pi. (3.14159265358997...)

However, it is returning: 3465.083806164093990270538663167216844483674020103009669083093329738829995996594112113602427583738613083176438797806351846300982902722428833574050222861187694471396267405291545817609533108750954365354212195605941387622559085119176400306480675261092997442439408294603789105964390454395204651576460276909255907631487405486520824235883248771043874827661539987701699416841018021446826499678827570121235368306872576254306598229009326889717753996718734392718618075165049466487288359942244801903168934714614170309678757603506011866944372461588147498677098427847851318712433009748103294948229140898154267231085846307054977253156699130772999134183988575084372414985869913173854223041950981761979896495643515026760478550671129162390748164871541140497789062760779768626522387243316931878193393452785548737047784121894435472579674449705114248061506094340065691136629320777648629750105245428304278166365832749864653836658443868224823787898586712833767298344565051523963802742101107695594850821360398938016854610915

Here is my code:

package picalculator;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 *
 * @author ThomasSatterthwaite
 */

public class PiCalculator {

static int odd=1;

public static void main(String[] args) {
    System.out.println("Please wait while I calculate pi...");
    calculatePi();
    System.out.println("I have successfully calculated pi.");
}
public static void calculatePi() {
    BigInteger firstFactorial;
    BigInteger secondFactorial;
    BigInteger firstMultiplication;
    BigInteger firstExponent;
    BigInteger secondExponent;
    int firstNumber = 1103;
    BigInteger firstAddition;
    BigDecimal currentPi = BigDecimal.ONE;
    BigDecimal pi = BigDecimal.ONE;
    BigDecimal one = BigDecimal.ONE;
    int secondNumber = 2;
    double thirdNumber = Math.sqrt(2.0);
    int fourthNumber = 9801;
    BigDecimal prefix = BigDecimal.ONE;
    DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

    for(int i=1;i<1000;i++){
        firstFactorial = factorial(4*i);
        secondFactorial = factorial(i);
        firstMultiplication = BigInteger.valueOf(26390*i);
        firstExponent = exponent(secondFactorial, 4);
        secondExponent = exponent(BigInteger.valueOf(396),4*i);
        firstAddition = BigInteger.valueOf(firstNumber).add(firstMultiplication);
        currentPi = currentPi.add(new BigDecimal(firstFactorial.multiply(firstAddition)).divide(new BigDecimal(firstExponent.multiply(secondExponent)), new MathContext(10000)));
        Date date=new Date();
        System.out.println("Interation: " + i + " at " + dateFormat.format(date));
    }

    prefix =new BigDecimal(secondNumber*thirdNumber);
    prefix = prefix.divide(new BigDecimal(fourthNumber), new MathContext(1000));

    currentPi = currentPi.multiply(prefix, new MathContext(1000));

    pi = one.divide(currentPi, new MathContext(1000));

    System.out.println("Pi is: " + pi);

    return;
}
public static BigInteger factorial(int a) {

    BigInteger result=new BigInteger("1");
    BigInteger smallResult = new BigInteger("1");
    long x=a;
    if (x==1) return smallResult;
    while(x>1)
    {
        result= result.multiply(BigInteger.valueOf(x));

       x--;
    }
    return result;
}
public static BigInteger exponent(BigInteger a, int b) {
    BigInteger answer=new BigInteger("1");

    for(int i=0;i<b;i++) {
        answer = answer.multiply(a);
    }

    return answer;
}

}

Is anyone able to spot a problem with what I am doing? Thanks so much in advance!

10
  • You're gonna need to do better than Math.sqrt(2.0);... Commented Mar 20, 2012 at 18:59
  • 2
    @Ramhound I suppose the OP wants more than just double precision. Commented Mar 20, 2012 at 19:05
  • 1
    @Ramhound This series converges extremely quickly. So 1000 terms is more than enough for 1000 digits. Commented Mar 20, 2012 at 19:13
  • 2
    @Ramhound Most homework involves doing things that have already been done, unless you are a phD candidate. Commented Mar 20, 2012 at 19:26
  • 1
    Then as @Mysticial said, you need to get the value of sqrt(2) with more precision. Try computing it your self for more fun :) Commented Mar 21, 2012 at 0:00

2 Answers 2

4

You aren't handling the first partial sum correctly. You start your loop with i=1 with the initial partial sum currentPi=1, when you should start with i=0 and currentPi=0.

(This would have worked if the first partial sum (when k=0) is equal to 1, but it's in fact equal to 1103.)

Sign up to request clarification or add additional context in comments.

1 Comment

@Toby You should also pay attention to how many of the digits are actually correct. Since you using Math.sqrt(2.0);, I bet that only the first 16 digits or so are correct. Also, if this answer solves your problem, you should accept it by clicking on the green checkmark. Same applies with your previous questions.
2

You should start your loop with i = 0. and with currentPi = 0.

// ...
BigDecimal currentPi = BigDecimal.ZERO;
// ...
for(int i = 0; i < 1000; i++) {
    // ...
}
// ...

This gives:

3.1415926535897930237...

3 Comments

While it is marginally accurate, no matter what number I put in for the iterations I get the same result, I've tried 1, 10, 100, 1000 and 2000 but they all give the identical result. Is this to be expected, or have I messed up a portion of the code?
No, they don't give don't give the same result, so check again. But, this series converges quickly, so you already get more than 50 correct decimals for just 10 iterations. For 1 iteration, you have about 7 correct decimals.
No matter what I put in for the amount of iterations, I get 15 correct decimal places of pi. Could this be a problem with my IDE (Netbeans)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.