
10-07-2008, 10:15 PM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 38
Rep Power: 0
|
|
(Help) Fraction Summation and Exponents
I'm making a program that's supposed to calculate the sum of this:
|
Code:
|
import java.util.Scanner;
public class Project5 {
public static void main (String[] args) {
Scanner scan = new Scanner (System.in);
System.out.println ("Enter n: ");
int n = scan.nextInt();
double sum = 0.0;
int x=1;
//I'm not sure if that's right, because I'm trying to make x be the numerator, so it can keep increasing by 1. Should I do that?
while (x<=n) {
if (x%2==1) {
sum += (x/ **how do I say 2 to the power of n?** );
}else{
sum -= (x/ **how do I say 2 to the power of n?** );
}
x++
} |
And how do I get it to reach that end summation, of (-1) to the power of n+1?
I'm not even sure if the stuff I have so far can possibly work. I just know that we're not allowed to use "Math.pow". Please help, I can't find any useful examples in my book or notes.
Last edited by SapphireSpark; 10-08-2008 at 05:52 AM.
Reason: to update the picture so its accurate
|
|

10-08-2008, 12:39 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
|
|
|
Quote:
|
|
calculate the sum of this
|
|
Quote:
|
|
summation, of (-1) to the power of n+1?
|
Can you explain with an algebraic expression or in english what you are trying to do?
Do you know what x to the power of y means?
x*x*x*x*... y times
Does that look like the place to use a loop?
|
|

10-08-2008, 05:54 AM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 38
Rep Power: 0
|
|
|
Yes, I know what x to the power of y means. I just can't figure out how to put it in a loop.
I updated the picture, its accurate now, that's what I'm trying to do: Find the sum of the sequence that continues until that final part is reached.
|
|

10-08-2008, 06:07 AM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 38
Rep Power: 0
|
|
|
So, if the user inputs n to equal 5, the last term in the summation sequence will be *1 times (5 over 32).
And the program displays the sum of all the terms that come before that last term, plus the last term.
|
|

10-08-2008, 07:15 AM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 6,506
Rep Power: 8
|
|
|
the poster is a spammer.
|
|

10-08-2008, 07:18 AM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 38
Rep Power: 0
|
|
|
hey, I said I was sorry for the previous topic, I really need help on this one. I'm not a spammer, it was the only way I could attach that picture on top.
Could someone please help me fix the loop statement? Please?
|
|

10-08-2008, 07:37 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,535
Rep Power: 11
|
|
Why don't you use the math class for that. It's most easiest as well as the best way to follow.
|
Code:
|
import java.util.Scanner;
public class FindSummation {
public static void main(String[] args) {
Scanner userScann = new Scanner(System.in);
System.out.println("Enter number of terms: ");
int terms = userScann.nextInt();
double sum = 0;
for(int i = 1; i <= terms; i++) {
sum += (Math.pow(-1, (i + 1))) * (i / (Math.pow(2, i)));
}
System.out.println(sum);
}
} |
|
|

10-08-2008, 07:37 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,535
Rep Power: 11
|
|
Originally Posted by Fubarable
|
|
the poster is a spammer.
|
Don't worry pal. I'll monitoring him, and if so I can ban the user.
|
|

10-08-2008, 07:38 AM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 38
Rep Power: 0
|
|
|
Thank you, but the only rule is that we are not allowed to use Math.pow. We are limited to if, else, or switch. And the result should be a double value. I've tried making this using those guildelines and examples, but I don't think its right. Can you please check my code?
Last edited by SapphireSpark; 10-08-2008 at 07:58 AM.
|
|

10-08-2008, 07:40 AM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 38
Rep Power: 0
|
|
|
Quote:
|
|
Don't worry pal. I'll monitoring him, and if so I can ban the user.
|
Thanks, but that won't be necessary, it was only so I could attach my explanation picture.  And, btw, I'm a girl, just confused by the class.
Your previous suggestion looks helpful, except for the useage of Math.pow. Is there a more basic translation for that, using simple operators, if possible?
Last edited by SapphireSpark; 10-08-2008 at 07:46 AM.
Reason: Expanding
|
|

10-08-2008, 08:00 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,535
Rep Power: 11
|
|
Originally Posted by SapphireSpark
|
|
Thank you, but the only rule is that we are not allowed to use Math.pow. We are limited to if, else, or switch. And the result should be a double value. I've tried making this using those guildelines and examples, but I don't think its right. Can you please check my code?
|
There are lots of ways to do it. Did you try any.
|
|

10-08-2008, 08:03 AM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 38
Rep Power: 0
|
|
|
Yes, I tried, and I thought maybe something to the nth power was a loop statement, but I have no idea how to make it work. The book doesn't mention variable powers in this chapter, and the examples from class were more like (value*value).
Also, for the first term, its not really 1, its (1/2), so that's not an integer, right? Will that mess up the result? All the class examples used int, so I'm not sure if I should change it to double, although it sounds right.
Last edited by SapphireSpark; 10-08-2008 at 08:12 AM.
|
|

10-08-2008, 08:59 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,535
Rep Power: 11
|
|
Here is the simple way to do it
|
Code:
|
import java.util.Scanner;
public class FindSummation {
public static void main(String[] args) {
Scanner userScann = new Scanner(System.in);
System.out.println("Enter number of terms: ");
int n = userScann.nextInt();
double sum = 0, newSum = 0;
double d1 = 1, d2 = 1;
int temp = 0;
for(int j = 1; j <= n; j++) {
sum += (Math.pow(-1, (j + 1))) * (j / (Math.pow(2, j)));
}
for(int i = 1; i <= n; i++) {
// Value of (-1)^(n+1)
do {
d1 *= (-1);
temp++;
}while(temp < (i + 1));
temp = 0;
// Value of 2^n
do {
d2 *= 2;
temp++;
}while(temp < i);
temp = 0;
newSum += (d1 * i) / d2; // Summation on each iteration
// Clear all dummy values
d1 = 1;
d2 = 1;
}
System.out.println(sum + " " + newSum);
}
} |
|
|

10-08-2008, 09:38 AM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 38
Rep Power: 0
|
|
|
Thank you very much. So, 'd1' is the numerator, and temp is the one changing it? What is *= doing to it?
|
|

10-08-2008, 10:10 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,535
Rep Power: 11
|
|
Actually 'd1' is not numerator, but in mathematically it is. It's calculate the part,
|
Quote:
|
|
(-1) to the power (n + 1)
|
temp is an iterator which is used to keep track of it.
*= is shorten form of expressing statements. In Java it's called as compound statement. So following two statements are same.
|
Code:
|
d1 *= (-1);
d1 = d1 * (-1); |
|
|

10-08-2008, 09:57 PM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 38
Rep Power: 0
|
|
Ok, thanks, I remember that. I can also see the application of the do/while statement, and why it works well for this, it controls the loop so it only runs up to n. It is useful, despite how my teacher said its not as useful as the regular while statement.
That's also a great way to make an exponent, I can definitely use that method later, as well. Thank you so much for the help, my notes are starting to make more sense now, as well.
|
|

10-09-2008, 01:17 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
|
|
|
Remember for the do{} while() loop, you ALWAYS do it once. If there is a possiblity of 0 entries in an array or a null pointer, you'll have to do a separate test before starting the loop to keep from getting an exception.
The while(){} loop can be done 0 times.
Which to use depends on the application.
|
|

10-09-2008, 03:44 AM
|
|
Senior Member
|
|
Join Date: Sep 2008
Posts: 564
Rep Power: 2
|
|
|
You know to get the sign, it's really inefficient to create a loop for something so simple. I'm sure it doesn't really matter for your assignment, but you should just set d1, or whatever the necessary variable is, based on whether n is even or odd. Just an idea.
|
|

10-09-2008, 04:59 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,535
Rep Power: 11
|
|
Originally Posted by SapphireSpark
|
Ok, thanks, I remember that. I can also see the application of the do/while statement, and why it works well for this, it controls the loop so it only runs up to n. It is useful, despite how my teacher said its not as useful as the regular while statement.
That's also a great way to make an exponent, I can definitely use that method later, as well. Thank you so much for the help, my notes are starting to make more sense now, as well.
|
Basically do-while loops guaranteed to execute at least once, and then check the condition. While is not like that, check the condition before execute the body.
|
|

10-09-2008, 05:01 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,535
Rep Power: 11
|
|
Originally Posted by emceenugget
|
|
You know to get the sign, it's really inefficient to create a loop for something so simple. I'm sure it doesn't really matter for your assignment, but you should just set d1, or whatever the necessary variable is, based on whether n is even or odd. Just an idea.
|
Ya, it's nice. Those kind of thing come in design level. That's the advantage of well plane design for an application.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +2. The time now is 05:39 PM.
|
|