Results 1 to 6 of 6
 02182013, 09:12 PM #1Member
 Join Date
 Feb 2013
 Posts
 63
 Rep Power
 0
Program to calculate sum of n integers need help
I am writing code to display the output of a sum of this form:
1 + (1/2) + (1/3) + ... + (1/n)
where n = 50,000
When calculating the sum: once it must be summed from left to right and display the output i.e. the value of the sum
then again it must be summed going right to left.
This is to show the difference in the accuracy of the calculation when it is done from L>R and viceversa.
Here is the code I have written:
Java Code:public class AccurateResult { public static void main(String[] args) { double n = 50000; double sum = 1 / 50000; while(n >= 0) { sum += (double) ( 1 / (n  1) ); n; break; } System.out.println(" The solution from right to left is: " + (double) sum); n = 0; sum = 0; do { sum += (double) ( 1 / (n + 1) ); ++n; } while( n <= 50000); System.out.print(" The solution from left to right is: " + sum); } }
My algorithm for the R > L sum is not correct.
Because computing that algorithm would lead to (1 / (11) ) which = infinity and (1 / (0 1) ) which would be 1 for the second last and last term when they should be 1/2 and 1 respectively. So can you help me get the right algorithm?Last edited by abi; 02182013 at 09:40 PM.
 02192013, 12:31 AM #2Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,151
 Rep Power
 12
Re: Program to calculate sum of n integers need help
Unless you are compelled to use while loops it might be easier to use a for loop. The first loop would start at 1 and increment to 49999. The next loop would start at 49999 and decrement to 1.
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 02192013, 01:03 AM #3Member
 Join Date
 Feb 2013
 Posts
 63
 Rep Power
 0
Re: Program to calculate sum of n integers need help
So basically, for Right to Left
for(n = 49999; n<=1 ; n)
{
sum+= (1 / n)
}
 02192013, 01:42 AM #4Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,151
 Rep Power
 12
Re: Program to calculate sum of n integers need help
Almost! In this case n >= 1 or n > 0.
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 02192013, 01:58 AM #5Member
 Join Date
 Feb 2013
 Posts
 63
 Rep Power
 0
Re: Program to calculate sum of n integers need help
Oh yeha thats what I mean. Thanks.
I just deleted what I had previously written and did the whole thing with the clear mind and it worked at one go. Haha. I think I am suffering from brain drain or something.
Here it is
Java Code:public class Accu { public static void main(String[] args) { double n ; double sum = 0; for( n = 50000; n > 0 ; n) { sum += ( 1 / n ); } System.out.println(" sum from r to l: " + sum); sum = 0; for( n = 1 ; n <= 50000; ++n ) { sum+= (1 / n); } System.out.print(" sum from l to r: " + sum); } }
 02192013, 03:16 AM #6Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,151
 Rep Power
 12
Re: Program to calculate sum of n integers need help
Yep! Been there myself. It happens.
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads

Program help/ Integers
By KMayers in forum New To JavaReplies: 2Last Post: 11292012, 02:33 PM 
Can't compile simple program to calculate primenumber
By Daniel Silvester in forum New To JavaReplies: 4Last Post: 04062012, 05:07 AM 
Program in Java To calculate GCD of n numbers.?
By ankitsinghal_89 in forum New To JavaReplies: 4Last Post: 02152011, 10:23 AM 
How to ask for several integers from the program user?
By busdude in forum New To JavaReplies: 1Last Post: 10202008, 08:55 PM 
Why my program cannot calculate the decimal value?
By pearllymary78 in forum New To JavaReplies: 4Last Post: 06232008, 01:52 AM
Bookmarks