For a homework assignment in my intro to java class I was given the following problem:

Write for, do-while, and while statements to compute the following sum and product
1. 1 + 7 + 25 + 79 + .... + (3^20 - 2)
2. 1 x 3 x 9 x 27 x 81 x ... x (3^20)

I understand how to write the statements, I'm just not sure how to express the increment.
For the first one (for statement) I have

int i, sum = 0;
for (i = 1; i =< 2486784399; // I'm not sure what would go here ) {
sum += i;

]

System.out.println(“The sum is” + sum);


I'm not sure how to show that it's increasing by 3^(n) - 2

5
Contributors
6
Replies
42
Views
4 Years
Discussion Span
Last Post by stultuske

Have a look at this post, maybe it will help you :) Click Here

Hello Victoria_1 and welcome to programming in Java :)
The "For loop" has the following form:
for(int i = 0; i < NUMBER; i++)
for(initialize i to a number; compare i with NUMBER; increment i by 1)
i++ is the same as i = i + 1
So if you want to increment i by 10 for example, you would write
i = i + 10 rather than i++

GL

Sorry for the delay, answer is here ;)
u can change another value for number and potency, in this example is
number= 3 (number = 2 - in a second example)
potency = 20 (potency = 5 -in a second example)
Class LoopsSolution:

package daniweb;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
*
* @author JoZulyk http://inovawebstudio.com
*/
public class LoopsSolution {
/*
For a homework assignment in my intro to java class I was given the following problem:

Write for, do-while, and while statements to compute the following sum and product
1. 1 + 7 + 25 + 79 + .... + (3^20 - 2)
2. 1 x 3 x 9 x 27 x 81 x ... x (3^20)

I understand how to write the statements, I'm just not sure how to express the increment.
For the first one (for statement) I have
*/
public static List<Double> numGenerator(int number, int potency){
//number is in this case 3, repetition is in this case 20 (0-20)
List<Double> numbersGenerated = new ArrayList<>();
for(int i= 0; i<= potency; i++){
}
return numbersGenerated;
}
public static String sumFuntionStringGenerator(List<Double> numsGenerated){
String sum = "";
Iterator it = numsGenerated.iterator();
while(it.hasNext()){
Double num = (double)it.next();
if(num!=1d)
sum += (num - 2) + " + ";
}
return sum;
}
public static String productFuntionStringGenerator(List<Double> numsGenerated){
String prod = "";
Iterator it = numsGenerated.iterator();
while(it.hasNext()){
Double num = (double)it.next();
prod += num + " * ";
}
return prod;
}
public static void main(String [] argv){
//here u can change for an different value ;)
int number = 3;
int potency = 20;
List<Double> numbers = numGenerator(number,potency);
//obtaining the sum: 1. 1 + 7 + 25 + 79 + .... + (3^20 - 2)
//are:  (3^1 - 2) + (3^2 - 2) +  (3^3 - 2)+...+ (3^20 - 2)
double sum = 0;
Iterator it = numbers.iterator();
while(it.hasNext()){
Double num = (double)it.next();
//skipping the first term because (3^0 - 2) doesn't apply
if(num != 1d){
sum = sum + num - 2;
}
}
System.out.println("Sum function is: " + sumFuntionStringGenerator(numbers) );
System.out.println("Result are: " + sum);

//obtaining the product: 2. 1 x 3 x 9 x 27 x 81 x ... x (3^20)
//are:  (3^0) + (3^1) +  (3^2)+...+ (3^20)
double prod = 1;
Iterator pit = numbers.iterator();
while(pit.hasNext()){
Double num = (double)pit.next();
prod *= num;
}
System.out.println("Product function is: " + productFuntionStringGenerator(numbers) );
System.out.println("Result are: " + prod);

}
}


the output are:

Sum function is: 1.0 + 7.0 + 25.0 + 79.0 + 241.0 + 727.0 + 2185.0 + 6559.0 + 19681.0 + 59047.0 + 177145.0 + 531439.0 + 1594321.0 + 4782967.0 + 1.4348905E7 + 4.3046719E7 + 1.29140161E8 + 3.87420487E8 + 1.162261465E9 + 3.486784399E9 +
Result are: 5.23017656E9
Product function is: 1.0 * 3.0 * 9.0 * 27.0 * 81.0 * 243.0 * 729.0 * 2187.0 * 6561.0 * 19683.0 * 59049.0 * 177147.0 * 531441.0 * 1594323.0 * 4782969.0 * 1.4348907E7 * 4.3046721E7 * 1.29140163E8 * 3.87420489E8 * 1.162261467E9 * 3.486784401E9 *
Result are: 1.5684240429131528E100


...
output for the seconde values:

Sum function is: 0.0 + 2.0 + 6.0 + 14.0 + 30.0 +
Result are: 52.0
Product function is: 1.0 * 2.0 * 4.0 * 8.0 * 16.0 * 32.0 *
Result are: 32768.0


its all ;), please mark this post how SOLVED! ... have a great day!

zolymo: please, try not to provide custom made code. they won't learn anything this way.
why should this thread be marked 'solved' ? because you posted your code ?

i post an question! and i think if i help ! i obtain help too. sorry.

that's the point exactly, you aren't helping.
This is like scripture saying: "give a man a fish, and he'll eat for a day. teach a man to fish, and he'll never get hungry again."

teach him how to solve his problem, don't just provide copy-paste ready code.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.