#JavaScript (ES6), 71 bytes
Inspired by xnor's answer .
A shorter version which gives a higher priority to small terms in the output.
Takes input as (s)(x).
s=>g=(x,a=[],y=0,n=1)=>x<y?x|a[s]?0:a:g(x,a,y+n,n+s-2)||g(x-y,[y,...a])
#JavaScript (ES6), (削除) 83 (削除ここまで) 80 bytes
#JavaScript (ES6), 71 bytes
Inspired by xnor's answer .
A shorter version which gives a higher priority to small terms in the output.
Takes input as (s)(x).
s=>g=(x,a=[],y=0,n=1)=>x<y?x|a[s]?0:a:g(x,a,y+n,n+s-2)||g(x-y,[y,...a])
#JavaScript (ES6), (削除) 83 (削除ここまで) 80 bytes
#JavaScript (ES6), (削除) 83 (削除ここまで) 80 bytes
#JavaScript (ES6), 71 bytes
Inspired by xnor's answer .
A shorter version which gives a higher priority to small terms in the output.
Takes input as (s)(x).
s=>g=(x,a=[],y=0,n=1)=>x<y?x|a[s]?0:a:g(x,a,y+n,n+s-2)||g(x-y,[y,...a])
#JavaScript (ES6), (削除) 83 (削除ここまで) 80 bytes
#JavaScript (ES6), (削除) 83 (削除ここまで) 80 bytes
#JavaScript (ES6), 71 bytes
Inspired by xnor's answer .
A shorter version which gives a higher priority to small terms in the output.
Takes input as (s)(x).
s=>g=(x,a=[],y=0,n=1)=>x<y?x|a[s]?0:a:g(x,a,y+n,n+s-2)||g(x-y,[y,...a])
#JavaScript (ES6), (削除) 83 (削除ここまで) 80 bytes
It turns out to be shorter to use a 0-indexedbased formula to compute the \$n\$\$s\$-gonal numbers in JS, i.e. to start with \$n=0\$ and to compute \$P(n+1,s)\$:
It turns out to be shorter to use a 0-indexed formula to compute the \$n\$-gonal numbers in JS, i.e. to start with \$n=0\$ and to compute \$P(n+1,s)\$:
It turns out to be shorter to use a 0-based formula to compute the \$s\$-gonal numbers in JS, i.e. to start with \$n=0\$ and to compute \$P(n+1,s)\$: