3 ms·
I think of it like a coin counting machine.
by techbio 8y ago
I think of it like a coin counting machine.
- ColinWright 8y agoI don't see how that helps - can you elaborate?
- techbio 8y ago// usage: node coin_counter.js <p(k)> <k> const args = process.argv.slice(2); let p_of_x = Number(args[0]); // p(k) let x = Number(args[1]); // k const coins = {0:1}; let coin_ndx = 0; // compute the values of coins to add to purse total // exit loop when coin face value is too large to subtract from purse for (let n = 0; Math.pow(x, n) <= p_of_x; n++,coin_ndx++) { coins[coin_ndx] = Math.pow(x, n); // compute coin face values } console.log("Show face values of coins:"); let keys = Object.keys(coins); keys.sort(); // find the largest exponent (max_key) let max_key = 0; for (let key in keys) { console.log(key, coins[key]); max_key = key; // use current largest } console.log("Show polynomial"); let purse = p_of_x; // purse is sum of coins for (max_key; max_key >= 0; max_key--) { let count = 0; while (coins[max_key] <= purse) { purse -= coins[max_key]; count++; } console.log("+" + count + " x**" + max_key); } >node coin_counting.js 12028 7 Show face values of coins: 0 1 1 7 2 49 3 343 4 2401 Show polynomial Edit: exercise for the reader
- techbio 8y agoThe face value of each coin is multiplied by the count of the coin type to total to the purse. Or rather, the sum of the nth powers of k multiplied by each positive coefficient equals the value of the function at k. Edit: Source in sibling.
- ColinWright 8y agoWow, what an interesting approach! Thank you ... I've copied that out to a file and I'll study it more later. Cheers!
- techbio 8y agoCheers to you. Let me know how close it was to what you were looking for. https://github.com/techbio/CoinCounting https://github.com/techbio/CoinCounting
- ColinWright 8y agoWhat you've done is not at all close to the solutions I already have, so it's exactly what I was looking for. On the one hand it's very much more complex, but on the other hand it's a more generic and general solution. The solutions I have are elsewhere in this thread, sometimes in disguise, and I'm gestating a blog post which I'll eventually submit. If you want to know more feel free to email me - address is in my profile. Added in edit: I would add that your code gives no hint as to why the solution should be unique.
- techbio 8y agoIt is not going to handle a vast range of inputs, including for example, p(k) = 665, for the basic reason that my code is a direct response to your request for an explanation and not proposed as a complete solution for the request of your original submission. Corrections requested, please. I would like to explore this paradigm to accurately address the problem in an exact way.
- ColinWright 8y agoI'll reply to your email ... thanks.