Thread: how to find a mathematical formula for this recursion??

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    how to find a mathematical formula for this recursion??

    i got this recursion

    Code:
    int b(int n,int count) {
      int i;
      count =a(n,count);
         for(i=0;i<n;i++)
         count =b(i,count);
         return count;
    b(0,0)=1
    b(1,0)=3
    b(1,1)=4
    b(2,0)=8

    the formula for function "a" is a(n,c)=2^n + c

    what is the formal way to find a formula for b
    so i could predict whats the output of each input like b(12,15)??
    i dont have any intuition
    i am looking for the formal way
    Last edited by transgalactic2; 10-09-2008 at 02:52 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Most any book with "discrete mathematics" in the title will contain information on solving recurrence relations (which is what this is). I don't feel like typing two chapters into this little box, so go read.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Something like:
    b(n, count) = foldr (b, a(n,count), [0..n])

    [0..n] is the list of elements from 0 to n. In math terms this is a sequence.

    foldr is accumulate function:
    Prototype would look something like:
    in foldr(int (*func)(int,int), int initial, List list)
    It applies func to each element of the list, taking the previous result as the first argument except for the first tiome, when initial is used.
    It can be defined recursively as:
    if the list is empty, the result is the initial value z
    if not, apply f to the first element and the result of folding the rest

    That's a math formula for you. You can try to simplify it by moving the recursion out of foldr and into b, so that b recurses twice. Then apply other simplification methods on recursive functions.
    The general process is:
    1) Convert loops into common higher order recursive functions, like foldr.
    2) Convert the compleat function to not use higher order functions.
    3) Apply techniques for simplifying recursive functions.

    But I'm a computer programmer, and this is a programming forum. A mathematician will probably give you better advice.

    EDIT: forgot about a().
    Last edited by King Mir; 10-09-2008 at 07:00 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Did you mean to say
    Code:
    count += b(i,count);
    ... otherwise it assigns count a lot redundantly. I can't replicate your results nohow.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Here it is simplified to first order:

    Code:
    b(c, n) = foldr (b, a(n,c), [0..n])
    
    b(c, n) = b( a(n,c),f([0..n]) )
    f(emptyList) = emptyList
    f(list) = b(  list[0], f( tail(list) )  )
    
    Tail is function that returns the rest of the list, excluding list[0].
    But that's as far as I know how to take this. As tabstop sais, consult a discrete math textbook.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by nonoob View Post
    Did you mean to say
    Code:
    count += b(i,count);
    ... otherwise it assigns count a lot redundantly. I can't replicate your results nohow.
    It's not redundant, because count appears again in the right side of the equation.

    Effectively it's the same as:
    Code:
    int i=0
    for(;i<n;++i)
       count op= i;
    Where op is the function b, except with argument reversed.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    this is a part of the solution but
    i cant completely understand how to get there
    and how to come to a formula from that expression

    b(n,count) = b(n,0)+count
    b(n):=b(n,0)
    b(n) = 2^n + b(0) + b(1) + b(2) + ... + b(n-1)

    and using this outputs
    b(0,0)=1
    b(1,0)=3
    b(1,1)=4
    b(2,0)=8

    in the end we need to come to the formula
    b(n,c)=c+(n+2)*2^(n-1)

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You'll need to figure out what b(0) is, to start the recurrence off. You'll also want to write b(n) in terms of b(n-1). Given your first three lines are true (I haven't tried to verify them against your code):
    b(n-1) = 2^(n-1) + b(0) + ... + b(n-2)
    b(n) = 2^n + b(0) + ... + b(n-2) + b(n-1)
    you can write b(n) = b(n-1) + b(n-1) + 2^(n-1), with the first b(n-1) being the literal b(n-1) at the end of the expression, the second b(n-1) being everything else in the expression (since it matches the first line above it, except for the constant), and the last bit being the difference between the constants.

    And then your discrete math textbook should have the proof that a recurrence of the form b(n) = 2b(n-1) + 2^(n-1) has the form of c2^n + dn2^n for some c and d.

  9. #9
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i got the idea that i need to build a formula for
    a(n) using a(n-1) terms
    the time in the test only allows me to find this 4 cases
    b(0,0)=1
    b(1,0)=3
    b(1,1)=4
    b(2,0)=8

    i cant see any pattern by looking only at these 4

    if i will study the discrete math course
    could i find your formula using these 4 cases?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Yes. (I believe you would need three to solve the 3x3 system for the missing coefficients.)

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I swear this looks familiar. I believe this question was asked last week.

  12. #12
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i got these points for this function "b" :
    b(0,0)=1
    b(1,0)=3
    b(1,1)=4
    b(2,0)=8

    b(0)=1
    b(1)=2+1=3
    b(2)=4+1+3=8

    b(n)=2^n +b(0) +b(1) +b(2) +.. + b(n-2)+b(n-1)

    b(n-1)=2^n +b(0) +b(1) +b(2) +.. + b(n-2)

    b(n)=b(n-1)+b(n-1) =2*b(n-1)


    which by the way is wrong because b(1) doesn't equal to 2* b(0)

    b(n)=2*b(n-1)


    how to transform it to the formula
    b(n,c)=c+(n-2)*2^(n-1)


    ???

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That's because you can't get from
    b(n)=2^n +b(0) +b(1) +b(2) +.. + b(n-2)+b(n-1)
    to
    b(n-1)=2^n +b(0) +b(1) +b(2) +.. + b(n-2)
    (Or at least they're not consistent.) What you should have, matching what you posted way up top, is "b(n-1) = 2^(n-1) + b(0) + b(1) + ... + b(n-2)". Which is also why b(n) = 2*b(n-1) is wrong, and not what I posted above.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. linked list find
    By confuted in forum C++ Programming
    Replies: 7
    Last Post: 07-03-2003, 03:30 PM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  4. Q: Recursion to find all paths of a maze
    By reti in forum C Programming
    Replies: 7
    Last Post: 11-26-2002, 09:28 AM
  5. Won't Return pointer, i can't find why...
    By ss3x in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2002, 08:50 PM