Wrap your head around this.
-
Can you figure out what it does? You won't have much luck trying to step through it or running it with numbers larger than two.
int C(int n, int k)
{
if(k == 0 || k == n)
return 1;
else
return C(n - 1, k) + C(n - 1, k - 1);
} -
Can you figure out what it does? You won't have much luck trying to step through it or running it with numbers larger than two.
int C(int n, int k)
{
if(k == 0 || k == n)
return 1;
else
return C(n - 1, k) + C(n - 1, k - 1);
}looks like the combinatorial function, also known as Newton's binomial coefficients. See here[^]. I would: - choose a more appropriate method name, probably Combinatorial - perform better input checking, i.e. take care of n<=0 and of k outside [0,n] - add a comment to what it does - add a comment to how it does its job, probably including a hyperlink - choose a different algorithm, as this one causes up to 2^n evaluations, most of them identical (cache some results would help too) and needs an n-deep nesting. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
modified on Saturday, November 28, 2009 4:31 PM
-
looks like the combinatorial function, also known as Newton's binomial coefficients. See here[^]. I would: - choose a more appropriate method name, probably Combinatorial - perform better input checking, i.e. take care of n<=0 and of k outside [0,n] - add a comment to what it does - add a comment to how it does its job, probably including a hyperlink - choose a different algorithm, as this one causes up to 2^n evaluations, most of them identical (cache some results would help too) and needs an n-deep nesting. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
modified on Saturday, November 28, 2009 4:31 PM
-
I thought it looked familiar. There's something a little strange about it though; I just can't quite put my finger on it.
OSDev :)
There is a little trick: assuming the initial values of n and k are alright (i.e. 0 <= k <= n), there is no need for extra tests, as k can't leave the interval [0,n] without hitting one of the equalities k==0 or k==n. Providing a full check would be safer and look better IMO, something like:
if (k<0 || k>n) throw new ArgumentOutOfRangeException("k outside [0,n]");
:)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
-
There is a little trick: assuming the initial values of n and k are alright (i.e. 0 <= k <= n), there is no need for extra tests, as k can't leave the interval [0,n] without hitting one of the equalities k==0 or k==n. Providing a full check would be safer and look better IMO, something like:
if (k<0 || k>n) throw new ArgumentOutOfRangeException("k outside [0,n]");
:)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
It's a private method. It may well be that a public or protected method is exposed which does the initial argument check and then calls the private method. As you pointed out it's only necessary to check once. :)
-
Can you figure out what it does? You won't have much luck trying to step through it or running it with numbers larger than two.
int C(int n, int k)
{
if(k == 0 || k == n)
return 1;
else
return C(n - 1, k) + C(n - 1, k - 1);
}Wrap your head around this.
-
Wrap your head around this.
Wrap your head around this. What? What am I wrapping my head around?
-
Wrap your head around this. What? What am I wrapping my head around?
Can't help out there, unless you turn on your webcam. :doh:
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
-
Can't help out there, unless you turn on your webcam. :doh:
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Luc Pattyn wrote:
unless you turn on your webcam
Oh it's on alright ;P
-
Wrap your head around this. What? What am I wrapping my head around?
-
Can you figure out what it does? You won't have much luck trying to step through it or running it with numbers larger than two.
int C(int n, int k)
{
if(k == 0 || k == n)
return 1;
else
return C(n - 1, k) + C(n - 1, k - 1);
}Neat looks like the number of k-combinations from a set of n elements http://en.wikipedia.org/wiki/Combination
-
Can you figure out what it does? You won't have much luck trying to step through it or running it with numbers larger than two.
int C(int n, int k)
{
if(k == 0 || k == n)
return 1;
else
return C(n - 1, k) + C(n - 1, k - 1);
} -
Can you figure out what it does? You won't have much luck trying to step through it or running it with numbers larger than two.
int C(int n, int k)
{
if(k == 0 || k == n)
return 1;
else
return C(n - 1, k) + C(n - 1, k - 1);
} -
Can you figure out what it does? You won't have much luck trying to step through it or running it with numbers larger than two.
int C(int n, int k)
{
if(k == 0 || k == n)
return 1;
else
return C(n - 1, k) + C(n - 1, k - 1);
}pascal triangle it is
-
Can you figure out what it does? You won't have much luck trying to step through it or running it with numbers larger than two.
int C(int n, int k)
{
if(k == 0 || k == n)
return 1;
else
return C(n - 1, k) + C(n - 1, k - 1);
}I afraid if k is greater than n first call to C in second return [C(n - 1, k)] will end up with exception as it will go in infinite loop.. :^) BTW this method will always return value greater than 1 if not an exception. CASE CLOSED .. Its Nothing ;P
modified on Wednesday, March 31, 2010 8:11 AM
-
Can you figure out what it does? You won't have much luck trying to step through it or running it with numbers larger than two.
int C(int n, int k)
{
if(k == 0 || k == n)
return 1;
else
return C(n - 1, k) + C(n - 1, k - 1);
}Appears to be a Fibonacci sequence.