0

I mean to define the following recursive function

recursive formula

But I don't know what goes wrong in my code. May anyone more experienced with Python take a look into that? Thank you!


    import scipy.special

def F(n,k):
    if (k == 1):
        F = 1
    else:
        F = 0
        j = 1
        while (j <= n-k+1):
            a = scipy.special.binom(n,j)
            b =  F(n-j,k-1)
            F = F + a*b
            j = j + 1
    return F 
5
  • please fix formatting so this is legible Commented Sep 13, 2019 at 22:34
  • @DetectivePikachu For some reason, Stack Overflow doesn't support LaTeX, even though other Stack Exchange communities such as Mathematics does. I added a rendered image of the equation. Commented Sep 13, 2019 at 22:40
  • It would help if you would give an input for which the expected output clashes with the output that would come from evaluating the formula by hand. Commented Sep 13, 2019 at 22:51
  • 1
    The attempted MathJax in your last edit is almost certainly not what you wanted. Please either restore the graphic inserted by John Coleman or correct your MathJax. If you do the latter, one of us can insert the appropriate graphic. Commented Sep 13, 2019 at 23:21
  • @RoryDaulton Sorry, I'm not allowed to upload a picture yet, and it seems that I couldn't restore sth. before either. I didn't know there's a picture. I'll see what I can do... sorry Commented Sep 17, 2019 at 23:21

2 Answers 2

1

The problem with @ArielLeung's code is that it defines both the function F(n,k) and the variable returned F as F. This creates ambiguity in the variable namespace.

The following should work fine.

import scipy.special

def F(n,k):
    f = 0
    if (k == 1):
        f = 1
    else:
        f = 0
        j = 1
        while (j <= n-k+1):
            a = scipy.special.binom(n,j)
            b =  F(n-j,k-1)
            f = f + a*b
            j = j + 1
    return f

However, I would prefer @Nikaidoh's list comprehension as a much concise solution.

Sign up to request clarification or add additional context in comments.

2 Comments

Good catch. I had trouble spotting the error (which is obvious in retrospect).
@ArielLeung Please accept the best answer. How to accept an answer
1

You need something like this:

import scipy.special

def F(n,k):
    if (k == 1):
        return 1
    else:
        return sum(scipy.special.binom(n,j) * F(n-j,k-1) for j in range(1, n-k+2))

In your code you are reassigning to F, which you define as function (def F(n,k)), the value of an int (F = 1). In this way, when the value k is greater than 1 you get this error:

b =  F(n-j,k-1)
TypeError: 'int' object is not callable

Because now F is an int, not a function anymore

5 Comments

This is a cleaner way to write it than OP's way (so +1) -- but it really seems like it is computing the same function. It doesn't explain what, if anything, is actually wrong with OP's code. Minor nit-pick: why the square brackets inside of sum()? No real reason to gather the iterable into a list before summing.
@JohnColeman I updated the answer with the error :) thanks for the suggestion!
Thanks so much, guys! This is very clarifying! (I'm a beginner in Python. I just write whatever comes to my mind first. So sometimes I don't kow what I'm doing ...)
@Ariel Leung. You are welcome. Please accept the best answer also :). Hope to see you around
Also, when n takes larger numbers, these codes become rather slow. For example, it takes more than 15 minutes to compute F(1000,500). So I'm thinking ... are there faster way to do a recursive function? A package, perhaps? Or I'm just daydreaming, then ignore me ... sorry. And thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.