Thread: Recursive code

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Recursive code


    Hi can anyone give any tips/ideas on how to generate the following sequence, using recursive calls, where the numbers are actually pixels:

    1 2 5 6
    3 4 7 8

    9 10 13 14
    11 12 15 16

    It should be able to do it to make bigger squares, but that's basically the flow of the pixels.

    The function call may be in the form:

    square( initial_x, initial_y, lower_x, lower_y, upper_x, upper_y);
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,856
    Rep Power
    481
    Code:
       ,./"3 i.2 2 2 2  NB. solution in j, www.jsoftware.com
     0  1  4  5
     2  3  6  7
    
     8  9 12 13
    10 11 14 15
       
    
       NB. explanation
       [A =: i.2 2 2 2  NB. integers, rank 4
     0  1
     2  3
    
     4  5
     6  7
    
    
     8  9
    10 11
    
    12 13
    14 15
    
       ,./"3 A  NB. stitch the integers at rank 3
     0  1  4  5
     2  3  6  7
    
     8  9 12 13
    10 11 14 15
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0
    Thanks for the solution, however I'm really not following..I'm coding in c++ and I don't recognize what's there :(
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,856
    Rep Power
    481
    The essentials:
    j is interactive; the j prompt is three spaces on a new line. New line terminates a j sentence. All the rest is output from the j engine.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,387
    Rep Power
    1871
    Code:
     1  2  5  6
     3  4  7  8
     9 10 13 14
    11 12 15 16
    Where
    red is 4^0
    blue is 4^1
    green is 4^2
    Does this help you see the pattern?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo