Results 1 to 5 of 5
 11012014, 10:56 PM #1Member
 Join Date
 Oct 2014
 Posts
 20
 Rep Power
 0
how to find a sum of series in java
Can someone explain to me how to find the sum of the following series
f(x)= 1+3+5+.......+2n+1
this what I did
Java Code:for ( n=0;x>0 && x<2*n+1;n++) { sum=sum+2*n+1; n++; System.out.println(sum);
is this correct ? it was in my exam and I think I massed up everything!
 11012014, 11:16 PM #2Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 14
Re: how to find a sum of series in java
First, it normally goes to 2n  1 so if n = 1 the first term is 1.
It is well known that the sum of an arithmetic series of odd numbers
starting with 1 is a square so you could just use:
sum = n*n; // for any number of n odd terms.
In any event you probably got a compiler error because x is not defined anywhere.
Regards,
JimLast edited by jim829; 11022014 at 02:56 PM. Reason: grammar and spelling and a bit more clarification
The Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 11022014, 06:07 PM #3Senior Member
 Join Date
 Apr 2013
 Location
 Sweden
 Posts
 272
 Rep Power
 8
Re: how to find a sum of series in java
Also, if you are faced with any arithmetic progression, you can use the formula:
(n(2a + (n  1) d)) / 2
where a is the first term, d is the common difference, and n is the number of terms you wish to sum.
of course in this case, as mentioned, your series is n^2.
 11042014, 04:38 PM #4Member
 Join Date
 Oct 2014
 Posts
 20
 Rep Power
 0
Re: how to find a sum of series in java
I didn't get it why is the sum=n*n
is this what u meant
Java Code:public class Review{ public static void main(String[] args){ int x=1,n=0,sum=0; for (n=0;x<=2*n+1 ; n++) { sum=sum+n*n; } System.out.println("sum ="+ sum); } }
sum= 1610612736
last question is this infinite loop?
Thank youLast edited by skj; 11042014 at 05:26 PM.
 11042014, 06:25 PM #5
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 28
Similar Threads

Draw series of concentric circles using java.util.Random
By rciri in forum New To JavaReplies: 12Last Post: 09152013, 09:09 PM 
Java Tutorials  The "How to" series for Java! Tower Defence, 3D, Platformer, Topdown
By Ulixava in forum Reviews / AdvertisingReplies: 2Last Post: 04082012, 11:58 PM 
alternating series sum java help
By java157 in forum New To JavaReplies: 18Last Post: 03202011, 03:41 AM 
Generating Fibonacci Series with a Multithreaded Java Program
By firesauce in forum Threads and SynchronizationReplies: 1Last Post: 10202009, 07:26 AM 
How to add a second series in jfreechart
By Manfizy in forum New To JavaReplies: 1Last Post: 03232009, 11:16 AM
Bookmarks