Reputation: 31
I am looking for a Pascal's triangle using a Python script.
I have done till here and don’t have any idea how to add on.
numstr = raw_input("Please enter the height: ")
height = int()
tri = []
row1 = [1]
row2 = [1, 1]
tri.append(row1)
tri.append(row2)
while len(tri) < height:
Upvotes: 2
Views: 4987
Reputation: 11
Note that the Pascal's Triangle is just the coefficients of the expansion of any binomial expression, so you can just use the formula for n choose k, that is, n!/k!(n-k)! where n represents the row, and k represents the column to actually print out the digits. You can then just make another function to space out the triangle properly, if you want(but the problem is, after 5 rows, it just becomes uneven due to the double digits). Here's a sort of pseudocode:
for row in rows:
number = 1
for space in rows-1 step -1:
print " "
for column in row+1:
print number
number = row!/column!(row-column)!
print new line
Upvotes: 0
Reputation: 21
This is Python code for Pascal's triangle:
# Python code for Pascal's triangle
# printPascal() function for printing Pascal's triangle.
def printPascal(N):
# Declaring two arrays
arr = [1]
temp = []
print("Pascal's triangle of", N, "Rows...")
# Calculating the next rows.
for i in range(N):
# printing current row.
print("rows", i+1, end=" : ")
for j in range(len(arr)):
print(arr[j], end=' ')
print()
# Calculating the next rows.
temp.append(1)
for j in range(len(arr)-1):
temp.append(arr[j] + arr[j + 1])
temp.append(1)
# Copy the next row to the current row.
arr = temp
# Initialize temp to an empty array.
temp = []
# Driver code
N = 9
printPascal(N)
For more details: https://algorithmdotcpp.blogspot.com/2021/07/print-n-rows-of-pascals-triangle-in-cpp-and-python.html
Upvotes: 0
Reputation: 772
C# solution:
public IList<IList<int>> Generate(int numRows) {
var result = new List<IList<int>>();
var item1 = new List<int>();
item1.Add(1);
result.Add(item1);
if (numRows == 1)
return result;
var item2 = new List<int>();
item2.Add(1);
item2.Add(1);
result.Add(item2);
if (numRows == 2)
return result;
var i = 2;
while (i != numRows)
{
var data = result.Last().ToArray();
var n = data.Length;
var item = new int[n + 1];
item[0] = 1;
item[n] = 1;
for (var x = 1; x < n; x++)
item[x] = data[x - 1] + data[x];
result.Add(item);
i++;
}
return result;
}
Upvotes: -1
Reputation: 11
Use:
// C++ code for Pascal's triangle
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include <time.h>
#include <stdlib.h>
long unsigned int Factorial(long unsigned int Number)
{
long unsigned int Fact = 0;
if (Number == 0)
return (long unsigned int) 1;
else
{
Fact = Number*Factorial(Number-1);
return Fact;
}
}
long unsigned int Combination(long unsigned int num1, long unsigned int num2)
{
long unsigned int Comb, num3;
long unsigned int Factor1, Factor2, Factor3;
Factor1 = Factorial(num1);
Factor2 = Factorial(num2);
num3 = num1 - num2;
Factor3 = Factorial(num3);
Comb = Factor1/(Factor2*Factor3);
return(Comb);
}
int main()
{
long unsigned int i, j, Num=0;
long unsigned int **Matrix;
clrscr();
printf(" %d\n ", sizeof(long unsigned int));
printf("Enter index of square matrix num: ");
scanf("%lu", &Num);
Matrix = (long unsigned int **) malloc(Num*Num*sizeof(long unsigned int *));
for(i=0; i<Num; i++)
{
for (j=0; j<Num; j++)
{
*(*(Matrix+i)+j) = 0;
}
}
for(i=0; i<Num; i++)
{
for(j=0; j<=i; j++)
{
printf(" %lu ", *(*(Matrix + i) + j));
}
printf("\n");
}
for(i=0; i<Num; i=i+1)
{
for(j=0; j<=i; j=j+1)
{
*(*(Matrix + i) + j) = Combination(i, j);
}
printf("\n");
}
for(i=0; i<Num; i++)
{
for(j=0; j<=i; j++)
{
//printf(" \n %lu %lu \n", i, j);
printf(" %lu ", *(*(Matrix + i) + j));
}
printf("\n");
}
getch();
return(0);
}
Upvotes: -1
Reputation: 43207
Here's the correct way to make Pascal's triangle.
Pascal's Triangle. And Its Patterns
Upvotes: 3
Reputation: 1
Try the SciPy 'pascal' module:
from scipy.linalg import pascal
pascal(6, kind='lower')
Output:
array([[ 1, 0, 0, 0, 0, 0],
[ 1, 1, 0, 0, 0, 0],
[ 1, 2, 1, 0, 0, 0],
[ 1, 3, 3, 1, 0, 0],
[ 1, 4, 6, 4, 1, 0],
[ 1, 5, 10, 10, 5, 1]], dtype=uint64)
Upvotes: 1
Reputation: 1116
Here is my solution to generate a Pascal's triangle:
def factorial(x):
return 1 if x == 0 else x * factorial(x - 1)
def triangle(n):
return [[factorial(i) / (factorial(j) * factorial(i - j)) for j in range(i + 1)] for i in range(n)]
Upvotes: 0
Reputation: 111
Actually, the next row is crossed axis sum of last row. For example, if the last row is [1, 1], the next row would be:
[1, 1]
+ [1, 1]
-----------
= [1, 2, 1]
[1, 2, 1]
+ [1, 2, 1]
--------------
= [1, 3, 3, 1]
So, the loop body can be like this:
tri.append(map(lambda x, y: x + y, [0] + tri[-1], tri[-1] + [0]))
Upvotes: 2
Reputation: 354516
You would have to take the last row there is in the triangle and create the next one like this:
You could also calculate the new numbers using binomial coefficients, though that's likely a little more work to get right.
Upvotes: 3