• Post Reply Bookmark Topic Watch Topic
  • New Topic

help me solve this pattern  RSS feed

 
T devillers
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


/*

Print Pattern

1
2  6
3  7  10
4  8  11  13
5  9  12  14  15   
  

*/
 
Ryan McGuire
Ranch Hand
Posts: 1143
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Heres some pseudo code to get you started

Col 1Col 2Col 3Col 4Col 5
Row 11
Row 226
Row 33710
Row 4481113
Row 559121415


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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys,
i think i have found the solution . Please look through and kindly tell me if there another way to approach this problem ?
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An improvement on the code would be a better choice of variable names.  Something that described what the variables contain:
row vs i
nbrToPrint vs k
column vs j
 
Stephan van Hulst
Saloon Keeper
Posts: 7992
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's another way, but it's a bit less intuitive. You can calculate the value of 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:
  j
i
01234
 0 1        
 1 2 6      
 2 3 710    
 3 4 81113  
 4 5 9121415

Now, if we know that k = i+1 + m, then what is m?
  j
i
01234
 0 0        
 1 0 4      
 2 0 4 7    
 3 0 4 7 9  
 4 0 4 7 910

m depends only on j, and the difference d between each successive value becomes smaller by 1:
j01234
m 0 4 7 910
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
     
    T devillers
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Stephan van Hulst wrote:There's another way, but it's a bit less intuitive. You can calculate the value of 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:


    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
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.

    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
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!