2

I am trying to produce the following results:

120
200   202   
280   284   288   
360   366   372   378   
440   448   456   464   472   
520   530   540   550   560   570   
600   612   624   636   648   660   672   
680   694   708   722   736   750   764   778   
760   776   792   808   824   840   856   872   888  

As you can see, each row increases by 80 and difference between consecutive numbers in each row is i*2 (where i row number starting with 0)

I have written this code down which actually makes this result:

 public  class Triangle
 {

       public static void main(String [] args)

       {
            System.out.println(120);
              System.out.print(i + "   ");
            }
            System.out.println();
            for (int i = 280 ; i <= 288; i = i + 4 ) {
                System.out.print(i + "   ");
            }
            System.out.println();
                  System.out.print(i + "   ");
            }
            System.out.println();
            for (int i = 440 ; i <= 472; i = i + 8 ) {
                 System.out.print(i + "   ");
            }
            System.out.println();
            for (int i = 520 ; i <= 570; i = i + 10 ) {
                 System.out.print(i + "   ");
            }
            System.out.println();
            for (int i = 600 ; i <= 672; i = i + 12 ) {
                 System.out.print(i + "   ");
            }
            System.out.println();
            for (int i = 680 ; i <= 778; i = i + 14 ) {
                 System.out.print(i + "   ");
            }
            System.out.println();
            for (int i = 760 ; i <= 888; i = i + 16 ) {
                 System.out.print(i + "   ");
            }
            System.out.println();
            }
          }

However, this code is really long. You can see that I add 80 myself every time. Is there a way I could add another forever loop that does that instead? An adds the 2s as well?

Thanks

2
  • @Pshemo :) ....................... Commented Jul 6, 2015 at 21:13
  • "Is there a way I could add another forever loop that does that instead?" it is not "forever" loop, but for loop. There is also while and do{}while. Also all of these loops can be infinite :) Commented Jul 6, 2015 at 21:32

4 Answers 4

3

Not tested, only using my brain:

for(int i=0;i<input;i++){
    int firstNumber = 120+80*i;
    for(int j=0;j<=i;j++){
        System.out.print(firstNumber+2*i*j); //optimalization firstNumber+(i*j<<1)
    }
    System.out.println();
}

EDIT input

input variable is count of rows

you can pass the variable as parameter from command line, for example:

public static void main(String[] args){
    int input=5; //default value
    try{
        input=Integer.valueOf(args[0]); //trying to get integer from command line
    }catch(ArrayIndexOutOfBoundsException e){
        System.out.println("Missing parameter");
    }catch(Exception e){
        System.out.println("First parameter is not an integer");
    }
    //input variable is now parameter or 5 by default
    for(int i=0;i<=input;i++){
        int firstNumber = 120+80*i;
        for(int j=0;j<i;j++){
            System.out.print(firstNumber+2*i*j);
        }
        System.out.println();
    }
}

EDIT explanation

The first number is variable in first column, if you will count by i variable, you will see this sequence:

120, 200, 280, 360, 340, ...

Which is really the first column.

Then, you should solve next column, as you see, in each row, the difference is +2 in each, and in each column too +2, so 2*i*j solves this

Sign up to request clarification or add additional context in comments.

4 Comments

Hi @maskacovnik Can you further elaborate? I am extremely new to this.
input is count of rows, inserted to answer too - you should pass to method/program this variable as input to get desired output @JohnHackle
for(int j=0;j<i;j++){ -> for(int j=0;j<=i;j++){
You are absolutelly right, thanks, corrected @saka1029
2

Try to split your problem in separate parts like

print N lines
  in each line print M values 

Such loop can look more or less like

for (int line = 1 ; line <= N; line++){
    for (int position = 1; position <= M; position++){
        print(calculateFor(line, position));
    }
    printLineSeparator;
}

(I started indexing from 1 but you should get used to indexing from 0 so consider rewriting these loops to be in form for (int x = 0; ...) as homework)

Now our problem is calculating proper value based on line number and its position in line.

Lets start from figuring out formula for first value in each line.
We can calculate it easily since it is linear function f(line)=... where we know that

  x |  1  |  2  |  3  |
----+-----+-----+-----+ .....
f(x)| 120 | 200 | 280 |

So after applying some math to

{120 = a*1 + b
{200 = a*2 + b 

we can figure out that f(line) is 80 * line + 40.

Now we need to figure out formula for rest of elements in line.
This is quite easy since we know that it increases by 2, which means that it is something in form firstValue + 2*(positionInLine -1)

Now last problem is how to decide how many values should be printed in each line?
If you take a closer look you at your data you will see that each line contains same amount of values as line number. So we should continue printing values in line as long position<=line.

So our final solution can look like

for (int line = 1; line <= 9; line++) {
    int firstValueInCurrentLine = 80 * line + 40;
    for (int position = 1; position <= line; position++) {
        System.out.print(firstValueInCurrentLine + 2 * (position -1) + "  ");
    }
    System.out.println();
}

Comments

2
public class Triangle {
    public static void main(String [] args) {
        int base_int = 40;
        for (int row = 1; row < 10; row++) {
            for (int col = 0; col < row; col++) {
                int result = base_int + (80 * row) + (2 * (row - 1) * col);
                 System.out.print(result);
                System.out.print(" ");
            }
            System.out.println();
        }
    }
}

Comments

0
for (int i = 0, m = 120; i < 9; ++i, m += 80) {
    for (int j = 0, s = i + i, n = m; j <= i; ++j, n += s)
        System.out.print(n + "   ");
    System.out.println();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.