Different combinations possible

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
4
down vote

favorite












Problem



Given a value n, imagine a mountain landscape inscribed in a reference (0, 0) to (2n, 0).
There musn't be white spaces between slopes and also the mountain musn't descend below the x axis.
The problem to be solved is: given n (which defines the size of the landscape) and the number k of peaks
(k always less than or equal to n), how many combinations of mountains are possible with k peaks?



Input



n who represents the width of the landscape and k which is the number of peaks.



Output



Just the number of combinations possible.



Example



Given n=3 and k=2 the answer is 3 combinations.



Just to give a visual example, they are the following:



 / / //
// / / /


are the 3 combinations possible using 6 (3*2) positions and 2 peaks.



Edit:
- more examples -



n k result
2 1 1
4 1 1
4 3 6
5 2 10









share|improve this question









New contributor




combinationsD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 2




    Is this the same as, "find the number of expressions of n matched parentheses pairs that contain exactly k instances of ()"?
    – xnor
    1 hour ago






  • 1




    https://oeis.org/A001263?
    – xnor
    1 hour ago






  • 1




    This also needs some more test cases for us to verify our solutions against.
    – Shaggy
    1 hour ago










  • @xnor yes it is.
    – Jonathan Allan
    41 mins ago














up vote
4
down vote

favorite












Problem



Given a value n, imagine a mountain landscape inscribed in a reference (0, 0) to (2n, 0).
There musn't be white spaces between slopes and also the mountain musn't descend below the x axis.
The problem to be solved is: given n (which defines the size of the landscape) and the number k of peaks
(k always less than or equal to n), how many combinations of mountains are possible with k peaks?



Input



n who represents the width of the landscape and k which is the number of peaks.



Output



Just the number of combinations possible.



Example



Given n=3 and k=2 the answer is 3 combinations.



Just to give a visual example, they are the following:



 / / //
// / / /


are the 3 combinations possible using 6 (3*2) positions and 2 peaks.



Edit:
- more examples -



n k result
2 1 1
4 1 1
4 3 6
5 2 10









share|improve this question









New contributor




combinationsD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 2




    Is this the same as, "find the number of expressions of n matched parentheses pairs that contain exactly k instances of ()"?
    – xnor
    1 hour ago






  • 1




    https://oeis.org/A001263?
    – xnor
    1 hour ago






  • 1




    This also needs some more test cases for us to verify our solutions against.
    – Shaggy
    1 hour ago










  • @xnor yes it is.
    – Jonathan Allan
    41 mins ago












up vote
4
down vote

favorite









up vote
4
down vote

favorite











Problem



Given a value n, imagine a mountain landscape inscribed in a reference (0, 0) to (2n, 0).
There musn't be white spaces between slopes and also the mountain musn't descend below the x axis.
The problem to be solved is: given n (which defines the size of the landscape) and the number k of peaks
(k always less than or equal to n), how many combinations of mountains are possible with k peaks?



Input



n who represents the width of the landscape and k which is the number of peaks.



Output



Just the number of combinations possible.



Example



Given n=3 and k=2 the answer is 3 combinations.



Just to give a visual example, they are the following:



 / / //
// / / /


are the 3 combinations possible using 6 (3*2) positions and 2 peaks.



Edit:
- more examples -



n k result
2 1 1
4 1 1
4 3 6
5 2 10









share|improve this question









New contributor




combinationsD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











Problem



Given a value n, imagine a mountain landscape inscribed in a reference (0, 0) to (2n, 0).
There musn't be white spaces between slopes and also the mountain musn't descend below the x axis.
The problem to be solved is: given n (which defines the size of the landscape) and the number k of peaks
(k always less than or equal to n), how many combinations of mountains are possible with k peaks?



Input



n who represents the width of the landscape and k which is the number of peaks.



Output



Just the number of combinations possible.



Example



Given n=3 and k=2 the answer is 3 combinations.



Just to give a visual example, they are the following:



 / / //
// / / /


are the 3 combinations possible using 6 (3*2) positions and 2 peaks.



Edit:
- more examples -



