Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I modify my program to print out Pascal's triangle?

Tags:

perl

So first of all Pascal's Triangle looks like this:

Pascal's Triangle

The first row that you see is the zero-ith row.

That's nothing unusual when you are a computer scientist.

Each term in Pascal's triangle can be predicted with a combination with the formula:

C(n, k) = n! / [k! * (n - k)!], where "n" is the row and "k" is any integer from zero to n.

So thus it follows that Pascal's triangle can be predicted with (n, k) combinations:

Pascal's triangle combinations

And that's what you are seeing in the figure above.

Pascal's triangle is basically binomial probability:

(H + T)^n # You flip a two sided coin "n" times and it lands on "heads" or "tails" and you collect the frequency of each in a set of coefficients, for n = 3, we get the expansion:

(H + T)^3 = 1(H^3) + 3(H^2)(T) + 3(H)(T^2) + 1(T^3), where those coefficients: 1, 3, 3, 1 are in row 3 of Pascal's triangle.


I defined a factorial (!), and a combination and was able to get the coefficient numbers on any row of Pascal's triangle with some looping Perl code:

use strict;
use warnings;

# Note the first row is row 0.
print("\nWhich row of Pascal's triangle to display: ");
my $row = <STDIN>; # The row that you want to display # This is also n. 
my $terms = $row + 1; # The number of terms is one more than the row number. 

Pascal_Row($row); # Print the Pascal numbers for that row. 

# Function displays the numbers for a row of Pascal's triangle. 
#######################################################
sub Pascal_Row
{
    my $row = shift; # Row is passed in. 

    for(my $k = 0; $k < $row + 1; $k++) # k alternates, but not the row which is n. 
    {
        print(combination($row, $k), "\t") # Print each row. 
    }
    print("\n"); # Print a newline after each time this function is called.
}

# This computes the factorial of a number.
###########################################
sub factorial
{
    my $number = shift; # argument. 
    my $factorial_number = 1; # initalize the factorial. 

    for(my $i = 1; $i <= $number; $i++)
    {
        $factorial_number *= $i; # compute the factorial, by multiplying all terms up to and including number.
    }

    return $factorial_number; # Return the factorial number. 
}

# Computes a matehmatical combination usually denoted as C(n, k)
# where n is the row number, and k is each item in a row of Pascal's traingle 
sub combination
{
    my($n, $k) = @_; # from input. 

    # This is the mathematical formula for a combination. 
    my $combination_number = factorial($n) / (factorial($k) * factorial($n - $k));

    return $combination_number # And returning it. 
}

If I run the code and ask for row 8 of Pascal's triangle I get this:

Which row of Pascal's triangle to display: 8
1       8       28      56      70      56      28      8       1

That's entirely true for row 8 of Pascal's triangle. If I were to loop this from row 0 to the row 8 of Pascal's triangle I would get all correct rows of Pascal's triangle, but it wouldn't look like a triangle (it would look more like a box), so how could I modify my code to adjust the indenting.

How do I decide how much to indent the first row if I want 8 rows of Pascal's triangle displayed? How can I make a "triangle"?

like image 470
xyz123 Avatar asked Jan 17 '26 05:01

xyz123


1 Answers

Left-aligned triangle:

my $MAX_VAL_SIZE = 5;

for my $n (0...$N) {
   my @row;
   for my $k (0..$n) {
      push @row, C($n, $k);
   }

   say join "  ", map sprintf("%*d", $MAX_VAL_SIZE, $_), @row;
}

Centered triangle:

sub center {
   my ($n, $s) = @_;
   my $pad_len = $n - length($s);
   my $pad_len_l = int($pad_len/2);
   my $pad_len_r = $pad_len - $pad_len_l;
   return ( " " x $pad_len_l ) . $s . ( " " x $pad_len_r );
}

my $MAX_VAL_SIZE = 5;

for my $n (0...$N) {
   my @row;
   for my $k (0..$n) {
      push @row, C($n, $k);
   }

   my $row = join "  ", map center($MAX_VAL_SIZE, $_), @row;
   say center(($N+1)*($MAX_VAL_SIZE+2)-2, $row);
}
like image 114
ikegami Avatar answered Jan 19 '26 17:01

ikegami



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!