Results 1 to 20 of 20
 10072008, 09:15 PM #1Member
 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:
Java 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++ }
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; 10082008 at 04:52 AM. Reason: to update the picture so its accurate
 10072008, 11:39 PM #2calculate the sum of thissummation, of (1) to the power of n+1?
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?
 10082008, 04:54 AM #3Member
 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.
 10082008, 05:07 AM #4Member
 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.

the poster is a spammer.
 10082008, 06:18 AM #6Member
 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?
 10082008, 06:37 AM #7
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 22
Why don't you use the math class for that. It's most easiest as well as the best way to follow.
Java 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); } }
 10082008, 06:37 AM #8
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 22
 10082008, 06:38 AM #9Member
 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; 10082008 at 06:58 AM.
 10082008, 06:40 AM #10Member
 Join Date
 Oct 2008
 Posts
 38
 Rep Power
 0
Don't worry pal. I'll monitoring him, and if so I can ban the user.
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; 10082008 at 06:46 AM. Reason: Expanding
 10082008, 07:00 AM #11
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 22
 10082008, 07:03 AM #12Member
 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; 10082008 at 07:12 AM.
 10082008, 07:59 AM #13
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 22
Here is the simple way to do it
Java 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); } }
 10082008, 08:38 AM #14Member
 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?
 10082008, 09:10 AM #15
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 22
Actually 'd1' is not numerator, but in mathematically it is. It's calculate the part,
(1) to the power (n + 1)
*= is shorten form of expressing statements. In Java it's called as compound statement. So following two statements are same.
Java Code:d1 *= (1); d1 = d1 * (1);
 10082008, 08:57 PM #16Member
 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. :)
 10092008, 12:17 AM #17
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.
 10092008, 02:44 AM #18Senior Member
 Join Date
 Sep 2008
 Posts
 564
 Rep Power
 9
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.
 10092008, 03:59 AM #19
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 22
 10092008, 04:01 AM #20
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 22
Similar Threads

Big Fraction 1.00
By JavaBean in forum Java SoftwareReplies: 1Last Post: 03262008, 05:24 AM 
Trouble getting Julia fraction correct.
By yllawwally in forum New To JavaReplies: 0Last Post: 12212007, 03:02 AM
Bookmarks