1

I am having trouble getting this python code to work right. it is a code to display pascal's triangle using binomials. I do not know what is wrong. The code looks like this

from math import factorial
def binomial (n,k):
    if k==0:
        return 1
    else:
        return int((factorial(n)//factorial(k))*factorial(n-k))

def pascals_triangle(rows):
    rows=20
    for n in range (0,rows):
        for k in range (0,n+1):
            print(binomial(n,k))
    print '\n'

This is what it keeps printing

1                                                                                                            
1                                                                                                         1                                                                                                            
1                                                                                                            
2                                                                                                            
1                                                                                                            
1                                                                                                            
12                                                                                                           
3                                                                                                            
1                                                                                                            
1                                                                                                            
144                                                                                                          
24                                                                                                           
4                                                                                                            
1                                                                                                            
1                                                                                                            
2880                                                                                                         
360                                                                                                          
40                                                                                                           
5                                                                                                            
1                                                                                                            
1                                                                                                            
86400                                                                                                        
8640                                                                                                         
720                                                                                                          
60                                                                                                           
6                                                                                                            
1                                                                                                            
1                                                                                                            
3628800                                                                                                      
302400                                                                                                       
20160                                                                                                        
1260              

and on and on. any help would be welcomed.!!

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Anny
  • 25
  • 1
  • 6

3 Answers3

0

there are many wrong things. The first one is the way you compute the values : if building a pascal triangle, you want to use the previous line to compute the current one, and not use the binomial computation (which is expensive due to the number of multiplications).

then by default, print appends a "\n"

Correct implementation:

def print_line(x):
    print (" ".join(map(str,x)))

def pascals_triangle(rows):
    cur_line=[1,1]
    for x in range (2,rows):
        new_line=[1]
        for n in range (0,len(cur_line)-1):
            new_line.append(cur_line[n]+cur_line[n+1])
        new_line.append(1)
        print_line (new_line)
        cur_line=new_line

this provides the following output

$ python pascal.py
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
Bruce
  • 7,094
  • 1
  • 25
  • 42
0
from math import factorial
def binomial (n,k):
    if k==0:
        return 1
    else:
        return int((factorial(n)//factorial(k))*factorial(n-k))

def pascals_triangle(rows):
    for n in range (rows):
        l = [binomial(n, k) for k in range (0,n+1)]
        print l

pascals_triangle(5)

output:
    [1]
    [1, 1]
    [1, 2, 1]
    [1, 12, 3, 1]
    [1, 144, 24, 4, 1]
huang
  • 521
  • 3
  • 11
0

Your binomial function had a small bracketing mistake in it, which was giving you incorrect output:

from math import factorial

def binomial(n, k):
    if k==0:
        return 1
    else:
        return int((factorial(n)/(factorial(k)*factorial(n-k))))

def pascals_triangle(rows, max_width):
    for n in range (0,rows):
        indent = (rows - n - 1) * max_width
        print(' ' * indent, end='')
        for k in range(0, n+1):
            print("{:^{w}}".format(binomial(n, k), w = max_width*2), end='')
        print()

pascals_triangle(7, 2)

With the addition of a padding parameter, the output can be made to look like this:

             1  
           1   1  
         1   2   1  
       1   3   3   1  
     1   4   6   4   1  
   1   5   10  10  5   1  
 1   6   15  20  15  6   1
Martin Evans
  • 45,791
  • 17
  • 81
  • 97