• Post Reply Bookmark Topic Watch Topic
  • New Topic

Pi Calculation program  RSS feed

 
Chris Green
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

Ok so since last week I started learning a little bit of Java. In have never programmed before in my life, so keep that in mind :-) I started with the online youtube tutorials from Derek Banas and now I am trying to create my own little program. This program asks the user to input a number on to how many times there has to be a calculation to PI. The bigger the number, the closer the calculation gets to PI. At the end of the first while loop, a question is shown to the user if he wants to continue. If he responds with Y or y the program has to check what the last previous calculated value was and has to continue again from that new value...again the user gets the question how many times from there on there has to be calculated to PI. So far so good...however there seems to be a problem with the code.

Example the first time I enter 3...calculation runs, I respond y and enter 3 again ==> calculation continues ==> OK BUT when first time I enter 3, calculations runs, I respond y and enter for example 100,....the values are being calculation but go under 3.14..... So that below pi and not OK...

Below my code:

 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kris, and welcome to JavaRanch!

It looks like you are doing well with the code (there's a few stylistic things I would comment on, but they're beside the point); it's the algorithm that giving you a little bit of trouble. Here's the problem: you are not doing something in your second while loop that you are doing in your first one. Take a closer look at the code and see if you can spot it (hint: take a look around line 54). If you can't find it, let me know and I will point it out, but you will learn better if you find the answers yourself!

Once you get that working, I would recommend trying to split your algorithm for calculating pi to a separate method. That way, when you call the method, you are certain it is running the same code and you don't suffer from copy-and-paste errors.
 
Chris Green
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joel McNary wrote:Hi Kris, and welcome to JavaRanch!

It looks like you are doing well with the code (there's a few stylistic things I would comment on, but they're beside the point); it's the algorithm that giving you a little bit of trouble. Here's the problem: you are not doing something in your second while loop that you are doing in your first one. Take a closer look at the code and see if you can spot it (hint: take a look around line 54). If you can't find it, let me know and I will point it out, but you will learn better if you find the answers yourself!

Once you get that working, I would recommend trying to split your algorithm for calculating pi to a separate method. That way, when you call the method, you are certain it is running the same code and you don't suffer from copy-and-paste errors.


Hi Joel,

Thanks for the feedback. I figured out what was wrong...I forgot to add 4 to my newFooter variable. I also understand why you say I am better to put the algorithm in a separate method. That way I can call this method each time it needs to be ran. However....I haven't gotten that far in the video course to learn how to write separate methods and calling them afterwards. :-)

When I reach that part I will adapt the code :-)

Thanks for the help.

Kris.
 
Chris Green
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joel,

I used a method now....Is this way OK ? or would you change something ? The program seems to work fine:

 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kris De Cooman wrote:Hi Joel,

I used a method now....Is this way OK ? . . .
No. You still have too much code in the main method. It is also unusual for the main method to call itself recursively, rather than using a loop. You can probably simply write main(args);
Consider the nextBoolean method of Scanner as an alternative. Always start class names with CapitalLetters.
 
Chris Green
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Kris De Cooman wrote:Hi Joel,

I used a method now....Is this way OK ? . . .
No. You still have too much code in the main method. It is also unusual for the main method to call itself recursively, rather than using a loop. You can probably simply write main(args);
Consider the nextBoolean method of Scanner as an alternative. Always start class names with CapitalLetters.


I have written it a bit different. Keep in mind that I am a beginner @ java knowing only the very basics so far. However I am trying to learn as much as I keep during my free time.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:No. You still have too much code in the main method. It is also unusual for the main method to call itself recursively, rather than using a loop.

And any recursive solution with this algorithm is likely to have problems, because it approaches the real value of π so slowly. Java has strict limits to how many times you can recursively call a method, and I suspect you'll get a StackOverflowError long before you reach even 5 decimal places of accuracy.

Which is not to say it's not a great exercise, and you're doing really well with it; but in this case recursion just isn't going to work very well.
So my suggestion: once you've tackled the business of getting as much code as possible out of main(), see if you can work out how to "unroll" the recursion into a normal loop.

And in case you're interested, the series you're using is called the Gregory-Leibnitz series, but there are others that converge quite a bit faster, while still remaining relatively simple, like Nilakantha's:


There is also the simple continued fraction, which is faster still, but requires a table of multipliers (this link contains the first 20,000), since there's no known pattern to them.
However, it gives 3/1, 22/7, 333/106, 355/113 and 103993/33102 from the first 5 iterations, a couple of which - the 2nd and the 4th - may already be familiar; and the 5th one is already good to 9 decimal places.

There are even faster ones, like Ramanujan and Chudnovsky, which add many decimal places of accuracy per iteration (14 in the latter case), but they're not really practical for Java as they involve huge numbers and also the calculation of a square root.

Hope it helps; and good luck with your project.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!