Different combinations possible
Clash 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
code-golf combinatorics recursion
New contributor
add a comment |Â
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
code-golf combinatorics recursion
New contributor
2
Is this the same as, "find the number of expressions ofn
matched parentheses pairs that contain exactlyk
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
add a comment |Â
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
code-golf combinatorics recursion
New contributor
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
code-golf combinatorics recursion
New contributor
New contributor
edited 1 hour ago
dylnan
3,8482528
3,8482528
New contributor
asked 2 hours ago
combinationsD
212
212
New contributor
New contributor
2
Is this the same as, "find the number of expressions ofn
matched parentheses pairs that contain exactlyk
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
add a comment |Â
2
Is this the same as, "find the number of expressions ofn
matched parentheses pairs that contain exactlyk
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
add a comment |Â
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!
add a comment |Â
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
add a comment |Â
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
.
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
add a comment |Â
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.
add a comment |Â
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!
add a comment |Â
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!
add a comment |Â
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!
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!
answered 42 mins ago
Jonathan Allan
48.6k534160
48.6k534160
add a comment |Â
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
answered 30 mins ago
Quintec
663315
663315
add a comment |Â
add a comment |Â
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
.
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
add a comment |Â
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
.
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
add a comment |Â
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
.
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
.
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
add a comment |Â
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
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
edited 21 mins ago
answered 41 mins ago
Arnauld
65.5k582277
65.5k582277
add a comment |Â
add a comment |Â
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.
combinationsD is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
Is this the same as, "find the number of expressions of
n
matched parentheses pairs that contain exactlyk
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