Task: Pascal matrix generation




Go: package main

import (
    "fmt"
    "strings"
)

func binomial(n, k int) int {
    if n < k {
        return 0
    }
    if n == 0 || k == 0 {
        return 1
    }
    num := 1
    for i := k + 1; i <= n; i++ {
        num *= i
    }
    den := 1
    for i := 2; i <= n-k; i++ {
        den *= i
    }
    return num / den
}

func pascalUpperTriangular(n int) [][]int {
    m := make([][]int, n)
    for i := 0; i < n; i++ {
        m[i] = make([]int, n)
        for j := 0; j < n; j++ {
            m[i][j] = binomial(j, i)
        }
    }
    return m
}

func pascalLowerTriangular(n int) [][]int {
    m := make([][]int, n)
    for i := 0; i < n; i++ {
        m[i] = make([]int, n)
        for j := 0; j < n; j++ {
            m[i][j] = binomial(i, j)
        }
    }
    return m
}

func pascalSymmetric(n int) [][]int {
    m := make([][]int, n)
    for i := 0; i < n; i++ {
        m[i] = make([]int, n)
        for j := 0; j < n; j++ {
            m[i][j] = binomial(i+j, i)
        }
    }
    return m
}

func printMatrix(title string, m [][]int) {
    n := len(m)
    fmt.Println(title)
    fmt.Print("[")
    for i := 0; i < n; i++ {
        if i > 0 {
            fmt.Print(" ")
        }
        mi := strings.Replace(fmt.Sprint(m[i]), " ", ", ", -1)
        fmt.Print(mi)
        if i < n-1 {
            fmt.Println(",")
        } else {
            fmt.Println("]\n")
        }
    }
}

func main() {
    printMatrix("Pascal upper-triangular matrix", pascalUpperTriangular(5))
    printMatrix("Pascal lower-triangular matrix", pascalLowerTriangular(5))
    printMatrix("Pascal symmetric matrix", pascalSymmetric(5))
}








Ada: 
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;


with Ada.Numerics.Generic_Elementary_Functions;

procedure PascalMatrix is

  type Matrix is array (Positive range <>, Positive range <>) of Natural;

  
  package Math is new Ada.Numerics.Generic_Elementary_Functions(Float_Type => Float);
  use Math;
  
  procedure Print(m: in Matrix) is
    
    w: Float := Log(Float(m'Length(1)**(m'Length(1)/2)), 10.0);
    width: Positive := Natural(Float'Ceiling(w)) + 1;
    begin
      for i in m'First(1)..m'Last(1) loop
        Put("( ");
        for j in m'First(2)..m'Last(2) loop
          Put(m(i,j), width);
        end loop;
        Put(" )"); New_Line(1);
      end loop;
    end Print;
  
  function Upper_Triangular(n: in Positive) return Matrix is
    result: Matrix(1..n, 1..n) := (
                                    1 => ( others => 1 ),
                                    others => ( others => 0 )
                                  );
    begin
      for i in 2..n loop
        result(i,i) := 1;
        for j in i+1..n loop
          result(i,j) := result(i,j-1) + result(i-1,j-1);
        end loop;
      end loop;
      return result;
    end Upper_Triangular;
  
  function Lower_Triangular(n: in Positive) return Matrix is
    result: Matrix(1..n, 1..n) := (
                                    others => ( 1 => 1, others => 0 )
                                  );
    begin
      for i in 2..n loop
        result(i,i) := 1;
        for j in i+1..n loop
          result(j,i) := result(j-1,i) + result(j-1,i-1);
        end loop;
      end loop;
      return result;
    end Lower_Triangular;
  
  function Symmetric(n: in Positive) return Matrix is
    result: Matrix(1..n, 1..n) := (
                                   1 => ( others => 1 ),
                                   others => ( 1 => 1, others => 0 )
                                  );
    begin
      for i in 2..n loop
        for j in 2..n loop
          result(i,j) := result(i,j-1) + result(i-1,j);
        end loop;
      end loop;
      return result;
    end Symmetric;
  
  n: Positive;
  
  begin
    Put("What dimension Pascal matrix would you like? ");
    Get(n);
    Put("Upper triangular:"); New_Line(1);
    Print(Upper_Triangular(n));
    Put("Lower triangular:"); New_Line(1);
    Print(Lower_Triangular(n));
    Put("Symmetric:"); New_Line(1);
    Print(Symmetric(n));
  end PascalMatrix;