# (Help) Fraction Summation and Exponents

• 10-07-2008, 09:15 PM
SapphireSpark
(Help) Fraction Summation and Exponents
I'm making a program that's supposed to calculate the sum of this:

http://fc68.deviantart.com/fs36/f/20..._CAA_DP_DA.jpg

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.
• 10-07-2008, 11:39 PM
Norm
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, 04:54 AM
SapphireSpark
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, 05:07 AM
SapphireSpark
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, 06:15 AM
Fubarable
the poster is a spammer.
• 10-08-2008, 06:18 AM
SapphireSpark
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.

• 10-08-2008, 06:37 AM
Eranga
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, 06:37 AM
Eranga
Quote:

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, 06:38 AM
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?
• 10-08-2008, 06:40 AM
SapphireSpark
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?
• 10-08-2008, 07:00 AM
Eranga
Quote:

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, 07:03 AM
SapphireSpark
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.
• 10-08-2008, 07:59 AM
Eranga
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, 08:38 AM
SapphireSpark
Thank you very much. So, 'd1' is the numerator, and temp is the one changing it? What is *= doing to it?
• 10-08-2008, 09:10 AM
Eranga
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, 08:57 PM
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. :)
• 10-09-2008, 12:17 AM
Norm
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, 02:44 AM
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.
• 10-09-2008, 03:59 AM
Eranga
Quote:

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, 04:01 AM
Eranga
Quote:

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.