views:

294

answers:

3

I passed through this recursion example, but I couldn't find out its purpose. Could you please help me?

public static int somerec(int n, int x, int y) {
    if (n < x*y)
        return 0;
    else if (n == x*y)
        return 1;
    else {
        int sum = 0;
        for (int i = x; i <= n;i++)
             sum += somerec(n-i, i, y-1);
        return sum;
    }
}

This is not a homework.

+3  A: 

Perhaps running the code may help you understand?

Here's my quick-n-dirty reimplementation in Python:

def somerec(n, x, y):
    if n < x*y:
        return 0
    if n == x*y:
        return 1
    sum_ = 0
    for i in range(x, n+1):
        sum_ += somerec(n-i, i, y-1)
    return sum_

Trying it out:

>>> [somerec(i, 1, 2) for i in range(30)]
[0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 
12, 13, 13, 14, 14]
>>> [somerec(i, 1, 3) for i in range(30)]
[0, 0, 0, 1, 1, 2, 3, 4, 5, 7, 8, 10, 12, 14, 16, 19, 21, 24, 27, 30, 33, 37, 40,
44, 48, 52, 56, 61, 65, 70]
>>> [somerec(i, 1, 4) for i in range(30)]
[0, 0, 0, 0, 1, 1, 2, 3, 5, 6, 9, 11, 15, 18, 23, 27, 34, 39, 47, 54, 64, 72, 84, 
94, 108, 120, 136, 150, 169, 185]
>>> [somerec(i, 2, 4) for i in range(30)]
[0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3, 5, 6, 9, 11, 15, 18, 23, 27, 34, 39, 47, 54,
64, 72, 84, 94, 108, 120]
Daniel Pryden
so what would you call that?
hhafez
Or perhaps not :-)
paxdiablo
I have no idea what it does either. But I figured, since I went through the trouble of writing it, I might as well post it.
Daniel Pryden
+9  A: 

It sorts out those students with good analytical capabilities from those without.

As such, it's a valuable tool for educators to figure out which students should be given a passing grade and which ones should be nudged towards a career in a different area :-)

paxdiablo
Completely true, except that I am not a student and this is not a question to assess me in anyway. I'm just curious about the solution :)
Sarah
@Sarah:"just curious about the solution " Why -> there are literally thousands of these things, to say the least. In general, what you should do here is slap the code in an editor and compile it and play around with it. My first ten lines of code blew into stl some ten thousand lines into the sources, I spotted then and there your basic ten-millimeter, lice-infested, blow out the stacks tool. I ended up writing a shift-or otp. To this day, I cannot get over the need to have hardware randomizers. That's because I was trying to do something. What are you trying to do.
Nicholas Jordan
@Nicholas: you forgot to mention how you linked the flux capacitor to the chrome-plated double overhead foxtrots .... :-)
Stephen C
This is not an answer, so why has it received so many votes?! It's true that such questions sort out students, but still, this is more of a comment than an answer.
Hosam Aly
Actually it is an answer. The purpose of the code is to do exactly what I have stated, sort out the wheat from the chaff :-) It may not be the answer the original poster wanted but I wouldn't be concerned about the voting because (1) it's community wiki so I'm not benefiting; and (2) there are no other answers that explain it yet. I'm sure that, when (or if) they arrive, they'll be voted above mine (or I'll delete mine to make sure they do).
paxdiablo
Actually I have an answer, but I cannot post it while the question is closed.
Hosam Aly
+3  A: 

Well this may sound ridiculous but that function:

Returns the number for times the product of x times y is exactly n

What I can't tell are the increments of n,x and y nor what do they describe.

For instance

for f( n=8, x=2, y=3) the value is 2 ( when

n=0, x=4, y=0,

n=3, x=3, y=1 )

f( n=9, x=2, y=3) the value is 3 ( when

n=0, x=5, y=0 ,

n=0, x=4, y=0,

n=6, x=3, y=2 )

Each time the product of x times y is exactly n the count increments.

I cross post this to mathoverflow to see if someone there knows the answer.

OscarRyz