1

Below is a table which has a recursive relation as current cell value is the sum of the upper and left cell.

enter image description here

I want to find the odd positions for any given row denoted by v(x) as represented in the first column.

Currently, I am maintaining two one arrays which I update with new sum values and literally checking if each positions value is odd or even.

Is there a closed form that exists which would allow me to directly say what are the odd positions available (say, for the 4th row, in which case it should tell me that p1 and p4 are the odd places).

Since it is following a particular pattern I feel very certain that a closed form should exist which would mathematically tell me the positions rather than calculating each value and checking it.

halfer
  • 19,824
  • 17
  • 99
  • 186
user3243499
  • 2,953
  • 6
  • 33
  • 75

1 Answers1

2

The numbers that you're looking at are the numbers in Pascal's triangle, just rotated ninety degrees. You more typically see it written out like this:

              1
            1   1
          1   2   1
        1   3   3   1
      1   4   6   4   1
    1   5   10  10  5   1
  1   6   15  20  15  6   1
1   7   21  35  35  21  7   1
            ...

You're cutting Pascal's triangle along diagonal stripes going down the left (or right, depending on your perspective) strips, and the question you're asking is how to find the positions of the odd numbers in each stripe.

There's a mathematical result called Lucas's theorem which is useful for determining whether a given entry in Pascal's triangle is even or odd. The entry in row m, column n of Pascal's triangle is given by (m choose n), and Lucas's theorem says that (m choose n) mod 2 (1 if the number is odd, 0 otherwise) can be found by comparing the bits of m and n. If n has a bit that's set in a position where m doesn't have that bit set, then (m choose n) is even. Otherwise, (m choose n) is odd.

As an example, let's try (5 choose 3). The bits in 5 are 101. The bits in 3 are 011. Since the 2's bit of 3 is set and the 2's bit of 5 is not set, the quantity (5 choose 3) should be even. And since (5 choose 3) = 10, we see that this is indeed the case!

In pseudocode using relational operators, you essentially want the following:

if ((~m & n) != 0) {
    // Row m, entry n is even
} else {
    // Row m, entry n is odd.
}
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • What will be the complexity of your approach for "n" number of positions and "n" number of v values to calculate? is it still O(n^2)? – user3243499 Sep 09 '17 at 18:26
  • Yes, I suppose so. However, this approach lets you directly calculate which positions are odd without having to construct the table at all. You could use this to determine what's odd in rows you haven't even generated. – templatetypedef Sep 09 '17 at 18:30
  • That means using this approach I can find any value (say, v100) directly without even bothering to find all of its predecessors? – user3243499 Sep 09 '17 at 18:42
  • @user3243499 Yep! – templatetypedef Sep 09 '17 at 18:42
  • Great, thanks. I think this is at max we can achieve in terms of performance then. Do you have any links to refer for understanding more about it? – user3243499 Sep 09 '17 at 18:43
  • 1
    @user3243499 I don't have any links to provide - sorry! I just learned about Lucas's theorem earlier today. There may be an even more efficient way to find all of the indices that are odd, and if I think of one I'll be sure to let you know. – templatetypedef Sep 09 '17 at 18:47