{"group":{"id":1,"name":"Community","lockable":false,"created_at":"2012-01-18T18:02:15.000Z","updated_at":"2026-05-26T00:16:20.000Z","description":"Problems submitted by members of the MATLAB Central community.","is_default":true,"created_by":161519,"badge_id":null,"featured":false,"trending":false,"solution_count_in_trending_period":0,"trending_last_calculated":"2026-05-26T00:00:00.000Z","image_id":null,"published":true,"community_created":false,"status_id":2,"is_default_group_for_player":false,"deleted_by":null,"deleted_at":null,"restored_by":null,"restored_at":null,"description_opc":null,"description_html":null,"published_at":null},"problems":[{"id":61011,"title":"Compute the tersum","description":"The tersum, which appears in certain mathematical games, involves adding two numbers in base 3 without carry and converting back to base 10. For example, the tersum of 5 and 8 would involve adding their base-3 representations 12 and 22 without carry to get 01 in base 3 or 1 in base 10. \r\nWrite a function to compute the tersum of two integers.","description_html":"\u003cdiv style = \"text-align: start; line-height: 20.44px; min-height: 0px; white-space: normal; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, monospace; font-style: normal; font-size: 14px; font-weight: 400; text-decoration: none solid rgb(0, 0, 0); white-space: normal; \"\u003e\u003cdiv style=\"block-size: 93px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 408px 46.5px; transform-origin: 408px 46.5px; vertical-align: baseline; \"\u003e\u003cdiv style=\"block-size: 63px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 385px 31.5px; text-align: left; transform-origin: 385px 31.5px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eThe tersum, which appears in certain mathematical games, involves adding two numbers in base 3 without carry and converting back to base 10. For example, the tersum of 5 and 8 would involve adding their base-3 representations 12 and 22 without carry to get 01 in base 3 or 1 in base 10. \u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 385px 10.5px; text-align: left; transform-origin: 385px 10.5px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eWrite a function to compute the tersum of two integers.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function y = tersum(m,n)\r\n  y = mod(m+n,3);\r\nend","test_suite":"%%\r\nm = 5;\r\nn = 8;\r\ny = tersum(m,n);\r\ny_correct = 1;\r\nassert(isequal(y,y_correct))\r\n\r\n%%\r\nm = 1;\r\nn = [0 1 2 15 23 30 54 78];\r\ny = arrayfun(@(k) tersum(m,k),n);\r\ny_correct = [1 2 0 16 21 31 55 79];\r\nassert(isequal(y,y_correct))\r\n\r\n%% \r\nm = 12;\r\nn = [14 91 167 452 8192 15663];\r\ny = arrayfun(@(k) tersum(m,k),n);\r\ny_correct = [26 103 179 437 8204 15675];\r\nassert(isequal(y,y_correct))\r\n\r\n%% \r\nm = 20;\r\nn = [65 288 510 809 3033 6057 9011 17777];\r\ny = arrayfun(@(k) tersum(m,k),n);\r\ny_correct = [55 281 503 799 3026 6050 9001 17767];\r\nassert(isequal(y,y_correct))\r\n\r\n%%\r\nm = 353;\r\nn = 829;\r\ny = tersum(m,n);\r\ny_correct = 1179;\r\nassert(isequal(y,y_correct))\r\n\r\n%%\r\nm = 4221;\r\nn = 7803;\r\ny = tersum(m,n);\r\ny_correct = 9108;\r\nassert(isequal(y,y_correct))\r\n\r\n%%\r\nA = [1 -2 4; 1 4 -2];\r\nr = randi(300);\r\nk = randi(2);\r\nm = 3*r+k;\r\nfor n = 1:60\r\n    a(n) = tersum(m,n);\r\nend\r\nassert(~isempty(strfind(diff(a),A(k,:))))","published":true,"deleted":false,"likes_count":0,"comments_count":0,"created_by":46909,"edited_by":46909,"edited_at":"2025-10-04T16:59:19.000Z","deleted_by":null,"deleted_at":null,"solvers_count":10,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2025-10-04T16:59:08.000Z","updated_at":"2026-04-23T13:16:10.000Z","published_at":"2025-10-04T16:59:19.000Z","restored_at":null,"restored_by":null,"spam":null,"simulink":false,"admin_reviewed":false,"description_opc":"{\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe tersum, which appears in certain mathematical games, involves adding two numbers in base 3 without carry and converting back to base 10. For example, the tersum of 5 and 8 would involve adding their base-3 representations 12 and 22 without carry to get 01 in base 3 or 1 in base 10. \u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eWrite a function to compute the tersum of two integers.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\",\"relationship\":null}],\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"target\":\"/matlab/document.xml\",\"relationshipId\":\"rId1\"}]}"},{"id":61008,"title":"Identify winnable games of Take a Square","description":"A game has a pile of tokens, and two players alternate in taking a certain number of tokens from the pile, with the constraint that the number has to be a non-zero perfect square. A player loses if no tokens remain to be taken. \r\nFor example, if the game starts with 4 tokens in the pile, then Player 1 can win by taking all of them. If the game starts with 14 tokens, Player 1 can win by taking 9, leaving 5. Player 2 must then take either 1 or 4. Either way, Player 1 wins by taking all of the remaining tokens. \r\nWrite a function that takes the initial number of tokens in the pile and returns true if Player 1 can force a win by playing optimally. ","description_html":"\u003cdiv style = \"text-align: start; line-height: 20.44px; min-height: 0px; white-space: normal; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, monospace; font-style: normal; font-size: 14px; font-weight: 400; text-decoration: none solid rgb(0, 0, 0); white-space: normal; \"\u003e\u003cdiv style=\"block-size: 165px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 408px 82.5px; transform-origin: 408px 82.5px; vertical-align: baseline; \"\u003e\u003cdiv style=\"block-size: 42px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 385px 21px; text-align: left; transform-origin: 385px 21px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eA game has a pile of tokens, and two players alternate in taking a certain number of tokens from the pile, with the constraint that the number has to be a non-zero perfect square. A player loses if no tokens remain to be taken. \u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 63px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 385px 31.5px; text-align: left; transform-origin: 385px 31.5px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eFor example, if the game starts with 4 tokens in the pile, then Player 1 can win by taking all of them. If the game starts with 14 tokens, Player 1 can win by taking 9, leaving 5. Player 2 must then take either 1 or 4. Either way, Player 1 wins by taking all of the remaining tokens. \u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 42px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 385px 21px; text-align: left; transform-origin: 385px 21px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eWrite a function that takes the initial number of tokens in the pile and returns true if Player 1 can force a win by playing optimally. \u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function tf = winTakeASquare(n)\r\n  tf = false;\r\nend","test_suite":"%% \r\nassert(winTakeASquare(4))\r\n\r\n%%\r\nassert(~winTakeASquare(7))\r\n\r\n%%\r\nassert(winTakeASquare(14))\r\n\r\n%%\r\nassert(winTakeASquare(43))\r\n\r\n%% \r\nassert(winTakeASquare(259))\r\n\r\n%%\r\nassert(~winTakeASquare(865))\r\n\r\n%%\r\nassert(~winTakeASquare(2254))\r\n\r\n%%\r\nassert(winTakeASquare(7777))\r\n\r\n%% \r\nassert(winTakeASquare(32568))\r\n\r\n%%\r\nassert(~winTakeASquare(421345))\r\n\r\n%%\r\nassert(winTakeASquare(1004341))\r\n\r\n%%\r\ns = randi(8000)^2;\r\nassert(winTakeASquare(s))","published":true,"deleted":false,"likes_count":2,"comments_count":3,"created_by":46909,"edited_by":46909,"edited_at":"2025-09-21T22:03:26.000Z","deleted_by":null,"deleted_at":null,"solvers_count":6,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2025-09-21T13:24:51.000Z","updated_at":"2026-04-24T11:11:11.000Z","published_at":"2025-09-21T13:25:04.000Z","restored_at":null,"restored_by":null,"spam":null,"simulink":false,"admin_reviewed":false,"description_opc":"{\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eA game has a pile of tokens, and two players alternate in taking a certain number of tokens from the pile, with the constraint that the number has to be a non-zero perfect square. A player loses if no tokens remain to be taken. \u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eFor example, if the game starts with 4 tokens in the pile, then Player 1 can win by taking all of them. If the game starts with 14 tokens, Player 1 can win by taking 9, leaving 5. Player 2 must then take either 1 or 4. Either way, Player 1 wins by taking all of the remaining tokens. \u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eWrite a function that takes the initial number of tokens in the pile and returns true if Player 1 can force a win by playing optimally. \u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\",\"relationship\":null}],\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"target\":\"/matlab/document.xml\",\"relationshipId\":\"rId1\"}]}"}],"problem_search":{"problems":[{"id":61011,"title":"Compute the tersum","description":"The tersum, which appears in certain mathematical games, involves adding two numbers in base 3 without carry and converting back to base 10. For example, the tersum of 5 and 8 would involve adding their base-3 representations 12 and 22 without carry to get 01 in base 3 or 1 in base 10. \r\nWrite a function to compute the tersum of two integers.","description_html":"\u003cdiv style = \"text-align: start; line-height: 20.44px; min-height: 0px; white-space: normal; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, monospace; font-style: normal; font-size: 14px; font-weight: 400; text-decoration: none solid rgb(0, 0, 0); white-space: normal; \"\u003e\u003cdiv style=\"block-size: 93px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 408px 46.5px; transform-origin: 408px 46.5px; vertical-align: baseline; \"\u003e\u003cdiv style=\"block-size: 63px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 385px 31.5px; text-align: left; transform-origin: 385px 31.5px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eThe tersum, which appears in certain mathematical games, involves adding two numbers in base 3 without carry and converting back to base 10. For example, the tersum of 5 and 8 would involve adding their base-3 representations 12 and 22 without carry to get 01 in base 3 or 1 in base 10. \u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 385px 10.5px; text-align: left; transform-origin: 385px 10.5px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eWrite a function to compute the tersum of two integers.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function y = tersum(m,n)\r\n  y = mod(m+n,3);\r\nend","test_suite":"%%\r\nm = 5;\r\nn = 8;\r\ny = tersum(m,n);\r\ny_correct = 1;\r\nassert(isequal(y,y_correct))\r\n\r\n%%\r\nm = 1;\r\nn = [0 1 2 15 23 30 54 78];\r\ny = arrayfun(@(k) tersum(m,k),n);\r\ny_correct = [1 2 0 16 21 31 55 79];\r\nassert(isequal(y,y_correct))\r\n\r\n%% \r\nm = 12;\r\nn = [14 91 167 452 8192 15663];\r\ny = arrayfun(@(k) tersum(m,k),n);\r\ny_correct = [26 103 179 437 8204 15675];\r\nassert(isequal(y,y_correct))\r\n\r\n%% \r\nm = 20;\r\nn = [65 288 510 809 3033 6057 9011 17777];\r\ny = arrayfun(@(k) tersum(m,k),n);\r\ny_correct = [55 281 503 799 3026 6050 9001 17767];\r\nassert(isequal(y,y_correct))\r\n\r\n%%\r\nm = 353;\r\nn = 829;\r\ny = tersum(m,n);\r\ny_correct = 1179;\r\nassert(isequal(y,y_correct))\r\n\r\n%%\r\nm = 4221;\r\nn = 7803;\r\ny = tersum(m,n);\r\ny_correct = 9108;\r\nassert(isequal(y,y_correct))\r\n\r\n%%\r\nA = [1 -2 4; 1 4 -2];\r\nr = randi(300);\r\nk = randi(2);\r\nm = 3*r+k;\r\nfor n = 1:60\r\n    a(n) = tersum(m,n);\r\nend\r\nassert(~isempty(strfind(diff(a),A(k,:))))","published":true,"deleted":false,"likes_count":0,"comments_count":0,"created_by":46909,"edited_by":46909,"edited_at":"2025-10-04T16:59:19.000Z","deleted_by":null,"deleted_at":null,"solvers_count":10,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2025-10-04T16:59:08.000Z","updated_at":"2026-04-23T13:16:10.000Z","published_at":"2025-10-04T16:59:19.000Z","restored_at":null,"restored_by":null,"spam":null,"simulink":false,"admin_reviewed":false,"description_opc":"{\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe tersum, which appears in certain mathematical games, involves adding two numbers in base 3 without carry and converting back to base 10. For example, the tersum of 5 and 8 would involve adding their base-3 representations 12 and 22 without carry to get 01 in base 3 or 1 in base 10. \u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eWrite a function to compute the tersum of two integers.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\",\"relationship\":null}],\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"target\":\"/matlab/document.xml\",\"relationshipId\":\"rId1\"}]}"},{"id":61008,"title":"Identify winnable games of Take a Square","description":"A game has a pile of tokens, and two players alternate in taking a certain number of tokens from the pile, with the constraint that the number has to be a non-zero perfect square. A player loses if no tokens remain to be taken. \r\nFor example, if the game starts with 4 tokens in the pile, then Player 1 can win by taking all of them. If the game starts with 14 tokens, Player 1 can win by taking 9, leaving 5. Player 2 must then take either 1 or 4. Either way, Player 1 wins by taking all of the remaining tokens. \r\nWrite a function that takes the initial number of tokens in the pile and returns true if Player 1 can force a win by playing optimally. ","description_html":"\u003cdiv style = \"text-align: start; line-height: 20.44px; min-height: 0px; white-space: normal; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, monospace; font-style: normal; font-size: 14px; font-weight: 400; text-decoration: none solid rgb(0, 0, 0); white-space: normal; \"\u003e\u003cdiv style=\"block-size: 165px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 408px 82.5px; transform-origin: 408px 82.5px; vertical-align: baseline; \"\u003e\u003cdiv style=\"block-size: 42px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 385px 21px; text-align: left; transform-origin: 385px 21px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eA game has a pile of tokens, and two players alternate in taking a certain number of tokens from the pile, with the constraint that the number has to be a non-zero perfect square. A player loses if no tokens remain to be taken. \u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 63px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 385px 31.5px; text-align: left; transform-origin: 385px 31.5px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eFor example, if the game starts with 4 tokens in the pile, then Player 1 can win by taking all of them. If the game starts with 14 tokens, Player 1 can win by taking 9, leaving 5. Player 2 must then take either 1 or 4. Either way, Player 1 wins by taking all of the remaining tokens. \u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 42px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 385px 21px; text-align: left; transform-origin: 385px 21px; white-space-collapse: preserve; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eWrite a function that takes the initial number of tokens in the pile and returns true if Player 1 can force a win by playing optimally. \u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function tf = winTakeASquare(n)\r\n  tf = false;\r\nend","test_suite":"%% \r\nassert(winTakeASquare(4))\r\n\r\n%%\r\nassert(~winTakeASquare(7))\r\n\r\n%%\r\nassert(winTakeASquare(14))\r\n\r\n%%\r\nassert(winTakeASquare(43))\r\n\r\n%% \r\nassert(winTakeASquare(259))\r\n\r\n%%\r\nassert(~winTakeASquare(865))\r\n\r\n%%\r\nassert(~winTakeASquare(2254))\r\n\r\n%%\r\nassert(winTakeASquare(7777))\r\n\r\n%% \r\nassert(winTakeASquare(32568))\r\n\r\n%%\r\nassert(~winTakeASquare(421345))\r\n\r\n%%\r\nassert(winTakeASquare(1004341))\r\n\r\n%%\r\ns = randi(8000)^2;\r\nassert(winTakeASquare(s))","published":true,"deleted":false,"likes_count":2,"comments_count":3,"created_by":46909,"edited_by":46909,"edited_at":"2025-09-21T22:03:26.000Z","deleted_by":null,"deleted_at":null,"solvers_count":6,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2025-09-21T13:24:51.000Z","updated_at":"2026-04-24T11:11:11.000Z","published_at":"2025-09-21T13:25:04.000Z","restored_at":null,"restored_by":null,"spam":null,"simulink":false,"admin_reviewed":false,"description_opc":"{\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eA game has a pile of tokens, and two players alternate in taking a certain number of tokens from the pile, with the constraint that the number has to be a non-zero perfect square. A player loses if no tokens remain to be taken. \u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eFor example, if the game starts with 4 tokens in the pile, then Player 1 can win by taking all of them. If the game starts with 14 tokens, Player 1 can win by taking 9, leaving 5. Player 2 must then take either 1 or 4. Either way, Player 1 wins by taking all of the remaining tokens. \u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eWrite a function that takes the initial number of tokens in the pile and returns true if Player 1 can force a win by playing optimally. \u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\",\"relationship\":null}],\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"target\":\"/matlab/document.xml\",\"relationshipId\":\"rId1\"}]}"}],"errors":[],"facets":[[],[{"value":"medium","count":2,"selected":false}]],"term":"tag:\"mathematical games\"","page":1,"per_page":50,"sort":"map(difficulty_value,0,0,999) asc"}}