0

So I wanted to do something different so I tried to create a program to calculte Pi. I am still new to programming so forgive me for my atrocity of a program.

import java.math.BigDecimal; import java.math.RoundingMode; public class pi { public static BigDecimal pie = new BigDecimal(4); public static void pi(int count, int a, int stop){ BigDecimal count1 = new BigDecimal(count); BigDecimal a1 = new BigDecimal(a); pie= pie.add(new BigDecimal(4).divide(a1.multiply(count1), 20, BigDecimal.ROUND_HALF_UP)); System.out.println(pie); if (stop<1000000) pi(count*-1, a+2, stop+1); } public static void main(String[] args) { // TODO Auto-generated method stub pi(-1,3,0); } } 

Result:

2.66666666666666666667

3.46666666666666666667

2.89523809523809523810

3.33968253968253968254

...

...

3.14137796917835535721

3.14180729192172768736

3.14137806131758561203

3.14180719982203792401

and then I got a stack overflow exception.

My question is what is wrong with my program, as I checked 4 of the first recursions give the right result but the program can't still get to pi as more recursions happen.

Edit: thanks for all of the comments and answers. Now that I know better, I will use better series and write the program iteratively. I've read about the downsides of recursions before but didn't see it for myself until now.

Another question I have now is does recursion have a limit in the number of loops? Because in this situation it apparently does.

6
  • 4
    I guess that 1 million nested recursive calls is too much for your call stack. Try to reduce that number. Commented Sep 17, 2019 at 13:51
  • 1
    Code it iteratively in a loop, not recursively. Commented Sep 17, 2019 at 13:53
  • 1
    "What's wrong" is that the stack is overflowing because you're out of stack space. Recursion is great for many things, but iterative calculations are not one of them since the JVM does not implement tail call optimization (which isn't a direct fit for your current version of the code anyway, but it could be). Commented Sep 17, 2019 at 13:59
  • The series you have picked converges extremely slow. Much faster converging series exist. Commented Sep 17, 2019 at 14:07
  • 1
    This is just how recursion works. As a rule of thumb, you should only solve a problem recursively when it greatly improves readability and won't cause an overflow. Commented Sep 17, 2019 at 14:49

1 Answer 1

1

I would suggest to remove recursion. It leads you to StackOverflowError. And some refactoring:

import java.math.BigDecimal; import static java.math.RoundingMode.HALF_UP; public class pi { public static BigDecimal pie = new BigDecimal(2); public static void pi(int count) { BigDecimal count1 = new BigDecimal(count); BigDecimal a1 = new BigDecimal(count-1); BigDecimal a2 = new BigDecimal(count+1); pie = pie.multiply(count1.pow(2).divide(a1, 20, HALF_UP).divide(a2, 20, HALF_UP)); System.out.println(pie); } public static void main(String[] args) { for (int i = 2; i < 1000000; i=i+2) { pi(i); } } } 

PS: I's Wallis' product for π. It is working for 10 minutes and value is 3.1415297443583857077035169813896585428463586724137842214143641304426802276061154436561964553155700401625655010

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

1 Comment

Thanks for the answer. I'll try rewriting the program iteratively and with a better algorithms :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.