Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Triangle calculation

  1. Dec 30, 2009 #1

    DavidSnider

    User Avatar
    Gold Member

    Let's say you have a triangle like:
    1
    2 3
    4 5 6
    7 8 9 10

    The children of each element in the triangle are those directly adjacent on the next row. For example the children of 4 are 7 and 8. The children of 5 are 8 and 9.

    Now let's say we flatten it to: [1,2,3,4,5,6,7,8,9,10]

    Is there a way to compose a function that takes the index of the parent and returns a tuple containing the indexes of the children? (Assume 0 based indexes. The 1 through 10 are values, not indexes)
     
    Last edited: Dec 30, 2009
  2. jcsd
  3. Dec 30, 2009 #2

    LCKurtz

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    Is 7 a child of 5?
     
  4. Dec 30, 2009 #3

    DavidSnider

    User Avatar
    Gold Member

    No.

    1: 2,3
    2: 4,5
    3: 5,6
    4: 7,8
    5: 8,9
    6: 9,10

    It's easier to picture when you draw the triangle as a pyramid shape, but I couldn't get the forum to format it that way.
     
  5. Dec 30, 2009 #4

    tiny-tim

    User Avatar
    Science Advisor
    Homework Helper

    Hi DavidSnider! :smile:

    Use the CODE tag :wink:
    Code (Text):
              1
             2 3
            4 5 6
           7 8 9 10
    Hint: the kth element in the nth row is parent to the kth and k+1th elements in the n+1th row, and the index of the first element in the nth row is … ? :smile:
     
  6. Dec 30, 2009 #5

    DavidSnider

    User Avatar
    Gold Member

    Ah, thanks. I think I got it.

    First element in nth row is the same as the triangle number:
    [tex]\frac{n^2 + n}{2}[/tex]

    What I needed was the Triangle root:
    [tex]\frac{-1\pm\sqrt{8n+1}}{2}[/tex]

    so the index would be:
    [tex]n+\frac{1+\sqrt{8n + 1}}{2}[/tex]
     
    Last edited: Dec 30, 2009
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook