3
func P(_ n:Int,_ k:Int)->Int{return n*k>0 ?P(n-k,k)+P(n-1,k-1):n==k ?1:0}
First of all, we define our function P
, with two integer parameters n
and k
, and give it a return type of Int
, with this piece of code: func P(_ n:Int,_ k:Int)->Int{...}
. The cool trick here is that we tell the compiler to ignore the names of the parameters, with _
followed by a space, which saves us two bytes when we call the function. return
is obviously used to return the result of our nested ternary described below.
Another trick I used was n*k>0
, which saves us a couple of bytes over n>0&&k>0
. If the condition is true (both the integers are still higher than 0
), then we recursively call our function with n
decremented by k
as our new n
and k
stays the same, and we add the result of P()
with n
and k
decremented by 1. If the condition is not true, we return either 1
or 0
depending on whether n
is equal to k
.
We know that the first element of the sequence is p0(0), and so we check that both integers are positive (n*k>0
). If they are not higher than 0, we check if they are equal (n==l ?1:0
), here being two cases:
There is exactly 1 possible partition, and thus we return 1, if the integers are equal.
There are no partitions if one of them is already 0 and the other is not.
However, if both are positive, we recursively call P
twice, adding the results of P(n-k,k)
and P(n-1,k-1)
. And we loop again until n
has reached 0.
* Note: The spaces cannot be removed.
3
-1 byte thanks to Erik the Outgolfer!
/lM./E
Inputs to the program are reversed (i.e. k, n
).
2
Ah ha, of course! – Jonathan Allan – 2017-07-26T14:31:58.967
2
-10 bytes thanks to Erik the Outgolfer. -1 byte thanks to Mr. Xcoder.
I will say, I did copy the hint from OP and translate it to Python. :P
P=lambda n,k:n*k>0and P(n-k,k)+P(n-1,k-1)or+(n==k)
1This breaks on the last test case of 10, 3. Too much recursion – jkelm – 2017-07-26T14:18:12.900
@jkelm Fixed. - – totallyhuman – 2017-07-26T14:19:01.540
1(n>0and k>0)
-> n>0<k
– Erik the Outgolfer – 2017-07-26T14:19:48.300
Heh, nice abuse of Python's comparison operators. I wish there was a way to have the 0 at the end though, so I could get rid of that space... – totallyhuman – 2017-07-26T14:21:32.480
1Also you can do +(n==k)
instead of [0,1][n==k]
. – Erik the Outgolfer – 2017-07-26T14:23:53.317
1You don't need the space in or +
. – Erik the Outgolfer – 2017-07-26T14:26:28.563
"I will say, I did copy the hint from OP and translate it to Python" - I don't feel so bad that my solution ended up being so similar to yours, now! – Shaggy – 2017-07-26T14:36:56.967
150 bytes, replace n>0<k and
with n*k>0and
– Mr. Xcoder – 2017-07-26T14:46:11.723
(also @EriktheOutgolfer) What is the +
in +(n==k)
for exactly? This seems to work for 48.
To cast n==k
to an integer. Technically, True
instead of 1 and False
instead of 0 is allowed but it shaves off 3 bytes and is just... meh. – totallyhuman – 2017-07-26T15:12:15.147
@JonathanAllan +
converts True
/False
to 0
/1
– Erik the Outgolfer – 2017-07-26T15:13:34.457
Does it ever need to be cast though, wont the addition in the pre-tail call always exist, hence casting any boolean results to integers? – Jonathan Allan – 2017-07-26T15:13:40.237
@JonathanAllan Not always. – Erik the Outgolfer – 2017-07-26T15:15:28.280
@EriktheOutgolfer right - if numbers less than 1 are required to be handled I suppose. – Jonathan Allan – 2017-07-26T15:17:10.187
@totallyhuman nothing is "just ...meh" when it comes to shaving off bytes ;p – Jonathan Allan – 2017-07-26T15:19:00.893
2
Length@IntegerPartitions[#,{#2}]&
here is n x k table
\k->
n1 0 0 0 0 0 0 0 0 0
|1 1 0 0 0 0 0 0 0 0
v1 1 1 0 0 0 0 0 0 0
1 2 1 1 0 0 0 0 0 0
1 2 2 1 1 0 0 0 0 0
1 3 3 2 1 1 0 0 0 0
1 3 4 3 2 1 1 0 0 0
1 4 5 5 3 2 1 1 0 0
1 4 7 6 5 3 2 1 1 0
1 5 8 9 7 5 3 2 1 1
1Of course. I knew there was a builtin. – Matthew Roh – 2017-07-26T14:17:51.120
A Mathematica Simplified port would save a 13 bytes here I believe (Length
->Len`1
and IntegerPartitions
-> Int`7
@JonathanAllan I'm actually making a super-short version of this. Hopefully I can finish it by the end of the month – JungHwan Min – 2017-07-26T16:50:32.857
2
y:Z^!S!Xu!Xs=s
Consider inputs 6
, 2
.
y % Implicitly take the two inputs. Duplicate from below
% STACK: 6, 2, 6
: % Range
% STACK: 6, 2, [1 2 3 4 5 6]
Z^ % Cartesian power
% STACK: 6, [1 1; 1 2; ... 1 5; 1 6; 2 1; 2 2; ...; 6 6]
!S! % Sort each row
% STACK: 6, [1 1; 1 2; ... 1 5; 1 6; 1 2; 2 2; ...; 6 6]
Xu % Unique rows
% STACK: 6, [1 1; 1 2; ... 1 5; 1 6; 2 2; ...; 6 6]
!Xs % Sum of each row, as a row vector
% STACK: 6, [2 3 ... 6 7 4 ... 12]
= % Equals, element-wise
% STACK: [0 0 ... 1 0 0 ... 0]
s % Sum. Implicitly display
% STACK: 3
Honestly I'm surprised MATL doesn't have an "integer partition" builtin or something. – Erik the Outgolfer – 2017-07-26T14:25:36.790
@Sanchises It should be working now. Thanks for telling me! – Luis Mendo – 2017-07-26T19:14:44.113
No problem. I was just playing with it to understand your method (which is generally easier for small cases) when I ran into the error. – Sanchises – 2017-07-26T20:22:18.957
2
I think this works.
f=(x,y)=>x*y>0?f(x-y,y)+f(--x,--y):x==y
k.value=(
f=(x,y)=>x*y>0?f(x-y,y)+f(--x,--y):x==y
)(i.value=10,j.value=3)
onchange=_=>k.value=f(+i.value,+j.value)
*{font-family:sans-serif}input{margin:0 5px 0 0;width:100px;}#j,#k{width:50px;}
<label for=i>n: </label><input id=i type=number><label for=j>k: </label><input id=j type=number><label for=k>P<sub>k</sub>(n): </label><input id=k readonly>
1
1
Pari/GP has a built-in for iterating over integer partitions.
n->k->i=0;forpart(x=n,i++,,[k,k]);i
1
Yeah it isn't (see my Jelly answer at the far bottom). – Erik the Outgolfer – 2017-07-26T14:29:22.140
Oh no Code bowling – Matthew Roh – 2017-07-26T14:30:35.880
@EriktheOutgolfer If it works why is it deleted? – Jonathan Allan – 2017-07-26T14:30:36.643
@Jonathan refresh – Matthew Roh – 2017-07-26T14:31:05.773
@JonathanAllan It was deleted before a test case was fixed. – Erik the Outgolfer – 2017-07-26T14:32:28.807
0
0
0
0
f=pryr::f(`if`(k<1|n<1,!n+k,f(n-k,k)+f(n-1,k-1)))
Which evaluates to the recursive function
function (k, n)
if (k < 1 | n < 1) !n + k else f(n - k, k) + f(n - 1, k - 1)
(k < 1 | n < 1)
checks if any of the initial states are met. !n + k
evaluates to TRUE (1) if both n
and k
are 0, and FALSE (0) otherwise. f(n - k, k) + f(n - 1, k - 1)
handles the recursion.
1You should clearly state that this is [tag:code-golf], even though it's tagged. – Mr. Xcoder – 2017-07-26T14:01:00.250
2Can someone link to the OEIS for this (I assume there is one with the number of partition sequences we've been running into on the CnR OEIS post) – Stephen – 2017-07-26T14:03:41.297
3I think the last test case should be 8 – H.PWiz – 2017-07-26T14:12:27.513
@H.PWiz Uhm let me calculate again – Matthew Roh – 2017-07-26T14:13:59.360
Related – miles – 2017-07-26T14:14:36.070
2I agree with H.PWiz that the last testcase should be
8
instead of7
. – Erik the Outgolfer – 2017-07-26T14:19:07.297Looks like It was 8. Sorry for your inconvenience. – Matthew Roh – 2017-07-26T14:22:21.863
2A008284 – miles – 2017-07-26T14:26:28.990
Is restricting the second input,
k
, to non-negative integers is acceptable? Also should the solution work for negativen
? Zeron
? (my guesses are yes, yes, and yes) – Jonathan Allan – 2017-07-26T15:29:59.777If
k
is fixed the generating function for this isx^k/((1-x)*(1-x^2)*...*(1-x^k))
. – orlp – 2017-07-26T15:32:51.260@JonathanAllan I don't see how
k
can be negative, given it's the length of a partition. – Erik the Outgolfer – 2017-07-26T15:37:03.403This needs more detail. Can 0 be a partition element? Can a partition contain duplicates? Do we count only increasing partitions? – Zgarb – 2017-07-26T15:37:46.900
@EriktheOutgolfer there are, by the very definition provided in the OP,
0
partitions fork<=0
. (and my guess to the answer to my question was "yes" too) – Jonathan Allan – 2017-07-26T15:56:52.983@Zgarb it could help to add such information to the post, yes. But to address these questions "an integer partition is a way of writing n as a sum of positive integers. Two sums that differ only in the order of their summands are considered the same partition.", so: no zeros inside; duplicates are acceptable; disregard order for counting. (Note that
P(0,0)
is1
however - one way to have the empty set). – Jonathan Allan – 2017-07-26T16:10:26.443In the tip, if
P0(0)=1
, shouldn't the condition in the other case be<0
instead of≤ 0
? – JAD – 2017-07-27T06:56:38.090