If you are worried about 70 milliseconds then. The difference at 1,000,000 iterations is ~70 ms. Norm, try the version that I posted last with i (x in your case) %2, it is much faster. Last edited by webyugioh September 28th, 2008 at 02:56 PM. Input = Reader.nextInt() //sets user inputĭouble sum = 0 //sets up the sum variableįor (int i = 1 i 0) // if the remander of i/2 is >0 then add theĮlse // if the remander of i/2 is not >0 then subtract the Int input // this is initialized and given value by the user Scanner Reader = new Scanner(System.in) // set up the scanner variable Public class Proj45v2 // name the project Here is the program that works perfectly:Ĭode: import // import scanner util I am only a few weeks into programming so I do not understand everything you have there. If you don't mind, can you explain some things to me. Double literals have been used (1.0 instead of 1 etcetera) and the int i has been explicitly cast to double.I thank you very much, it works perfectly. This is to avoid implicit conversion surprises. Note that in the contribution calculation care has been taken to use double throughout. This approach would then utilize the fact that (i%2=0) evaluates to true when i is even and to false when i is odd. ("Pi is ", output*4.0) As an alternative you can decide whether the contribution is positive or negative by checking whether i is even or odd repectively.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |