Java examples for java.lang:Math Calculation
Computes (N choose K), the number of ways to select K different elements from a set of size N.
// Copyright (C) 2002-2012 Three Rings Design, Inc., All Rights Reserved //package com.java2s; public class Main { /**//from ww w . j ava2 s . c o m * Computes (N choose K), the number of ways to select K different elements from a set of size * N. */ public static int choose(int n, int k) { // Base case: One way to select or not select the whole set if (k <= 0 || k >= n) { return 1; } // Recurse using pascal's triangle return (choose(n - 1, k - 1) + choose(n - 1, k)); } }