n k result
2 1 1
4 1 1
4 3 6
5 2 10






code-golf combinatorics recursion






share|improve this question









New contributor




combinationsD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




combinationsD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 1 hour ago









dylnan

3,8482528




3,8482528






New contributor




combinationsD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 hours ago









combinationsD

212




212




New contributor




combinationsD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





combinationsD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






combinationsD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 2




    Is this the same as, "find the number of expressions of n matched parentheses pairs that contain exactly k instances of ()"?
    – xnor
    1 hour ago






  • 1




    https://oeis.org/A001263?
    – xnor
    1 hour ago






  • 1




    This also needs some more test cases for us to verify our solutions against.
    – Shaggy
    1 hour ago










  • @xnor yes it is.
    – Jonathan Allan
    41 mins ago












  • 2




    Is this the same as, "find the number of expressions of n matched parentheses pairs that contain exactly k instances of ()"?
    – xnor
    1 hour ago






  • 1




    https://oeis.org/A001263?
    – xnor
    1 hour ago






  • 1




    This also needs some more test cases for us to verify our solutions against.
    – Shaggy
    1 hour ago










  • @xnor yes it is.
    – Jonathan Allan
    41 mins ago







2




2




Is this the same as, "find the number of expressions of n matched parentheses pairs that contain exactly k instances of ()"?
– xnor
1 hour ago




Is this the same as, "find the number of expressions of n matched parentheses pairs that contain exactly k instances of ()"?
– xnor
1 hour ago




1




1




https://oeis.org/A001263?
– xnor
1 hour ago




https://oeis.org/A001263?
– xnor
1 hour ago




1




1




This also needs some more test cases for us to verify our solutions against.
– Shaggy
1 hour ago




This also needs some more test cases for us to verify our solutions against.
– Shaggy
1 hour ago












@xnor yes it is.
– Jonathan Allan
41 mins ago




@xnor yes it is.
– Jonathan Allan
41 mins ago










4 Answers
4






active

oldest

votes

















up vote
0
down vote














Jelly, 8 bytes



,’$c’}P:


A dyadic Link accepting n on the left and k on the right which yields the count.



Try it online!






share|improve this answer



























    up vote
    0
    down vote













    APL(Dyalog), 19 18 16 bytes



    ⍺÷⍨×/(⍵,⍵-1)!⍺


    Uses the identity in the OEIS sequence. Takes n on the left and k on the right.



    TIO






    share|improve this answer



























      up vote
      0
      down vote














      Jelly, 7 bytes



      cⱮṫ-P÷⁸


      Try it online!



      Takes input as n then k. Uses the formula



      $N(n,k)=frac1nbinomnkbinomnk-1$



      which I found on Wikipedia.



      cⱮṫ-P÷⁸
      c Binomial coefficient of n and...
      â±® each of 1..k
      ṫ- Keep the last two. ṫ is tail, - is -1.
      P Product of the two numbers.
      ÷ Divide by
      ⁸ n.


      7 bytes



      Each line works on it's own.



      ,’$c@P÷
      c@€ṫ-P÷


      Takes input as k then n.






      share|improve this answer






















      • Wait... tail is automatically defined as 2 numbers? (Don't know Jelly at all, just a silly question)
        – Quintec
        20 mins ago










      • @Quintec There are two tail functions. One (Ṫ) that just takes the last element of a single argument and the one I used (ṫ) which takes two arguments. The fist argument is a list and the second one is a number (In my case -1 represented by a - in the code) which tells you how many elements to save. Having -1 give two elements was the golfiest way to define ṫ
        – dylnan
        15 mins ago










      • Gotcha, thanks! I see how jelly was built for golf... hehe
        – Quintec
        12 mins ago

















      up vote
      0
      down vote













      JavaScript (ES7), 59 58 49 bytes



      Takes input as (n)(k).





      n=>k=>(g=k=>k?m--*g(k-1)/k:1)(k,m=n)*g(k-1,m=n)/n


      Try it online!



      Computes:



      $$a_n,k=frac1kbinomn-1k-1binomnk-1=frac1nbinomnkbinomnk-1$$



      This formula comes from A001263, the OEIS sequence that xnor referred to in the challenge comments.






      share|improve this answer






















        Your Answer




        StackExchange.ifUsing("editor", function ()
        return StackExchange.using("mathjaxEditing", function ()
        StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
        StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
        );
        );
        , "mathjax-editing");

        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "200"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        convertImagesToLinks: false,
        noModals: false,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );






        combinationsD is a new contributor. Be nice, and check out our Code of Conduct.









         

        draft saved


        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f173081%2fdifferent-combinations-possible%23new-answer', 'question_page');

        );

        Post as a guest






























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        0
        down vote














        Jelly, 8 bytes



        ,’$c’}P:


        A dyadic Link accepting n on the left and k on the right which yields the count.



        Try it online!






        share|improve this answer
























          up vote
          0
          down vote














          Jelly, 8 bytes



          ,’$c’}P:


          A dyadic Link accepting n on the left and k on the right which yields the count.



          Try it online!






          share|improve this answer






















            up vote
            0
            down vote










            up vote
            0
            down vote










            Jelly, 8 bytes



            ,’$c’}P:


            A dyadic Link accepting n on the left and k on the right which yields the count.



            Try it online!






            share|improve this answer













            Jelly, 8 bytes



            ,’$c’}P:


            A dyadic Link accepting n on the left and k on the right which yields the count.



            Try it online!







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 42 mins ago









            Jonathan Allan

            48.6k534160




            48.6k534160




















                up vote
                0
                down vote













                APL(Dyalog), 19 18 16 bytes



                ⍺÷⍨×/(⍵,⍵-1)!⍺


                Uses the identity in the OEIS sequence. Takes n on the left and k on the right.



                TIO






                share|improve this answer
























                  up vote
                  0
                  down vote













                  APL(Dyalog), 19 18 16 bytes



                  ⍺÷⍨×/(⍵,⍵-1)!⍺


                  Uses the identity in the OEIS sequence. Takes n on the left and k on the right.



                  TIO






                  share|improve this answer






















                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    APL(Dyalog), 19 18 16 bytes



                    ⍺÷⍨×/(⍵,⍵-1)!⍺


                    Uses the identity in the OEIS sequence. Takes n on the left and k on the right.



                    TIO






                    share|improve this answer












                    APL(Dyalog), 19 18 16 bytes



                    ⍺÷⍨×/(⍵,⍵-1)!⍺


                    Uses the identity in the OEIS sequence. Takes n on the left and k on the right.



                    TIO







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 30 mins ago









                    Quintec

                    663315




                    663315




















                        up vote
                        0
                        down vote














                        Jelly, 7 bytes



                        cⱮṫ-P÷⁸


                        Try it online!



                        Takes input as n then k. Uses the formula



                        $N(n,k)=frac1nbinomnkbinomnk-1$



                        which I found on Wikipedia.



                        cⱮṫ-P÷⁸
                        c Binomial coefficient of n and...
                        â±® each of 1..k
                        ṫ- Keep the last two. ṫ is tail, - is -1.
                        P Product of the two numbers.
                        ÷ Divide by
                        ⁸ n.


                        7 bytes



                        Each line works on it's own.



                        ,’$c@P÷
                        c@€ṫ-P÷


                        Takes input as k then n.






                        share|improve this answer






















                        • Wait... tail is automatically defined as 2 numbers? (Don't know Jelly at all, just a silly question)
                          – Quintec
                          20 mins ago










                        • @Quintec There are two tail functions. One (Ṫ) that just takes the last element of a single argument and the one I used (ṫ) which takes two arguments. The fist argument is a list and the second one is a number (In my case -1 represented by a - in the code) which tells you how many elements to save. Having -1 give two elements was the golfiest way to define ṫ
                          – dylnan
                          15 mins ago










                        • Gotcha, thanks! I see how jelly was built for golf... hehe
                          – Quintec
                          12 mins ago














                        up vote
                        0
                        down vote














                        Jelly, 7 bytes



                        cⱮṫ-P÷⁸


                        Try it online!



                        Takes input as n then k. Uses the formula



                        $N(n,k)=frac1nbinomnkbinomnk-1$



                        which I found on Wikipedia.



                        cⱮṫ-P÷⁸
                        c Binomial coefficient of n and...
                        â±® each of 1..k
                        ṫ- Keep the last two. ṫ is tail, - is -1.
                        P Product of the two numbers.
                        ÷ Divide by
                        ⁸ n.


                        7 bytes



                        Each line works on it's own.



                        ,’$c@P÷
                        c@€ṫ-P÷


                        Takes input as k then n.






                        share|improve this answer






















                        • Wait... tail is automatically defined as 2 numbers? (Don't know Jelly at all, just a silly question)
                          – Quintec
                          20 mins ago










                        • @Quintec There are two tail functions. One (Ṫ) that just takes the last element of a single argument and the one I used (ṫ) which takes two arguments. The fist argument is a list and the second one is a number (In my case -1 represented by a - in the code) which tells you how many elements to save. Having -1 give two elements was the golfiest way to define ṫ
                          – dylnan
                          15 mins ago










                        • Gotcha, thanks! I see how jelly was built for golf... hehe
                          – Quintec
                          12 mins ago












                        up vote
                        0
                        down vote










                        up vote
                        0
                        down vote










                        Jelly, 7 bytes



                        cⱮṫ-P÷⁸


                        Try it online!



                        Takes input as n then k. Uses the formula



                        $N(n,k)=frac1nbinomnkbinomnk-1$



                        which I found on Wikipedia.



                        cⱮṫ-P÷⁸
                        c Binomial coefficient of n and...
                        â±® each of 1..k
                        ṫ- Keep the last two. ṫ is tail, - is -1.
                        P Product of the two numbers.
                        ÷ Divide by
                        ⁸ n.


                        7 bytes



                        Each line works on it's own.



                        ,’$c@P÷
                        c@€ṫ-P÷


                        Takes input as k then n.






                        share|improve this answer















                        Jelly, 7 bytes



                        cⱮṫ-P÷⁸


                        Try it online!



                        Takes input as n then k. Uses the formula



                        $N(n,k)=frac1nbinomnkbinomnk-1$



                        which I found on Wikipedia.



                        cⱮṫ-P÷⁸
                        c Binomial coefficient of n and...
                        â±® each of 1..k
                        ṫ- Keep the last two. ṫ is tail, - is -1.
                        P Product of the two numbers.
                        ÷ Divide by
                        ⁸ n.


                        7 bytes



                        Each line works on it's own.



                        ,’$c@P÷
                        c@€ṫ-P÷


                        Takes input as k then n.







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited 21 mins ago

























                        answered 40 mins ago









                        dylnan

                        3,8482528




                        3,8482528











                        • Wait... tail is automatically defined as 2 numbers? (Don't know Jelly at all, just a silly question)
                          – Quintec
                          20 mins ago










                        • @Quintec There are two tail functions. One (Ṫ) that just takes the last element of a single argument and the one I used (ṫ) which takes two arguments. The fist argument is a list and the second one is a number (In my case -1 represented by a - in the code) which tells you how many elements to save. Having -1 give two elements was the golfiest way to define ṫ
                          – dylnan
                          15 mins ago










                        • Gotcha, thanks! I see how jelly was built for golf... hehe
                          – Quintec
                          12 mins ago
















                        • Wait... tail is automatically defined as 2 numbers? (Don't know Jelly at all, just a silly question)
                          – Quintec
                          20 mins ago










                        • @Quintec There are two tail functions. One (Ṫ) that just takes the last element of a single argument and the one I used (ṫ) which takes two arguments. The fist argument is a list and the second one is a number (In my case -1 represented by a - in the code) which tells you how many elements to save. Having -1 give two elements was the golfiest way to define ṫ
                          – dylnan
                          15 mins ago










                        • Gotcha, thanks! I see how jelly was built for golf... hehe
                          – Quintec
                          12 mins ago















                        Wait... tail is automatically defined as 2 numbers? (Don't know Jelly at all, just a silly question)
                        – Quintec
                        20 mins ago




                        Wait... tail is automatically defined as 2 numbers? (Don't know Jelly at all, just a silly question)
                        – Quintec
                        20 mins ago












                        @Quintec There are two tail functions. One (Ṫ) that just takes the last element of a single argument and the one I used (ṫ) which takes two arguments. The fist argument is a list and the second one is a number (In my case -1 represented by a - in the code) which tells you how many elements to save. Having -1 give two elements was the golfiest way to define ṫ
                        – dylnan
                        15 mins ago




                        @Quintec There are two tail functions. One (Ṫ) that just takes the last element of a single argument and the one I used (ṫ) which takes two arguments. The fist argument is a list and the second one is a number (In my case -1 represented by a - in the code) which tells you how many elements to save. Having -1 give two elements was the golfiest way to define ṫ
                        – dylnan
                        15 mins ago












                        Gotcha, thanks! I see how jelly was built for golf... hehe
                        – Quintec
                        12 mins ago




                        Gotcha, thanks! I see how jelly was built for golf... hehe
                        – Quintec
                        12 mins ago










                        up vote
                        0
                        down vote













                        JavaScript (ES7), 59 58 49 bytes



                        Takes input as (n)(k).





                        n=>k=>(g=k=>k?m--*g(k-1)/k:1)(k,m=n)*g(k-1,m=n)/n


                        Try it online!



                        Computes:



                        $$a_n,k=frac1kbinomn-1k-1binomnk-1=frac1nbinomnkbinomnk-1$$



                        This formula comes from A001263, the OEIS sequence that xnor referred to in the challenge comments.






                        share|improve this answer


























                          up vote
                          0
                          down vote













                          JavaScript (ES7), 59 58 49 bytes



                          Takes input as (n)(k).





                          n=>k=>(g=k=>k?m--*g(k-1)/k:1)(k,m=n)*g(k-1,m=n)/n


                          Try it online!



                          Computes:



                          $$a_n,k=frac1kbinomn-1k-1binomnk-1=frac1nbinomnkbinomnk-1$$



                          This formula comes from A001263, the OEIS sequence that xnor referred to in the challenge comments.






                          share|improve this answer
























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            JavaScript (ES7), 59 58 49 bytes



                            Takes input as (n)(k).





                            n=>k=>(g=k=>k?m--*g(k-1)/k:1)(k,m=n)*g(k-1,m=n)/n


                            Try it online!



                            Computes:



                            $$a_n,k=frac1kbinomn-1k-1binomnk-1=frac1nbinomnkbinomnk-1$$



                            This formula comes from A001263, the OEIS sequence that xnor referred to in the challenge comments.






                            share|improve this answer














                            JavaScript (ES7), 59 58 49 bytes



                            Takes input as (n)(k).





                            n=>k=>(g=k=>k?m--*g(k-1)/k:1)(k,m=n)*g(k-1,m=n)/n


                            Try it online!



                            Computes:



                            $$a_n,k=frac1kbinomn-1k-1binomnk-1=frac1nbinomnkbinomnk-1$$



                            This formula comes from A001263, the OEIS sequence that xnor referred to in the challenge comments.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited 21 mins ago

























                            answered 41 mins ago









                            Arnauld

                            65.5k582277




                            65.5k582277




















                                combinationsD is a new contributor. Be nice, and check out our Code of Conduct.









                                 

                                draft saved


                                draft discarded


















                                combinationsD is a new contributor. Be nice, and check out our Code of Conduct.












                                combinationsD is a new contributor. Be nice, and check out our Code of Conduct.











                                combinationsD is a new contributor. Be nice, and check out our Code of Conduct.













                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f173081%2fdifferent-combinations-possible%23new-answer', 'question_page');

                                );

                                Post as a guest













































































                                Comments

                                Popular posts from this blog

                                Long meetings (6-7 hours a day): Being “babysat” by supervisor

                                Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                                Confectionery