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