how can i find how many combinations(possibilities) can i do from the numbers that i get.
for example i get 1,2,3 so the combinaions is: 1,3,2 3,1,2 3,2,1
......(there is more)
you have to use all the three numbers in the combination.
This is a discussion on Numbers combinations(possibilities) within the C Programming forums, part of the General Programming Boards category; how can i find how many combinations(possibilities) can i do from the numbers that i get. for example i get ...
how can i find how many combinations(possibilities) can i do from the numbers that i get.
for example i get 1,2,3 so the combinaions is: 1,3,2 3,1,2 3,2,1
......(there is more)
you have to use all the three numbers in the combination.
Hang on. Are you trying to find out how many permutations there are, or what they are ?
Ray
Originally Posted by snaidis
how many.
Write all the perms for 2, 3 and 4 symbols, and observe the handy pattern they generate.
If you can't manage perms by hand, forget about trying to write the code.
N! (Pronounced N factorial)
So if there are 3 items with the set: i.e. 1,2,3
Then there are N! combos.
i.e 3! = 3x2x1 = 6
... 4! = 4x3x2x1= 24 and so on.
Do it recursively
or something to that effect...Code:int factorial(int n) { if (n ==0) return 1; return n * factorial(n-1) }
thanks a lot to all of you
>for example i get 1,2,3 so the combinaions is: 1,3,2 3,1,2 3,2,1
For future reference, combinations and permutations are very different. You want permutations.
>Do it recursively
It's just as easy to do it non-recursively. The factorial example is a good example of a bad way to use recursion.
My best code is written with the delete key.
If you were to do it recursively, how would you have done it ?Originally Posted by Prelude
>If you were to do it recursively, how would you have done it ?
I wouldn't have done it recursively, that's the point. A recursive factorial function doesn't buy you anything over a non-recursive function:
It's a silly example that displays exactly none of the benefits of recursion and all of the problems.Code:int factorial ( int n ) { int f = n != 0 ? n : 1; while ( --n > 1 ) f *= n; return f; }
My best code is written with the delete key.
>>A recursive factorial function doesn't buy you anything over a non-recursive function.
Sometimes it buy.
1.It makes the code short n sweet.
2.Sometimes its easy to find recursive than iterative soln for a novice like me.
Of course recursive soln have lots of overhead but who cares when it is short program .
Long time no C. I need to learn the language again.
Help a man when he is in trouble and he will remember you when he is in trouble again.
You learn in life when you lose.
Complex problems have simple, easy to understand wrong answers.
"A ship in the harbour is safe, but that's not what ships are built
for"
She said a recursive factorial function, not a recursive function. Specific vs general.
>Sometimes it buy.
If your compiler fixes tail recursion then you won't see any difference, but unless there's a serious benefit to recursion (which there isn't in this case), you would be better off writing something that is more likely to be good everywhere.
>1.It makes the code short n sweet.
So 3 lines is short and sweet, and 4 lines is ridiculously long? I can take out whitespace and merge the loop condition and body if it makes you feel better:
There, 3 lines, just like the recursive version.Code:int factorial ( int n ) { int f = n != 0 ? n : 1; while ( --n > 1 ) f *= n; return f; }
>2.Sometimes its easy to find recursive than iterative soln for a novice like me.
Well, aside from being trivial for even the greenest C programmer, I just posted an interative solution.
>Of course recursive soln have lots of overhead but who cares when it is short program
Factorials are useful. You'll find that as a utility, a function that gives you the factorial of N will be used in programs of any size. So it's wise to come up with the best solution you can manage, and because a recursive solution has no benefit over a non-recursive solution, why are you even bothering to defend your position?
My best code is written with the delete key.
>>So 3 lines is short and sweet, and 4 lines is ridiculously long?
>>Well, aside from being trivial for even the greenest C programmer, I just posted an interative solution.
I was not talking abt factorial problem but a general case.
>>why are you even bothering to defend your position?
I was not defending or arguing.I was telling my opinion or with which i am comfortable with.
Long time no C. I need to learn the language again.
Help a man when he is in trouble and he will remember you when he is in trouble again.
You learn in life when you lose.
Complex problems have simple, easy to understand wrong answers.
"A ship in the harbour is safe, but that's not what ships are built
for"
>I was not talking abt factorial problem but a general case.
Then see Thantos' reply. The off-topic parts of the thread would grow by leaps and bounds if we started talking about recursion in general.
My best code is written with the delete key.