Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Iteration to Recursion

Name: Anonymous 2012-02-18 22:23

Is it possible to convert the following iteration code into a recursion one?

public static void triangle(int m, int n)
{
if(m<=n)
{

int x =0;
int y =0;

while(x<m)
{

System.out.print("*");

x++;

}

System.out.print("\n");
triangle(m + 1,n);

while(y<m)
{

System.out.print("*");

y++;
}

System.out.print("\n");


}

}

The output is an asterisks of a triangle.

Name: Anonymous 2012-02-18 22:29

row = [1]

def binomial(n):
    if n == 0:
        return [1]
    else:
        mrow = [0]
        mrow += binomial(n-1)
        mrow += [0]
        nrow = []

        for k in xrange(len(mrow)-1):
            nrow += [mrow[k] + mrow[k+1]]

        return nrow


# test the recursive binomial function
for n in range(5):
    print binomial(n)

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List