Thread: Numbers combinations(possibilities)

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    22

    Numbers combinations(possibilities)

    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.

  2. #2
    Registered User
    Join Date
    Dec 2003
    Posts
    12
    Hang on. Are you trying to find out how many permutations there are, or what they are ?

    Ray

    Quote Originally Posted by snaidis
    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.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    22
    how many.

  4. #4
    Super Moderator Harbinger's Avatar
    Join Date
    Nov 2004
    Posts
    74
    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.

  5. #5
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    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.

  6. #6
    Registered User
    Join Date
    Dec 2003
    Posts
    12
    Do it recursively

    Code:
    int factorial(int n) 
    {
     if (n ==0)
         return 1;
     return n * factorial(n-1)
    }
    or something to that effect...

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    22
    thanks a lot to all of you

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.

  9. #9
    Registered User
    Join Date
    Dec 2003
    Posts
    12
    Quote Originally Posted by Prelude
    The factorial example is a good example of a bad way to use recursion.
    If you were to do it recursively, how would you have done it ?

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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:
    Code:
    int factorial ( int n )
    {
      int f = n != 0 ? n : 1;
    
      while ( --n > 1 )
        f *= n;
    
      return f;
    }
    It's a silly example that displays exactly none of the benefits of recursion and all of the problems.
    My best code is written with the delete key.

  11. #11
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    >>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"

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    She said a recursive factorial function, not a recursive function. Specific vs general.

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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:
    Code:
    int factorial ( int n )
    {
      int f = n != 0 ? n : 1;
      while ( --n > 1 ) f *= n;
      return f;
    }
    There, 3 lines, just like the recursive version.

    >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.

  14. #14
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    >>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"

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Rational Numbers (C++)
    By cloudjc in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2008, 04:03 PM
  2. Writing unique numbers to an array
    By yardy in forum C Programming
    Replies: 6
    Last Post: 12-27-2006, 09:15 PM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM