Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

Rollback to Revision 8
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

#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])

Try it online!


#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])

Try it online!


#JavaScript (ES6), (削除) 83 (削除ここまで) 80 bytes

#JavaScript (ES6), (削除) 83 (削除ここまで) 80 bytes

added a shorter version
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

#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])

Try it online!


#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])

Try it online!


#JavaScript (ES6), (削除) 83 (削除ここまで) 80 bytes

typo
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

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)\$:

minor update
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
added a short description of the algorithm
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
minor update
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
added an explanation of the formula used
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
added a commented version
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
saved 3 bytes
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading

AltStyle によって変換されたページ (->オリジナル) /