Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. Wrap your head around this.

Wrap your head around this.

Scheduled Pinned Locked Moved Clever Code
question
16 Posts 13 Posters 44 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CaptainSeeSharp
    wrote on last edited by
    #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);
    }

    Watch the Fall of the Republic (High Quality 2:24:19)[^]

    L P G L L 8 Replies Last reply
    0
    • C CaptainSeeSharp

      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);
      }

      Watch the Fall of the Republic (High Quality 2:24:19)[^]

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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

      0 1 Reply Last reply
      0
      • L Luc Pattyn

        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

        0 Offline
        0 Offline
        0x3c0
        wrote on last edited by
        #3

        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 :)

        L 1 Reply Last reply
        0
        • 0 0x3c0

          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 :)

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          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


          D 1 Reply Last reply
          0
          • L Luc Pattyn

            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


            D Offline
            D Offline
            dojohansen
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • C CaptainSeeSharp

              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);
              }

              Watch the Fall of the Republic (High Quality 2:24:19)[^]

              P Offline
              P Offline
              pengchengwanli
              wrote on last edited by
              #6

              Wrap your head around this.

              N 1 Reply Last reply
              0
              • P pengchengwanli

                Wrap your head around this.

                N Offline
                N Offline
                NormDroid
                wrote on last edited by
                #7

                Wrap your head around this. What? What am I wrapping my head around?

                I wish I could tell you that the Monopoly guy fought the good fight and the Sisters let him be. I wish I could tell you that. But prison is no fairy-tale world

                L D 2 Replies Last reply
                0
                • N NormDroid

                  Wrap your head around this. What? What am I wrapping my head around?

                  I wish I could tell you that the Monopoly guy fought the good fight and the Sisters let him be. I wish I could tell you that. But prison is no fairy-tale world

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  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


                  N 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    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


                    N Offline
                    N Offline
                    NormDroid
                    wrote on last edited by
                    #9

                    Luc Pattyn wrote:

                    unless you turn on your webcam

                    Oh it's on alright ;P

                    I wish I could tell you that the Monopoly guy fought the good fight and the Sisters let him be. I wish I could tell you that. But prison is no fairy-tale world

                    1 Reply Last reply
                    0
                    • N NormDroid

                      Wrap your head around this. What? What am I wrapping my head around?

                      I wish I could tell you that the Monopoly guy fought the good fight and the Sisters let him be. I wish I could tell you that. But prison is no fairy-tale world

                      D Offline
                      D Offline
                      Dan Neely
                      wrote on last edited by
                      #10

                      Dunno, but the last 3 of his 7 total posts have been copies of a subject line. The other 4 aren't exactly fonts of knowledge either.

                      3x12=36 2x12=24 1x12=12 0x12=18

                      1 Reply Last reply
                      0
                      • C CaptainSeeSharp

                        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);
                        }

                        Watch the Fall of the Republic (High Quality 2:24:19)[^]

                        G Offline
                        G Offline
                        Gareth Richards
                        wrote on last edited by
                        #11

                        Neat looks like the number of k-combinations from a set of n elements http://en.wikipedia.org/wiki/Combination

                        1 Reply Last reply
                        0
                        • C CaptainSeeSharp

                          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);
                          }

                          Watch the Fall of the Republic (High Quality 2:24:19)[^]

                          L Offline
                          L Offline
                          leeosun
                          wrote on last edited by
                          #12

                          I think it`s a formula in mathematics, it`s a formula on combination!

                          1 Reply Last reply
                          0
                          • C CaptainSeeSharp

                            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);
                            }

                            Watch the Fall of the Republic (High Quality 2:24:19)[^]

                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #13

                            hey captain, are you still on christmas holiday :laugh:

                            Ravie Busie Coding is my birth-right and bugs are part of feature my code has! _________________________________________ Me  Facebook  Twitter

                            1 Reply Last reply
                            0
                            • C CaptainSeeSharp

                              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);
                              }

                              Watch the Fall of the Republic (High Quality 2:24:19)[^]

                              U Offline
                              U Offline
                              User 770072
                              wrote on last edited by
                              #14

                              pascal triangle it is

                              1 Reply Last reply
                              0
                              • C CaptainSeeSharp

                                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);
                                }

                                Watch the Fall of the Republic (High Quality 2:24:19)[^]

                                N Offline
                                N Offline
                                NavnathKale
                                wrote on last edited by
                                #15

                                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

                                1 Reply Last reply
                                0
                                • C CaptainSeeSharp

                                  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);
                                  }

                                  Watch the Fall of the Republic (High Quality 2:24:19)[^]

                                  C Offline
                                  C Offline
                                  CircusUgly
                                  wrote on last edited by
                                  #16

                                  Appears to be a Fibonacci sequence.

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups