T devillers

Greenhorn

Posts: 19

posted 1 year ago

hi guys,

i have tried alot on this question but not able to find logic and there are not much help for such programs on the web..

please tell me how to print it .

With Regards,

T S

i have tried alot on this question but not able to find logic and there are not much help for such programs on the web..

please tell me how to print it .

With Regards,

T S

/*

Print Pattern

1

2 6

3 7 10

4 8 11 13

5 9 12 14 15

*/

Ryan McGuire

Ranch Hand

Posts: 1143

9

posted 1 year ago

Of course we're not going to just solve it for you, but we're certainly willing to help. What do you have so far? For instance, what is the outline of your program?

T devillers wrote:T devillers wrote: 1

2 6

3 7 10

4 8 11 13

5 9 12 14 15

Please edit my posts . I want '1' to be printed at left top corner above '2'

Of course we're not going to just solve it for you, but we're certainly willing to help. What do you have so far? For instance, what is the outline of your program?

posted 1 year ago

There are many ways to do this. If you have no constraints you can do it with several (or even just one) print statements.

if you're trying to do it some other way (which you probably are), remember don't try and solve it all at once. Write the code that just prints the correct number of lines, even if it just prints "new line".

Once you're printing the correct number of lines, see if you can print the correct number of elements on the line...

and just keep adding to it.

if you're trying to do it some other way (which you probably are), remember don't try and solve it all at once. Write the code that just prints the correct number of lines, even if it just prints "new line".

Once you're printing the correct number of lines, see if you can print the correct number of elements on the line...

and just keep adding to it.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

posted 1 year ago

- 1

Heres some pseudo code to get you started

Notice in the 1st column all values are printed

Notice every subsequent column 1 value is skipped as compared to the previous column

In Java you can nest loops, i.e. Have a loop inside a loop

Col 1 | Col 2 | Col 3 | Col 4 | Col 5 | |
---|---|---|---|---|---|

Row 1 | 1 | ||||

Row 2 | 2 | 6 | |||

Row 3 | 3 | 7 | 10 | ||

Row 4 | 4 | 8 | 11 | 13 | |

Row 5 | 5 | 9 | 12 | 14 | 15 |

Notice in the 1st column all values are printed

Notice every subsequent column 1 value is skipped as compared to the previous column

In Java you can nest loops, i.e. Have a loop inside a loop

T devillers

Greenhorn

Posts: 19

Norm Radder

Rancher

Posts: 2240

28

Stephan van Hulst

Saloon Keeper

Posts: 7992

143

posted 1 year ago

- 1

There's another way, but it's a bit less intuitive. You can calculate the value of

The calculation becomes easier if we first use zero based indexes. That means our rows and columns start at index

Now, if we know that

As you can see,

How can we calculate

This yields

`k`directly inside the inner loop, without adding to it between loop iterations.The calculation becomes easier if we first use zero based indexes. That means our rows and columns start at index

`0`: ji | 0 | 1 | 2 | 3 | 4 |

0 | 1 | | | | |

1 | 2 | 6 | | | |

2 | 3 | 7 | 10 | | |

3 | 4 | 8 | 11 | 13 | |

4 | 5 | 9 | 12 | 14 | 15 |

Now, if we know that

`k = i+1 + m`, then what is`m`? ji | 0 | 1 | 2 | 3 | 4 |

0 | 0 | | | | |

1 | 0 | 4 | | | |

2 | 0 | 4 | 7 | | |

3 | 0 | 4 | 7 | 9 | |

4 | 0 | 4 | 7 | 9 | 10 |

`m`depends only on`j`, and the difference`d`between each successive value becomes smaller by`1`:j | 0 | 1 | 2 | 3 | 4 |

m | 0 | 4 | 7 | 9 | 10 |

d | 4 | 3 | 2 | 1 | 0 |

As you can see,

`m(0) == (0)`,

`m(1) == (4)`,

`m(2) == (4 + 3)`,

`m(3) == (4 + 3 + 2)`, and

`m(4) == (4 + 3 + 2 + 1)`.

How can we calculate

`m(j)`? Notice that`(4 + 3) == (1 + 2 + 3 + 4) - (1 + 2)`. We can easily calculate these terms! They are called triangle numbers. A triangle number`t`is given by the formula`t(x) == x*(x-1)/2`. So:`m(0) == t(5)-t(5)`,

`m(1) == t(5)-t(4)`,

`m(2) == t(5)-t(3)`,

`m(3) == t(5)-t(2)`, and

`m(4) == t(5)-t(1)`.

This yields

`m(j) == t(5) - t(5-j)`.`t(5)`is equal to`10`, so our final formula becomes`k = i + 1 + 10 - t(5-j)`, or*The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.*

T devillers

Greenhorn

Posts: 19

posted 1 year ago

Thank you, Stephan for giving new perspective to the program . How did the idea of Triangle Number come to your mind for this particular problem ? And please check my another pattern problem if possible .

Number Pattern2 Problem

Stephan van Hulst wrote:There's another way, but it's a bit less intuitive. You can calculate the value ofkdirectly inside the inner loop, without adding to it between loop iterations.

The calculation becomes easier if we first use zero based indexes. That means our rows and columns start at index0:

Thank you, Stephan for giving new perspective to the program . How did the idea of Triangle Number come to your mind for this particular problem ? And please check my another pattern problem if possible .

Number Pattern2 Problem

Stephan van Hulst

Saloon Keeper

Posts: 7992

143

posted 1 year ago

- 1

When I'm looking for a function that describes a series, I first look at how elements in the series could relate to their successor. It was easy to see that there's a pattern here, because the difference between each element decreased by 1 with each step. The last element was described by the sum

You could take the maths even further by substituting

We end up with a very concise loop, but I prefer your initial solution because it's more clear what's going on.

`(1 + 2 + 3 + 4)`. This is a triangle number, which I know from experience with maths problems.You could take the maths even further by substituting

`(5-j)`for`x`in the definition of the triangle number function, and then simplifying the equation:We end up with a very concise loop, but I prefer your initial solution because it's more clear what's going on.

T devillers

Greenhorn

Posts: 19

posted 1 year ago

Thank you, Stephan once again for taking time for this problem . I was hoping that you would check my another pattern question . I have tried but not getting the logic right . Please check if you have time another question

Stephan van Hulst wrote:When I'm looking for a function that describes a series, I first look at how elements in the series could relate to their successor. It was easy to see that there's a pattern here, because the difference between each element decreased by 1 with each step. The last element was described by the sum(1 + 2 + 3 + 4). This is a triangle number, which I know from experience with maths problems.

Thank you, Stephan once again for taking time for this problem . I was hoping that you would check my another pattern question . I have tried but not getting the logic right . Please check if you have time another question