2012-03-20 3 views
1

를 계산합니다 : 파이를 반환해야합니다구현하는 알고리즘은 내가이 알고리즘을 구현하는 중이 야 PI

The Algorithm

합니다. 그러나 (3.14159265358997 ...)

, 그것은 반환 : 3465.083806164093990270538663167216844483674020103009669083093329738829995996594112113602427583738613083176438797806351846300982902722428833574050222861187694471396267405291545817609533108750954365354212195605941387622559085119176400306480675261092997442439408294603789105964390454395204651576460276909255907631487405486520824235883248771043874827661539987701699416841018021446826499678827570121235368306872576254306598229009326889717753996718734392718618075165049466487288359942244801903168934714614170309678757603506011866944372461588147498677098427847851318712433009748103294948229140898154267231085846307054977253156699130772999134183988575084372414985869913173854223041950981761979896495643515026760478550671129162390748164871541140497789062760779768626522387243316931878193393452785548737047784121894435472579674449705114248 여기

061506094340065691136629320777648629750105245428304278166365832749864653836658443868224823787898586712833767298344565051523963802742101107695594850821360398938016854610915 내 코드입니다 :

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; 
} 

} 

사람이 내가 무슨 일을하고 문제를 발견 할 수 있습니까? 미리 감사드립니다.

+0

당신은'Math.sqrt (2.0);보다 더 잘 수행 할 필요가 있습니다. ... – Mysticial

+0

1/Math.PI가 당신에게 같은 결과를 줄 때 실제로 그 공식을 구현하는 것은 어리석은 것처럼 보입니다. –

+2

@Ramhound 나는 OP가 단순히 '이중'정밀도 이상을 원한다고 생각한다. – Mysticial

답변

2

0

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

이 제공 :

3.1415926535897930237 ...

+0

아주 잘 작동합니다. 정말 고마워요 !!!!! – Toby

+0

거의 정확하지만 반올림을 위해 어떤 숫자를 입력했는지에 관계없이 동일한 결과를 얻었지만 1, 10, 100, 1000 및 2000을 시도했지만 모두 동일한 결과를 제공합니다. 예상되는 부분입니까, 아니면 일부 코드를 엉망으로 만든 부분입니까? – Toby

+0

아니요, 그들은 동일한 결과를 제공하지 않으므로 다시 확인하십시오. 그러나이 시리즈는 신속하게 수렴되므로 단 10 번의 반복만으로 50 자 이상의 올바른 소수 자릿수를 얻을 수 있습니다. 1 반복의 경우, 올바른 7 자리가 올바르게 나타납니다. – sch

4

첫 번째 부분 합계를 올바르게 처리하지 않습니다. i=0currentPi=0으로 시작해야하는 경우 i=1 및 초기 부분 합계가 currentPi=1 인 루프를 시작합니다.

는 (첫 번째 부분 합이 ( k=0)를 1 같은 경우는이 일 것이다,하지만 1103과 동일한 사실이다) 당신은 내가 = 0과와 currentPi =로 루프를 시작해야

+0

그것은 매우 효과적입니다. – Toby

+0

@Toby 또한 실제로 올바른 숫자의 수에주의해야합니다. 'Math.sqrt (2.0);'을 사용하고 있기 때문에 처음 16 자리 정도만 맞을 것입니다. 또한이 답이 문제를 해결하면 녹색 확인 표시를 클릭하여 수락해야합니다. 이전 질문에도 동일하게 적용됩니다. – Mysticial

+0

아, 네가 할 수 있다는 것을 몰랐어, 고마워! – Toby

관련 문제