Finding the sum of a series below 15 terms
Author |
Message |
Shiro786
|
Posted: Fri Mar 23, 2007 2:56 pm Post subject: Finding the sum of a series below 15 terms |
|
|
First off, I'd like to say that I've been a lurker of these sites to see if there was a way to make a game in Turing. Thankfully, and successfully I made a simple pong-esque game. Thank you Comp Sci! This site is great. (This was all last year.)
Now, I'm at a halt. Java is getting a bit tough (gone through most of the book, our class is almost done! n__n)
So my question is this.
This is taken straight from the Exploring Java - Level One Java Textbook. If you have the book, it's page 181, question 4C.
"Find the sum of each of the series below to 15 terms. Use a class called SumofSeries (it basically says make your own.)
0.5 + 0.75 - 0.83 + 0.87 ...
I need to know if there is a way to code this. Note, I already know that a for loop is required (although I know some of them, functions are NOT allowed, this means no (Math.sqrt) shiate =P)
Thanks in advance. I look forward to seeing your answers. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Clayton
|
Posted: Fri Mar 23, 2007 3:19 pm Post subject: Re: Finding the sum of a series below 15 terms |
|
|
does the question specifically state whether the series is geometric or arithemetic? Finding the sum of either series is completely different from the other. |
|
|
|
|
|
PaulButler
|
Posted: Fri Mar 23, 2007 3:20 pm Post subject: RE:Finding the sum of a series below 15 terms |
|
|
A formula can be derived from most sequences so that you don't need to use a loop to find their sum.
In the case you provided, t(n) = ((2n - 1) / 2n)
I don't know off-hand the formula for this sum, but I'm sure there is one. |
|
|
|
|
|
Shiro786
|
Posted: Fri Mar 23, 2007 3:30 pm Post subject: Re: Finding the sum of a series below 15 terms |
|
|
Freakman @ Fri Mar 23, 2007 3:19 pm wrote: does the question specifically state whether the series is geometric or arithemetic? Finding the sum of either series is completely different from the other.
No it doesn't, I can also scan the question if you guys want.
PaulButler wrote:
A formula can be derived from most sequences so that you don't need to use a loop to find their sum.
In the case you provided, t(n) = ((2n - 1) / 2n)
I don't know off-hand the formula for this sum, but I'm sure there is one.
LMFAO! Shit man, I'm doing Arithmetic Equations like that in math now. And funny thing is I did that exact same thing. I showed my teacher and asked if I can do something like this, but he said since we're doing a For Loops chapter, we have to use loops. I have this done:
Quote: // The "SumofSeries2" class.
import java.awt.*;
import hsa.Console;
public class SumofSeries2
{
static Console c; // The output console
public static void main (String[] args)
{
c = new Console ();
double total = 0;
double num = 1;
double num2 = 2;
for (int i = 1 ; i <= 14 ; i++)
{
c.println (total, 0, 2);
total = (num / num2);
num = num + 2;
num2 = num2 + 2;
}
// Place your program here. 'c' is the output console
} // main method
} // SumofSeries2 class
Ignore all the comments and the hsa.console thing. That's what I have so far. But the thing is I have to add the numbers, and subtract them also. To output the negative and positive integers, would I have to use a variable (change) to be a -1 so that for every negative number it would turn positive or something? Damn I'm kind've confusing myself here >_>
Edit: Also, if it helps any, the 15th term is actually 0.3627 or something. That should be the answer. |
|
|
|
|
|
PaulButler
|
Posted: Fri Mar 23, 2007 4:20 pm Post subject: Re: Finding the sum of a series below 15 terms |
|
|
Shiro786 @ Fri Mar 23, 2007 3:30 pm wrote: Edit: Also, if it helps any, the 15th term is actually 0.3627 or something. That should be the answer.
Remember, you are looking for the sum. The sum is t1 + t2 + t3 + t4 + t5 + t6 + t7 + t8 + t9 + t10 + t11 + t12 + t13 + t14 + t15. Don't confuse the sum with the last term. |
|
|
|
|
|
Cervantes
|
Posted: Fri Mar 23, 2007 4:27 pm Post subject: RE:Finding the sum of a series below 15 terms |
|
|
Is that minus supposed to be there in your series?
Anyways, if you know whether a sequence is arithmetic or geometric, then you can use simple formulas to find the sum. However, unless you're told that the series is one or the other, you'll have to go through the whole series to check whether it is arithmetic or geometric, and then do the math. This is more work than going through the series and manually adding up the terms.
The main question I have is about how the data is given to you. Are you reading it in from a file, or keyboard input? Are you supposed to generate the terms based on some rules?
If you show us how you're given the data, we can easily illustrate how to sum of the terms.
--edit: Hmm, it seems a cosmic blip happened, in which the last two posts were made without my seeing them. Odd.
anyways, if you want to alternatively add then subtract terms, you don't need to keep another variable. You can simply check whether your index, i, mod 2 equals 0, or 1. If it's 0, add. If it's 1, subtract. Or perhaps in the other order, depending on how you want it. The other option is to simply multiply your term by (-1)^i, or perhaps (-1)^(i+1). This saves you an if statement, but involves a bunch of multiplications of -1. Even better would be to do (-1) ^ (i mod 2) or perhaps (-1) ^ (i mod 2 + 1). |
|
|
|
|
|
Shiro786
|
Posted: Fri Mar 23, 2007 5:10 pm Post subject: Re: Finding the sum of a series below 15 terms |
|
|
Yes the minus is supposed to be there. Cerventes. I have scanned the page:
It seems like the mod thing is more efficient. Can you elaborate on it though? I don't know what mod stands for (or is for that matter =P). Once again, thank for the help.
PaulButler wrote:
Shiro786 @ Fri Mar 23, 2007 3:30 pm wrote: Edit: Also, if it helps any, the 15th term is actually 0.3627 or something. That should be the answer.
Remember, you are looking for the sum. The sum is t1 + t2 + t3 + t4 + t5 + t6 + t7 + t8 + t9 + t10 + t11 + t12 + t13 + t14 + t15. Don't confuse the sum with the last term.
Sorry, I worded that incorrectly. I meant that the 15th term in the sequence should be 15. We're not supposed to add either. So it's t1 + t2 - t3 + t4... etc. |
|
|
|
|
|
Clayton
|
Posted: Fri Mar 23, 2007 5:37 pm Post subject: Re: Finding the sum of a series below 15 terms |
|
|
okay, here's your problem, you put everything into decimal. Keep everything as a rational expression, finding the numerator on it's own (n), and then finding the denominator on it's own (2n). Then multiply the entire fraction by -1 for every term following the 1st. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Shiro786
|
Posted: Fri Mar 23, 2007 5:47 pm Post subject: Re: Finding the sum of a series below 15 terms |
|
|
Damn. So is it like that? Quote: public static void main (String[] args)
{
c = new Console ();
double total = 0;
double num = 1;
double num2 = 2;
for (int i = 1 ; i <= 14 ; i++)
{
c.println (total, 0, 2);
total = (total + (num / num2)) * -1;
num = num + 2;
num2 = num2 + 2;
} [/quote] |
|
|
|
|
|
Clayton
|
Posted: Fri Mar 23, 2007 5:50 pm Post subject: Re: Finding the sum of a series below 15 terms |
|
|
Hint: Add the fractions, not the decimals.
Edit: mod is the mathematical operation called modulus. It's like division, except that it gives you the remainder of the division (an integer) as a result instead of a quotient. In Java (and other languages) it can be represented by the % operator. |
|
|
|
|
|
Shiro786
|
Posted: Fri Mar 23, 2007 6:02 pm Post subject: Re: Finding the sum of a series below 15 terms |
|
|
My num and num2 are the numerator and denominator respectively.
So is it the total statement that's wrong? I add it like this (total + (num / num2) but the thing is, you say that I have to add the fraction, not the decimal. How do I get it so that it remains as a rational expression?
Damnit...I'm really not getting this drilled in my head.
Sorry for being a nuisance Freakman. =3
Freakman @ Fri Mar 23, 2007 5:50 pm wrote: Hint: Add the fractions, not the decimals.
Edit: mod is the mathematical operation called modulus. It's like division, except that it gives you the remainder of the division (an integer) as a result instead of a quotient. In Java (and other languages) it can be represented by the % operator.
Ah that's what % was. If you can believe this, we've been using this without actually knowing what it was rofl. We knew it gave us the remainder of division though for sure. Just didn't know it was a math operation. =P
Thanks for the heads-up. |
|
|
|
|
|
Clayton
|
Posted: Fri Mar 23, 2007 6:40 pm Post subject: Re: Finding the sum of a series below 15 terms |
|
|
Well the thing is, when Java sees a division problem, it automatically reduces it to a decimal, you are then no longer adding fractions, but again decimals. Try keeping an array with all of the numerators, and an array with all of the denominators and work from there. |
|
|
|
|
|
bugzpodder
|
Posted: Fri Mar 23, 2007 7:04 pm Post subject: RE:Finding the sum of a series below 15 terms |
|
|
create a java fraction class |
|
|
|
|
|
wtd
|
Posted: Fri Mar 23, 2007 7:25 pm Post subject: Re: Finding the sum of a series below 15 terms |
|
|
Freakman @ Sat Mar 24, 2007 7:40 am wrote: Well the thing is, when Java sees a division problem, it automatically reduces it to a decimal, you are then no longer adding fractions, but again decimals. Try keeping an array with all of the numerators, and an array with all of the denominators and work from there.
Parallel arrays are the devil's favorite programming technique. |
|
|
|
|
|
klopyrev
|
Posted: Fri Mar 23, 2007 8:59 pm Post subject: Re: Finding the sum of a series below 15 terms |
|
|
a/b + c/d = ad/bd + bc/bd = (ad+bc)/bd. Also, if you know how, you could add in the gcd method in there to simplify the operation. Let u = lcm(b,d) = a*b/gcd(b,d). Then a/b + c/d = (au/b)/u + (cu/d)/u. The top 2 numbers would end up being integers, so you just add them normally.
KL |
|
|
|
|
|
|
|