![]() ![]() ![]() 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! Take a closer look at the code and see if you can spot it (hint: take a look around line 54). Here's the problem: you are not doing something in your second while loop that you are doing in your first one. 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. Joel McNary wrote:Hi Kris, and welcome to JavaRanch! NewPi = newPi - (4/newFooter) + (4/(newFooter+2)) ("Do you want to continue calculating ? Y/N ? ") ("How many times do you want to calculate to PI ? ") Static Scanner myInput = new Scanner(System.in) So far so good.however there seems to be a problem with the code.Įxample 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. 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. At the end of the first while loop, a question is shown to the user if he wants to continue. The bigger the number, the closer the calculation gets to PI. This program asks the user to input a number on to how many times there has to be a calculation to PI. 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. 10^8 range 10 repeats: 327.Ok so since last week I started learning a little bit of Java.Point 3 ( x*(1/y) -> x/y) fix only after I was almost done with timings and noticed I didn't list this change above: That gives us around 4.8 times faster code I am currently running timeit for more accurate times. Note: the timings were done manually with datetime. ![]() (I made it a parametrized function for quicker adjustment of range) I think (and timings below suggest it) that I did it between points 1 and 2. I also changed x*(1/y) which is two operations into just x/y. 10^9 iterations completed after 1.5 minutes. If you want to get into really high values of i, this small detail might make a difference.įor 100 million iterations, it further improved time measured, to 8 seconds. I*2+1 is not too bad, but it's just previous value+2. That's almost 3x less than your original solution. Or just check parity of i.Īfter fixing it, for 100 million iterations I get around 13 seconds. ![]() Solution: make the new value be minus previous value. For computer, it has to calculate the power over and over again. (-1)**i - for humans, we can get the result instantly based on parity of i. However, there are some places where you can improve your code: 100 million - 38 seconds.Īs the comments pointed out, the method is generally slow. When I enter exactly 10 million, it takes a bit less than 3.8 seconds. When I try to enter more than 10 million in the range it gets really slow ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |