
    
        
        
        
            
{"version":"https:\/\/jsonfeed.org\/version\/1","title":"mathspp.com feed","home_page_url":"https:\/\/mathspp.com\/blog\/tags\/polynomials","feed_url":"https:\/\/mathspp.com\/blog\/tags\/polynomials.json","description":"Stay up-to-date with the articles on mathematics and programming that get published to mathspp.com.","author":{"name":"Rodrigo Gir\u00e3o Serr\u00e3o"},"items":[{"title":"Problem #023 - guess the polynomial","date_published":"2020-11-15T00:00:00+01:00","id":"https:\/\/mathspp.com\/blog\/problems\/guess-the-polynomial","url":"https:\/\/mathspp.com\/blog\/problems\/guess-the-polynomial","content_html":"<p>In this problem you have to devise a strategy to beat the computer in a \"guess the polynomial\" game.<\/p>\n\n<script>\n    var max_degree = 3;\n    var max_coef = 3;\n    var poly_times = 0;\n    var evaluated_at = [];\n\n    \/\/ Generate a random integer between a and b, inclusive.\n    randint = function(a, b) {\n        return Math.floor(Math.random()*(1+b-a)) + a;\n    }\n\n    reset_poly = function() {\n        poly_times = 0;\n        evaluated_at = [];\n        document.getElementById(\"polyHint\").innerHTML = \"\";\n        document.getElementById(\"polyTimes\").innerHTML = 0;\n        document.getElementById(\"polyResult\").innerHTML = \"\";\n        reset_test_coefs();\n        set_disables(guessing = true);\n    }\n\n    \/* set the disabled status of inputs and buttons, depending on whether\n     * the user is currently guessing the poly or not. *\/\n    set_disables = function(guessing) {\n        document.getElementById(\"newPolyBtn\").disabled = guessing;\n        document.getElementById(\"verifyPolyBtn\").disabled = !guessing;\n        document.getElementById(\"giveUpPolyBtn\").disabled = !guessing;\n        set_disabled_test_coefs(disabled = !guessing);\n    }\n\n    reset_test_coefs = function() {\n        for (var i = 0; i <= max_degree; ++i) {\n            document.getElementById(`c${i}`).value = 0;\n        }\n    }\n\n    set_disabled_test_coefs = function(disabled) {\n        for (var i = 0; i <= max_degree; ++i) {\n            document.getElementById(`c${i}`).disabled = disabled;\n        }\n    }\n\n    var poly = new Array(max_degree + 1);\n    generate_poly = function() {\n        for (var i = 0; i <= max_degree; ++i) {\n            poly[i] = randint(0, max_coef);\n        }\n        reset_poly();\n    }\n\n    evaluate_poly = function() {\n        var a = parseInt(document.getElementById(\"polyAt\").value);\n        var value = 0;\n        for (var i = 0; i <= max_degree; ++i) {\n            value += poly[i]*a**i;\n        }\n        document.getElementById(\"polyHint\").innerHTML = `p(${a}) = ${value}`;\n        if (-1 === evaluated_at.indexOf(a)) {\n            evaluated_at.push(a);\n            ++poly_times;\n            document.getElementById(\"polyTimes\").innerHTML = poly_times;\n        }\n    }\n\n    verify_poly = function() {\n        var right = true;\n        for (var i = 0; i <= max_degree; ++i) {\n            right &= document.getElementById(`c${i}`).value === `${poly[i]}`;\n        }\n        if (right) {\n            document.getElementById(\"polyResult\").innerHTML = \"Correct!\";\n            set_disables(guessing = false);\n        } else {\n            document.getElementById(\"polyResult\").innerHTML = \"Wrong!\";\n        }\n    }\n\n    give_up_poly = function() {\n        set_disables(guessing = false);\n        polyResult = `The polynomial was p(n) = ${poly[0]}`\n        for (var i = 1; i<= max_degree; ++i) {\n            polyResult += ` + ${poly[i]}n^${i}`;\n        }\n        document.getElementById(\"polyResult\").innerHTML = polyResult;\n    }\n\n    window.onload = generate_poly;\n<\/script><figure class=\"image-caption\"><img title=\"Photo by Emily Morter on Unsplash\" alt=\"A question mark in a neon light\" src=\"\/images\/6\/e\/e\/9\/7\/6ee97fa1f35461424a54a2d0112230b1a0cc2f18-thumbnail.jpg\"><figcaption class=\"\">Photo by Emily Morter on Unsplash<\/figcaption><\/figure><h2 id=\"problem-statement\">Problem statement<a href=\"#problem-statement\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>I want you to play a little game with the computer.\nThe computer is going to think of a polynomial with non-negative, integer coefficients.\nLet's say <span class=\"mathjax mathjax--inline\">\\(p(n)\\)<\/span> is the secret polynomial the computer is thinking of.<\/p>\n<p>I want you to find out what <span class=\"mathjax mathjax--inline\">\\(p(n)\\)<\/span> is, and the only thing you can do is to ask for hints in the form of values of <span class=\"mathjax mathjax--inline\">\\(p(n)\\)<\/span> for <span class=\"mathjax mathjax--inline\">\\(n \\geq 0\\)<\/span> integer.\nFor example, you can ask what <span class=\"mathjax mathjax--inline\">\\(p(0)\\)<\/span> or <span class=\"mathjax mathjax--inline\">\\(p(49)\\)<\/span> is, but you can't ask for the value of <span class=\"mathjax mathjax--inline\">\\(p(-1)\\)<\/span> or <span class=\"mathjax mathjax--inline\">\\(p(0.5)\\)<\/span>.\nYou have to come up with the best strategy possible, that allows you to determine <span class=\"mathjax mathjax--inline\">\\(p(n)\\)<\/span> with the least number of hints possible.<\/p>\n<p>You can test your strategy with the computer below.\nThe computer will only think of polynomials with degree at most <span class=\"mathjax mathjax--inline\">\\(3\\)<\/span>\nand the coefficients will be at most <span class=\"mathjax mathjax--inline\">\\(3\\)<\/span> as well, but that is just to make testing your strategy easier.\nThe strategy should work for higher degrees and larger coefficients.<\/p>\n<p>With the restrictions for the computer test, we have<\/p>\n<p class=\"mathjax mathjax--block\">\\[\np(n) = c_0 + c_1n + c_2n^2 + c_3n^3, 0 \\leq...<\/p>","summary":"In this problem you have to beat the computer in a guessing game.","date_modified":"2025-07-23T16:49:02+02:00","tags":["mathematics","polynomials","number theory","game"],"image":"\/user\/pages\/02.blog\/03.problems\/p023-guess-the-polynomial\/thumbnail.jpg"}]}
