0

Here's my piece of code. Say whay's wrong.

array = [[0]*101]*101
#To initialise a 2d array of size 101x101

def ncr(n, r):
    global array
    if r==0 or n==r:
        return 1
    if array[n][r] is not 0:
        return array[n][r]
    array[n][r] = ncr(n-1,r)+ncr(n-1,r-1)
    return array[n][r]


print ncr(11,8)

Now the output of the program should be ncr(11,8) = 165. But it prints 9841. Whats wrong with the code?

Ayush Mishra
  • 1,273
  • 3
  • 11
  • 13
  • Prints 25 with python 3.3.4. Also, you probably don't have to implement it yourself: http://stackoverflow.com/questions/4941753/is-there-a-math-ncr-function-in-python – J0HN Jul 07 '14 at 11:00
  • 1
    In the future, bear in mind that *"Say whay's [sic] wrong."* is *not* a good way to ask a question. – jonrsharpe Jul 07 '14 at 11:07

0 Answers0