Results 1 to 7 of 7
 11152015, 07:41 PM #1Member
 Join Date
 Oct 2013
 Posts
 63
 Rep Power
 0
Is this the most efficient solution for taking the sum?
Here are two methods, iterative and recursive, which calculates the sum of all positive integers between 1 and a given integer n (input into the method). We know that as n gets larger, the solution takes longer to solve. Is there you think a much more efficient way of writing these codes?
NOTE: I'VE CROSS POST HERE Is this the most efficient solution for taking the sum? (Beginning Java forum at JavaRanch)
I'll let everyone know whether my answer is resolved from the other forum so I don't waste anybody's time. Thanks!
Iterative Method:
Java Code:public static int iterativeSum(int n) { int sum = 0; if ((n == 0)  (n == 1)) { return n; } for (int i = 0; i < n ; n) { sum = sum + n; } return sum; }
Recursive Method:
Java Code:public static int recursiveSum(int n) { int sum = 0; if ((n == 0)  (n == 1)) { return n; } else { sum = recursiveSum(n1) + n; } return sum;
Last edited by csanch11; 11152015 at 07:44 PM.
 11152015, 07:46 PM #2Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 14
Re: Is this the most efficient solution for taking the sum?
The Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 11152015, 08:32 PM #3Member
 Join Date
 Oct 2013
 Posts
 63
 Rep Power
 0
Re: Is this the most efficient solution for taking the sum?
Hey jim, I would have never thought of that. Is that some sort of formula that you look up, or does it come from experience?
 11152015, 09:11 PM #4Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 14
Re: Is this the most efficient solution for taking the sum?
Experience based on math courses. Probably algebra or whenever one learns about summation of infinite and finite series (both arithmetic and geometric series). Here is how it's derived.
S = 1 + 2 + 3 + 4 + 5 + ..... + n
S = n + n1 + n2 + n3 + n4 + ......+ 1
Add each column of corresponding terms.
2S = 1+n + 1+n + 1+n + 1+n + .....................1+n (and since you have n of these)
2S = n(1+n) now divide by 2
S = n(1+n)/2
And as a bonus, if you square the above formula, you get the sum of consecutive cubes from 1 to n.
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 11152015, 09:26 PM #5Member
 Join Date
 Oct 2013
 Posts
 63
 Rep Power
 0
Re: Is this the most efficient solution for taking the sum?
Why did S = 1 + 2 + 3 turn into n + n 1 + n2 ???
 11152015, 09:31 PM #6Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 14
Re: Is this the most efficient solution for taking the sum?
The Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 11212015, 07:47 AM #7Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,716
 Rep Power
 18
Re: Is this the most efficient solution for taking the sum?
> (Jim) And as a bonus, if you square the above formula, you get the sum of consecutive cubes from 1 to n.
Wikipedia gives a lovely picture proof of this: https://upload.wikimedia.org/wikiped...rem_3D.svg.png
Similar Threads

most efficient way to write xml
By mondol in forum XMLReplies: 1Last Post: 11052013, 04:13 PM 
Tell me efficient way
By lkalaivanan in forum New To JavaReplies: 3Last Post: 08112011, 11:36 AM 
is there a more efficient way?
By Yakg in forum New To JavaReplies: 3Last Post: 01232011, 05:32 PM 
Need a simple and efficient solution.
By Samgetsmoney in forum New To JavaReplies: 9Last Post: 02202009, 01:18 AM
Bookmarks