Ok. I know this isnt a math class, but I have some questions about the Math Classes in C#. For example, lets say I have 3 numbers. Each can be 0-10. How would I be able to LIST all the combiniations of that. Can someone please help me out
Well... I didnt want to mark just one reply as the answer, because all of them were very appreciated. I thank you all very much for you reply for I could not find any help anywhere else. Thank you so much again.
In ordered combinations one has the number of possibilities (in your case 0 - 10) and the number of places to fill (in your case 3). To find out how many possible combinations there are one can use the mathematical function factorial. It's suprising the C# Math library does not contain this function as it is used often in computer science. Not to worry though, it's a simple recurisvely defined function; below is my version.
private int
factorial(int theNumber)
{
if (theNumber <= 1)
return 1;
else
return theNumber * (factorial(theNumber - 1));
}
This only gives you the number of combinations if the possibilities == the places. So what you need do is call factorial as follows to get the number of combinations given a set of places smaller than or equal to the possibilities(you'll have to define variables for the possibilities and places):
Thanks for participation (and you right, Math class could contain more about Math...), but one thing I can't pass by - recursive methods. This is because it's not optimal to write such code, here is how it should be done:
public
static class MathEx
{
public static int Factorial(int iterations)
{
int result = 1;
for (int i = 2; i <= iterations; i++)
{
result *= i;
}
return result;
}
}
In this version, there is no method calls, so no overhead related to method calls. This will be much faster and optimal implementation. Every time you call method, local stack allocated, parameters transferred to method and all this include reading/writing memory, which is much slower than * operation in CPU. If you use for() - only 3 variables in memory required - iterations, i & result. They all will be placed into CPU registers and processed much faster.
If what you are looking for is output like: 000 001 002 ...
the code below will do that. It takes as input the min and max value for each digit and the number of digits in the string.
Please forgive the formatting. This is my first post and this is how the source pasted in. I couldn't insert tabs without directly editing the HTML and I was having a heck of a time with increase/decrease indent.
Method Call: // Print the possible combinations of 3 digits between 3 and 5 inclusive PrintCombinations(3, 5, 3);
public void PrintCombinations(int minValue, int maxValue, int count) { bool done = false; // The max value that any offset should be int maxOffset = maxValue - minValue; // Offsets for the individual digits int[] digitOffset = new int[count]; int x = 0;
// Loop while there is more to do while(!done) {
// Reset the output string string output = "";
// Print the current offset values for(x = 0; x < count; x++) { output += minValue + digitOffset[x]; }
Console.WriteLine(output);
// Increment the offset values // Starting with the rightmost digit for(x = count - 1; x >= 0; x--) { if(digitOffset[x] == maxOffset) {
// If the digit offset is equal to the // max offset, reset it to 0 digitOffset[x] = 0;
// We may or may not be done done = true;
} else {
// Otherwise increment the offset digitOffset[x]++;
// As long as an offset was incremented // there is more work to do. done = false; break;
There's nothin inherent to the Math namespace to do what you want. do you want all combinations of the 3 specific numbers, or all the combinations of 3 numbers in the range 0-10
I tried copying and pasting that code snippet. But I get errors. For the Triple...It asks if I am missing an assembly reference. Could you explain further please.
Math
ccchai
Triple is defined at the end of the posted code, could you please post the complete error
Janni J
what you're looking for is permutation of 3 numbers. I don't have much time to explain it but check this page for the algorithm: http://csharpcomputing.com/Tutorials/Lesson7.htm
If you don't understand it just ask and when I have more time I'll describe it.
vanita
KK Wong
BillH
Hi. Cool question, I hope the following helps.
In ordered combinations one has the number of possibilities (in your case 0 - 10) and the number of places to fill (in your case 3). To find out how many possible combinations there are one can use the mathematical function factorial. It's suprising the C# Math library does not contain this function as it is used often in computer science. Not to worry though, it's a simple recurisvely defined function; below is my version.
private int
factorial(int theNumber){
if (theNumber <= 1)
return 1;
else
return theNumber * (factorial(theNumber - 1));
}
This only gives you the number of combinations if the possibilities == the places. So what you need do is call factorial as follows to get the number of combinations given a set of places smaller than or equal to the possibilities(you'll have to define variables for the possibilities and places):
int
m_intNumberOfCombinations = factorial(theNumberOfPossibilities)/ factorial(theNumberOfPossibilities - theNumberOfPlaces);
Dennisld
Do you mean this
hharding
Hi GvS!
Thanks for participation (and you right, Math class could contain more about Math...), but one thing I can't pass by - recursive methods. This is because it's not optimal to write such code, here is how it should be done:
public
static class MathEx{
public static int Factorial(int iterations){
int result = 1; for (int i = 2; i <= iterations; i++){
}
return result;}
}
In this version, there is no method calls, so no overhead related to method calls. This will be much faster and optimal implementation. Every time you call method, local stack allocated, parameters transferred to method and all this include reading/writing memory, which is much slower than * operation in CPU. If you use for() - only 3 variables in memory required - iterations, i & result. They all will be placed into CPU registers and processed much faster.
If you feel interested in performance & optimizations questions - check this thread and vote there: http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=329219&SiteID=1
Roberson
If what you are looking for is output like:
000
001
002
...
the code below will do that. It takes as input the min and max value for each digit and the number of digits in the string.
Please forgive the formatting. This is my first post and this is how the source pasted in. I couldn't insert tabs without directly editing the HTML and I was having a heck of a time with increase/decrease indent.
Method Call:
// Print the possible combinations of 3 digits between 3 and 5 inclusive
PrintCombinations(3, 5, 3);
public void PrintCombinations(int minValue, int maxValue, int count)
{
bool done = false;
// The max value that any offset should be
int maxOffset = maxValue - minValue;
// Offsets for the individual digits
int[] digitOffset = new int[count];
int x = 0;
// Loop while there is more to do
while(!done)
{
// Reset the output string
string output = "";
// Print the current offset values
for(x = 0; x < count; x++)
{
output += minValue + digitOffset[x];
}
Console.WriteLine(output);
// Increment the offset values
// Starting with the rightmost digit
for(x = count - 1; x >= 0; x--)
{
if(digitOffset[x] == maxOffset)
{
// If the digit offset is equal to the
// max offset, reset it to 0
digitOffset[x] = 0;
// We may or may not be done
done = true;
}
else
{
// Otherwise increment the offset
digitOffset[x]++;
// As long as an offset was incremented
// there is more work to do.
done = false;
break;
}
}
}
}
Hope this helps.
David B. P
webGov