Quick actions

cmd+k|ctrl+k

Navigation

Languages

RecursionHW 1/12/16

Snippet info

Language

Java

Visibility

public

Author

rjm27trekkie

Created

2016-01-12T20:38:46Z

Updated

2016-01-12T20:47:13Z

import java.util.*;

class Main {
    
    private static int count1 = 0;
    private static int count2 = 0;
    
    public static void main(String[] args)
    {
       System.out.println(choose1(4, 2));
       System.out.println(count1);
       System.out.println(choose2(4, 2));
       System.out.println(count2);
    }
    
    public static int choose1(int n, int k)
    {
        count1++;
        if(k == 0)
        {
            return 1;
        }
        else
        {
            return choose1(n, k-1) * (n-k+1)/k;
        }
    }
    
    public static int choose2(int n, int k)
    {
        count2++;
        if(k < 0 || k > n)
        {
            return 0;
        }
        else if (n == 0)
        {
            return 1;
        }
        else
        {
            return choose2(n-1, k-1) + choose2(n-1, k);
        }
    }
}
INFO