Polynomial Arrays

A while ago a friend and I were looking at some JS functions and found out how crazy powerful Array.flat() is. For those that don't know, Array.flat() is a method that creates a new array with all sub-array elements concatenated into it recursively up to the specified depth. Now you might be thinking "specified depth? What could that possibly be good for?" Well that's what I thought as well, until I realized how powerful it is. For example look at the beautiful function that computes Fibonacci numbers:

function fibonacci(n) { let arr = [[0],0]; arr[0][0]=arr[1]=arr; arr=[[arr]]; return arr.flat(n).length; }

So simple and elegant! No crazy control flow patterns like recursion, not even a loop, just a single call to flat. I wonder what else this function can compute? I happen to be a physics student which means I have learned that if you wave your hands while saying "Taylor" every function turns into a Polynomial. So let's write a function that takes 3 positive integers a,b,c and returns the Array corresponding to ax^2+bx+c. In other words the function should be used like this:

function get_array(a,b,c) { // Your code goes here } let arr = get_array(1,1,1); let polynomial = (n) => arr.flat(n).length; console.log(polynomial(2)); // 7

Assume that a,b,c, and n are all non-zero positive integers.

Solution:

function get_array(a,b,c) { }

Yo this submit button uses eval for obvious reasons, please don't copy-paste code you don't trust



Need a hint? Click below:

Think about this array as a directed graph. If you assign each node an integer, what does a single call to flat do?

More hints? Click below:

Try constructing arrays that have length 1,n,n^2 after n flat calls first. Then you can use those to construct the final array.

Final hint? Click below:

What length does an array that contains only itself have after n flat calls? How can you make it grow?