4

I have an programming assignment in Java.

I have implemented it by making an nCr ( http://en.wikipedia.org/wiki/Combination ) function then using a double for loop to make the triangle by printing it out.

However, the assignment calls for a uneven 2d array to be created and then populated by adding the two numbers from the previous lines and then printing the array out.

I know I am going to have to do the assignment the way it asks, however I have a small feeling that (at least for small triangles anyway) that the approach I have implemented is better.

Which is the better approach?

Portablejim
  • 824
  • 1
  • 12
  • 21

3 Answers3

1

I'd think the method called for by the assignment would be better. You're method requires a number of multiplications to calculate each of the elements of the triangle. This number will increase for each row of the triangle you need to calculate.

The assignment's method, however, requires a single addition for each element of the triangle.

Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116
1

If I understand your question, you are trying to compare two approaches to generating Pascal's triangle:

  1. By running an nCr function to populate each cell of the triangle.
  2. By generating the triangle in one pass by filling in each cell by a simple addition.

The second approach seems hands-down better. Am I missing something? Even if you use memoization in your nCr function there is overhead in those calls.

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
  • Using nCr I am not generating an array at all. – Portablejim Aug 02 '11 at 06:33
  • Ah, I see now. I must have misread. Still, I would say that two lookups and one addition per cell would be a win almost always. Your assignment might require you to keep the whole triangle in memory but in practice you only have to keep the last two rows. As values of n and k get larger the const of the `nCr` function grows. This does not happen with the incremental approach. – Ray Toal Aug 02 '11 at 06:39
0
  1. By using recursion

    /*By using recursion*/
    class RecursivePascal {
        public static void main(String args[]) {
            int n = 100;
            for (int i = 0; i < n; i++) {
                for (int j = 0; j <= i; j++) {
                    //System.out.print(i+","+j+" ");
                    System.out.print(pascal(i, j) + " ");
                }
                System.out.println();
            }
        }
    
        static int pascal(int i, int j) {
            if (j == 0)
                return 1;
            else if (j == i)
                return 1;
            else {
                return pascal(i - 1, j - 1) + pascal(i - 1, j);
            }
        }
    }
    
  2. By using simple logic

    /*By using logic*/
    class p {
        public static void main(String args[]) {
            int one[] = {1};
            int n = 13;
            System.out.println("1");
            for (int j = 0; j < n; j++) {
                int two[] = new int[one.length + 1];
                int twoCounter = 0;
                for (int i = 0; i < one.length; i++) {
                    if (i == 0) {
                        two[twoCounter++] = one[i];
                        System.out.print(one[i] + " ");
                    }
                    if (i != 0) {
                        two[twoCounter++] = one[i] + one[i - 1];
                        System.out.print((one[i] + one[i - 1]) + " ");
                    }
                    if (i == one.length - 1) {
                        two[twoCounter++] = one[i];
                        System.out.print(one[i] + " ");
                    }
                }
                System.out.println();
                one = two;
            }
        }
    }
    
Community
  • 1
  • 1
Esann
  • 77
  • 5