0
package ValueOfPi;
import java.math.*;
import java.util.Scanner;

public class RamanujanAlgorithm {

    public static void main(String[] args) {
        
        double  constant = 0.0002885855652;
        
        Scanner sc = new Scanner(System.in);
        
        System.out.println("Enter limit of summation : ");
        
        int limit = sc.nextInt();
        
        BigDecimal pireci = new BigDecimal("0.00");
        
        for(int n = 0; n<=limit;n++) {
            
            BigDecimal summation = new BigDecimal((factorial(4*n)/Math.pow(factorial(n), 4))* 
                    (26390*n + 1103)/Math.pow(396, 4*n));
            
            
            pireci = pireci.add(summation);
        }
        
        pireci = pireci.multiply(BigDecimal.valueOf(constant)); 
        
    
        
        BigDecimal pi = BigDecimal.ONE.divide(pireci,pireci.scale(),RoundingMode.HALF_UP);
        
    
        
        System.out.println(pi);
    }
    
    public static long factorial(int num) {
        
        long factorial = 1;
        
        for(int i = 2; i<=num; i++) {
            
            factorial = factorial*i;
            
        }
        return factorial;
        
        
    }

}

SOURCE OF FORMULA -

The Ramanujan Formula I used is -

https://en.wikipedia.org/wiki/Pi#Rapidly_convergent_series The first formula you see is the one used by me...

EXPLANATION OF MY CODE

In my code, I have first stored the value of root(8)/9801 in the variable named constant. Then I have taken user input for the upper limit of the summation... Then the loop works like the Sigma, where everything on the right side of the sigma in the formula is computed and stored in the BigDecimal pireci and at last after coming out of the loop, I have multiplied pireci with the variable constant

The result is stored in pireci. So, for getting pi, I find the reciprocal of pireci(because the formula gives me value of 1/pi) and print the result There is also one method called factorial which I have made to compute the factorial in the formula

RESULTS PRODUCED

On putting limit = 1 I get -

3.14159265383525216155408817653305805189369961082170142700979067898169315680169553

On putting limit = 10 I get -

3.141592653835251522017825683538367933441136808152187507749300560259859937277166891211043856677465587830817787411902252466648641215763228301502439410432665788310356378854402610761037687401200267944051297889143892295707008454487636735727641182639754208990826247667192290712119662965838581189023690921554268043065123435639930191440467906529034361442072184293368529887693375871414064191030918467799966026346716917502424050190813

Whereas, the real value of pi is

3.1415926535 8979323846 2643383279 5028841971 6939937510 5820974944 5923078164 0628620899 8628034825 3421170679 8214808651 3282306647 0938446095......

MY QUESTION

You see, after 9 digits the result obtained by my code starts differing with the real value. Why does this happen???? Is it due to the formula or due to my code??? Please help me. I have also read that the precision increases with increase in the limit of summation. I tried using a larger limit, still its coming out wrong.

And thank you in advance

0 Answers0