Pascal’s Diagonals in Java


The challenge

Create a function that returns an array containing the first l digits from the nth diagonal of Pascal’s triangle.

n = 0 should generate the first diagonal of the triangle (the ‘ones’). The first number in each diagonal should be 1.

If l = 0, return an empty array. Assume that both n and l will be non-negative integers in all test cases.

The solution in Java code

Option 1:

public class PascalDiagonals {

    public static long[] generateDiagonal(int n, int l) {
        long[] result = new long[l];
        if(l > 0) {
            result[0] = 1;
        }
        for(int i = 1; i < l; i++) {
            result[i] =  ( result[i-1] *  (n + i) /  i);
        }
        return result;
    }

}

Option 2:

public class PascalDiagonals {

  public static long[] generateDiagonal(int n, int l) {
    long[] result = new long[l];    
    if (l > 0) {         
      result[0] = 1;
      for (int i = 1; i < l; ++i)
        result[i] = result[i-1] * (n + i) / i;          
    }
    return result;
  }  
}

Option 3:

public class PascalDiagonals {

  public static long[] generateDiagonal(int n, int l) {
        if (l == 0) return new long[0];
        long[] diagonal = new long[l];
        long[] temp = null;
        long[][] result = new long[n + l][];
        for (int i = 1; i <= n + l; i++) {
            long[] row = new long[i];
            for (int j = 0; j < i; j++) {
                if (j == 0 || j == i - 1) row[j] = 1;
                else row[j] = temp[j - 1] + temp[j];
            }
            result[i - 1] = row;
            temp = row;
        }
        for (int i = n, j = 0; i < n + l; i++, j++) {
            diagonal[j] = result[i][n];
        }
        return diagonal;
  }

}

Test cases to validate our solution

import org.junit.Test;
import static org.junit.Assert.assertArrayEquals;
import org.junit.runners.JUnit4;
import java.util.Random;
import java.util.Arrays;

public class SolutionTest {

    @Test
    public void basicTests() {
    
        long[] expected = new long[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
        assertArrayEquals("All the ones", expected, PascalDiagonals.generateDiagonal(0, 10));
        
        expected = new long[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        assertArrayEquals("Natural numbers", expected, PascalDiagonals.generateDiagonal(1, 10));
        
        expected = new long[] { 1, 3, 6, 10, 15, 21, 28, 36, 45, 55 };
        assertArrayEquals("Triangular numbers", expected, PascalDiagonals.generateDiagonal(2, 10));
        
        expected = new long[] { 1, 4, 10, 20, 35, 56, 84, 120, 165, 220 };
        assertArrayEquals("Tetrahedral numbers", expected, PascalDiagonals.generateDiagonal(3, 10));
        
        expected = new long[] { 1, 5, 15, 35, 70, 126, 210, 330, 495, 715 };
        assertArrayEquals("Pentatope numbers", expected, PascalDiagonals.generateDiagonal(4, 10));
    }
    
    @Test
    public void edgeCases() {
      
      assertArrayEquals("Array length zero", new long[] {}, PascalDiagonals.generateDiagonal(10, 0));
      
      long[] expected = new long[] { 1, 101, 5151, 176851, 4598126, 96560646 };
      assertArrayEquals("Late row, short array", expected, PascalDiagonals.generateDiagonal(100, 6));
    }
    
    @Test
    public void randomTests() {
    
      Random r = new Random();
      for (int i = 0; i < 100; i++) {
        int n = r.nextInt(26) + 25;
        int l = r.nextInt(6) + 10;
        assertArrayEquals("Random " + i, generateDiagonal(n, l), PascalDiagonals.generateDiagonal(n, l));
      }
    }
    
    private static long[] generateDiagonal(int n, int l) {
    
      long[] diagonal = new long[l];
      
      Arrays.fill(diagonal, 1);
      
      for (int i = 1; i < l; i++)
        diagonal[i] = diagonal[i - 1] * (n + i) / i;
        
      return diagonal;
    }
}