participate


New To Java - Combinations problem.
This question is not answered.

<<   Back to Forum  |   Give us Feedback
This topic has 5 replies on 1 page.
Parastar
Posts:45
Registered: 10/16/09
Combinations problem.   
Nov 4, 2009 5:56 AM
 
 
Wanted to know how this program functions.
tried hard to figure out the code,but couldn't.
can someone explain to me step by step?

   FIGURE 5-5 Program to compute the combinations function C(n, k)
/*
* File: Combinations.java
* -----------------------
* This program computes the mathematical combinations function
* C(n, k), which is the number of ways of selecting k objects
* from a set of n distinct objects.
*/
import acm.program.*;
public class Combinations extends ConsoleProgram {
/* Runs the program */
public void run() {
int n = readInt("Enter number of objects in the set (n): ");
int k = readInt("Enter number to be chosen (k): ");
println("C(" + n + ", " + k + ") = " + combinations(n, k));
}
/*
* Returns the mathematical combinations function C(n, k),
* which is the number of ways of selecting k objects
* from a set of n distinct objects.
*/
private int combinations(int n, int k) {
return factorial(n) / (factorial(k) * factorial(n - k));
}
/*
* Returns the factorial of n, which is defined as the
* product of all integers from 1 up to n.
*/
private int factorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
}             
 
kevinaworkman
Posts:1,305
Registered: 3/30/09
Re: Combinations problem.   
Nov 4, 2009 5:59 AM (reply 1 of 5)  (In reply to original post )
 
 
Parastar wrote:
tried hard to figure out the code,but couldn't.

Where did you get stuck? How did you try to "figure out the code"? If you haven't already, try stepping through it with a debugger. Or (arguably) better yet, step through it yourself using a piece of paper and a pencil to keep track of what's happening.
 
tschodt
Posts:4,430
Registered: 4/24/98
Re: Combinations problem.   
Nov 4, 2009 6:17 AM (reply 2 of 5)  (In reply to original post )
 
 
Parastar wrote:
... how this program functions.
tried hard to figure out the code, but couldn't.
What part of it do you not understand?

/*
* Returns the mathematical combinations function C(n, k),
* which is the number of ways of selecting k objects
* from a set of n distinct objects.
*/
private int combinations(int n, int k) {
return factorial(n) / (factorial(k) * factorial(n - k));
}
That was combination

/*
* Returns the factorial of n, which is defined as the
* product of all integers from 1 up to n.
*/
private int factorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
}             
That was factorial
 
Parastar
Posts:45
Registered: 10/16/09
Re: Combinations problem.   
Nov 4, 2009 7:11 AM (reply 3 of 5)  (In reply to #2 )
 
 
-I cant understand where factoria(k) comes from.
-can I write factorial(n-k) as factorial(n)-factorial(k)?
 
warnerja
Posts:23,517
Registered: 9/26/01
Re: Combinations problem.   
Nov 4, 2009 7:34 AM (reply 4 of 5)  (In reply to #3 )
 
 
Parastar wrote:
-I cant understand where factoria(k) comes from.
This isn't a Java related problem, but a math one. factorial(k) is k!, meaning
k * (k - 1) * (k - 2) * (k - 3) ... * 1

-can I write factorial(n-k) as factorial(n)-factorial(k)?
No, as for example (4 - 2)! is 2! (equals 2), which is not the same as 4! - 2! (equals 22 if I did my math right)
 
JosAH
Posts:12,932
Registered: 4/6/04
Re: Combinations problem.   
Nov 4, 2009 7:34 AM (reply 5 of 5)  (In reply to #3 )
 
 
Parastar wrote:
-I cant understand where factoria(k) comes from.
-can I write factorial(n-k) as factorial(n)-factorial(k)?

No need to calculate all those factorials, most of them cancel out anyway; have a look at this:

public static int comb(int n, int k) {
		
	int m= Math.min(n-k, k);
		
	int c= 1;
	for (int i= 1; i <= m; i++) {
		c*= n--; c/= i;
	}
		
	return c;
}


It takes longer (larger values of n and k) before this method overflows on average.

kind regards,

Jos
 
This topic has 5 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 29
  • Guests : 131

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums