Skip to main content
added the 'How?' section
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 649

#JavaScript (ES6), 49 bytes

f=(a,kn)=>a.some((nv,ik)=>n<4*k*k=>v<4*n*n-~-i*kk*n)?~k~n:f(a,~-kn)

Try it online!

###How?

The number \$P_{(n,k)}\$ of color particles required to generate \$n\$ times the \$k\$-th color is:

$$P_{(n,k)}=n(4n+(k-1))=4n^2+(k-1)n$$

We recursively try all values of \$n\$ until at least one entry \$v_k\$ in the input array is lower than \$P_{(n,k)}\$.

But for golfing purposes, we start with n === undefined and use negative values of n afterwards. The first iteration is always successful because the right side of the inequality evaluates to NaN. Therefore, the first meaningful test is the 2nd one with n == -1.

#JavaScript (ES6), 49 bytes

f=(a,k)=>a.some((n,i)=>n<4*k*k-~-i*k)?~k:f(a,~-k)

Try it online!

#JavaScript (ES6), 49 bytes

f=(a,n)=>a.some((v,k)=>v<4*n*n-~-k*n)?~n:f(a,~-n)

Try it online!

###How?

The number \$P_{(n,k)}\$ of color particles required to generate \$n\$ times the \$k\$-th color is:

$$P_{(n,k)}=n(4n+(k-1))=4n^2+(k-1)n$$

We recursively try all values of \$n\$ until at least one entry \$v_k\$ in the input array is lower than \$P_{(n,k)}\$.

But for golfing purposes, we start with n === undefined and use negative values of n afterwards. The first iteration is always successful because the right side of the inequality evaluates to NaN. Therefore, the first meaningful test is the 2nd one with n == -1.

saved 2 bytes
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 649

#JavaScript (ES6), 5149 bytes

f=(a,k=0k)=>a.some((n,i)=>n<4*k*k-~-i*k)?~k:f(a,k~-1k)

Try it online!Try it online!

#JavaScript (ES6), 51 bytes

f=(a,k=0)=>a.some((n,i)=>n<4*k*k-~-i*k)?~k:f(a,k-1)

Try it online!

#JavaScript (ES6), 49 bytes

f=(a,k)=>a.some((n,i)=>n<4*k*k-~-i*k)?~k:f(a,~-k)

Try it online!

Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 649

#JavaScript (ES6), 51 bytes

f=(a,k=0)=>a.some((n,i)=>n<4*k*k-~-i*k)?~k:f(a,k-1)

Try it online!