{"group":{"group":{"id":31,"name":"Matrix Patterns II","lockable":false,"created_at":"2017-07-12T15:55:28.000Z","updated_at":"2026-04-06T14:01:22.000Z","description":"Create even more patterned matrices.","is_default":false,"created_by":26769,"badge_id":45,"featured":false,"trending":false,"solution_count_in_trending_period":0,"trending_last_calculated":"2026-04-06T00:00:00.000Z","image_id":410,"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":"{\"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\u003eCreate even more patterned matrices.\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\"}]}","description_html":"\u003cdiv style = \"text-align: start; line-height: normal; 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=\"display: block; min-width: 0px; padding-top: 0px; perspective-origin: 289.5px 10.5px; transform-origin: 289.5px 10.5px; \"\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 2px; text-align: left; white-space: pre-wrap; perspective-origin: 266.5px 10.5px; transform-origin: 266.5px 10.5px; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eCreate even more patterned matrices.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","published_at":"2019-05-08T19:57:07.000Z"},"current_player":null},"problems":[{"id":1636,"title":"ZigZag matrix with reflected format","description":"ZigZag MATRIX with REFLECTED format.\r\n\r\nWe have only input x. We have to create a matrix in the following pattern.\r\n\r\n input n=5; \r\n\r\nthen\r\n\r\n     2     4     6     8    10\r\n     1     3     5     7     9\r\n     1     3     5     7     9\r\n     2     4     6     8    10\r\n\r\nHint: meshgrid(1:n,1:n) \u0026 (2*i)-mod(j,n-1)+1","description_html":"\u003cp\u003eZigZag MATRIX with REFLECTED format.\u003c/p\u003e\u003cp\u003eWe have only input x. We have to create a matrix in the following pattern.\u003c/p\u003e\u003cpre\u003e input n=5; \u003c/pre\u003e\u003cp\u003ethen\u003c/p\u003e\u003cpre\u003e     2     4     6     8    10\r\n     1     3     5     7     9\r\n     1     3     5     7     9\r\n     2     4     6     8    10\u003c/pre\u003e\u003cp\u003eHint: meshgrid(1:n,1:n) \u0026 (2*i)-mod(j,n-1)+1\u003c/p\u003e","function_template":"function y = your_fcn_name(x)\r\n  y = x;\r\nend","test_suite":"%%\r\nx = 5;\r\ny_correct = [     2     4     6     8    10;\r\n     1     3     5     7     9;\r\n     1     3     5     7     9;\r\n     2     4     6     8    10];\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n\r\n%%\r\nx = 7;\r\ny_correct = [   2     4     6     8    10    12    14;\r\n     1     3     5     7     9    11    13;\r\n     1     3     5     7     9    11    13;\r\n     2     4     6     8    10    12    14];\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n\r\n%%\r\nx = 11;\r\ny_correct = [  2     4     6     8    10    12    14    16    18    20    22;\r\n     1     3     5     7     9    11    13    15    17    19    21;\r\n     1     3     5     7     9    11    13    15    17    19    21;\r\n     2     4     6     8    10    12    14    16    18    20    22];\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n","published":true,"deleted":false,"likes_count":5,"comments_count":0,"created_by":13514,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":296,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":31,"created_at":"2013-06-10T07:23:13.000Z","updated_at":"2026-03-05T02:59:38.000Z","published_at":"2013-06-10T07:23:13.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\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\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eZigZag MATRIX with REFLECTED format.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eWe have only input x. We have to create a matrix in the following pattern.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ input n=5;]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ethen\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[     2     4     6     8    10\\n     1     3     5     7     9\\n     1     3     5     7     9\\n     2     4     6     8    10]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHint: meshgrid(1:n,1:n) \u0026amp; (2*i)-mod(j,n-1)+1\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":2695,"title":"Toeplitz Matrix","description":"For a given square matrix of order n-by-n check whether this is a Toeplitz matrix or not. Return true if it is.","description_html":"\u003cdiv style = \"text-align: start; line-height: 20.4333px; 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: rgb(0, 0, 0); white-space: normal; \"\u003e\u003cdiv style=\"block-size: 21px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 407px 10.5px; transform-origin: 407px 10.5px; vertical-align: baseline; \"\u003e\u003cdiv style=\"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: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; 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: 330px 8px; transform-origin: 330px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eFor a given square matrix of order n-by-n check whether this is a Toeplitz matrix or not. Return true if it is.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function y = toeplitzcheck(a)\r\n  y = a;\r\nend","test_suite":"%%\r\na = [6 5 4;3 6 5;2 3 6];\r\ny_correct = 1;\r\nassert(isequal(toeplitzcheck(a),y_correct))\r\n\r\n%%\r\na =[3 2 1 0;4 3 2 1;5 4 3 2;6 5 4 3];\r\ny_correct = 1;\r\nassert(isequal(toeplitzcheck(a),y_correct))\r\n\r\n%%\r\na =[1 2 3;4 5 6;7 8 9];\r\ny_correct = 0;\r\nassert(isequal(toeplitzcheck(a),y_correct))\r\n\r\n%%\r\na =[2 0 7 8 9;3 2 0 7 8;6 3 2 0 7;4 6 3 2 0;1 4 6 3 2];\r\ny_correct = 1;\r\nassert(isequal(toeplitzcheck(a),y_correct))\r\n\r\n%%\r\na =[2 0 5 8 7;3 1 0 2 8;6 4 2 0 3;4 6 1 2 0;7 4 8 3 9];\r\ny_correct = 0;\r\nassert(isequal(toeplitzcheck(a),y_correct))\r\n\r\n%%\r\na =[1 1 1;0 1 0;1 1 1];\r\ny_correct = 0;\r\nassert(isequal(toeplitzcheck(a),y_correct))\r\n","published":true,"deleted":false,"likes_count":3,"comments_count":2,"created_by":32100,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":294,"test_suite_updated_at":"2021-05-08T15:50:24.000Z","rescore_all_solutions":false,"group_id":31,"created_at":"2014-11-29T11:26:11.000Z","updated_at":"2026-03-18T07:34:28.000Z","published_at":"2014-11-29T11:26:59.000Z","restored_at":null,"restored_by":null,"spam":false,"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\u003eFor a given square matrix of order n-by-n check whether this is a Toeplitz matrix or not. Return true if it is.\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":2342,"title":"Numbers spiral diagonals (Part 2)","description":"Inspired by Project Euler n°28 and 58.\r\nA n x n spiral matrix is obtained by starting with the number 1 and moving to the right in a clockwise direction.\r\nFor example with n=5, the spiral matrix is :\r\n                       21 22 23 24 25\r\n                       20  7  8  9 10\r\n                       19  6  1  2 11\r\n                       18  5  4  3 12\r\n                       17 16 15 14 13\r\nThe sum of the numbers on the diagonals is 101 (See problem 2340) and you have 5 primes (3, 5, 7, 13, 17) out of the 9 numbers lying along both diagonals. So the prime ratio is 5/9 ≈ 55%.\r\nWith a 7x7 spiral matrix, the ratio is 62% (8 primes out of the 13 diagonal numbers).\r\nWhat is the side length (always odd and greater than 1) of the square spiral for which the ratio of primes along both diagonals FIRST falls below p% ? (0\u003cp\u003c1)","description_html":"\u003cdiv style = \"text-align: start; line-height: 20.4333px; 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: rgb(0, 0, 0); white-space: normal; \"\u003e\u003cdiv style=\"block-size: 326.167px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 407px 163.083px; transform-origin: 407px 163.083px; vertical-align: baseline; \"\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: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; 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: 120px 8px; transform-origin: 120px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eInspired by Project Euler n°28 and 58.\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: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; 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: 341px 8px; transform-origin: 341px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eA n x n spiral matrix is obtained by starting with the number 1 and moving to the right in a clockwise direction.\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: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; 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: 131.5px 8px; transform-origin: 131.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eFor example with n=5, the spiral matrix is :\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgb(247, 247, 247); block-size: 102.167px; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-end-end-radius: 4px; border-end-start-radius: 4px; border-start-end-radius: 4px; border-start-start-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; margin-block-end: 10px; margin-block-start: 10px; margin-bottom: 10px; margin-inline-end: 3px; margin-inline-start: 3px; margin-left: 3px; margin-right: 3px; margin-top: 10px; perspective-origin: 404px 51.0833px; transform-origin: 404px 51.0833px; margin-left: 3px; margin-top: 10px; margin-bottom: 10px; margin-right: 3px; \"\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; 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; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 148px 8.5px; tab-size: 4; transform-origin: 148px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e                       21 22 23 24 25\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; 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; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 148px 8.5px; tab-size: 4; transform-origin: 148px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e                       20  7  8  9 10\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; 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; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 148px 8.5px; tab-size: 4; transform-origin: 148px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e                       19  6  1  2 11\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; 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; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 148px 8.5px; tab-size: 4; transform-origin: 148px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e                       18  5  4  3 12\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; 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; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 148px 8.5px; tab-size: 4; transform-origin: 148px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e                       17 16 15 14 13\u003c/span\u003e\u003c/span\u003e\u003c/div\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: 10px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 10px; perspective-origin: 384px 21px; text-align: left; transform-origin: 384px 21px; white-space: pre-wrap; margin-left: 4px; margin-top: 10px; 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: 382.5px 8px; transform-origin: 382.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eThe sum of the numbers on the diagonals is 101 (See problem 2340) and you have 5 primes (3, 5, 7, 13, 17) out of the 9 numbers lying along both diagonals. So the prime ratio is 5/9 ≈ 55%.\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: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; 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: 262px 8px; transform-origin: 262px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eWith a 7x7 spiral matrix, the ratio is 62% (8 primes out of the 13 diagonal numbers).\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: 384px 21px; text-align: left; transform-origin: 384px 21px; white-space: pre-wrap; 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: 364.5px 8px; transform-origin: 364.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"perspective-origin: 74px 8px; transform-origin: 74px 8px; \"\u003eWhat is the side length \u003c/span\u003e\u003cspan style=\"border-block-end-style: solid; border-block-end-width: 1px; border-bottom-style: solid; border-bottom-width: 1px; perspective-origin: 2.5px 8.5px; transform-origin: 2.5px 8.5px; \"\u003e(\u003c/span\u003e\u003cspan style=\"perspective-origin: 96px 8px; transform-origin: 96px 8px; \"\u003ealways odd and greater than 1\u003c/span\u003e\u003cspan style=\"border-block-end-style: solid; border-block-end-width: 1px; border-bottom-style: solid; border-bottom-width: 1px; perspective-origin: 2.5px 8.5px; transform-origin: 2.5px 8.5px; \"\u003e)\u003c/span\u003e\u003cspan style=\"perspective-origin: 189.5px 8px; transform-origin: 189.5px 8px; \"\u003e of the square spiral for which the ratio of primes along both diagonals FIRST falls below p% ? (0\u0026lt;p\u0026lt;1)\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function res=spiral_ratio(pourcentage)\r\nres=pourcentage*2;\r\nend","test_suite":"%%\r\nx = 0.8;\r\ny_correct = 3;\r\nassert(isequal(spiral_ratio(x),y_correct))\r\n%%\r\nx = 0.5;\r\ny_correct = 11;\r\nassert(isequal(spiral_ratio(x),y_correct))\r\n%%\r\nx = 0.4;\r\ny_correct = 31;\r\nassert(isequal(spiral_ratio(x),y_correct))\r\n%%\r\nx = 0.3;\r\ny_correct = 49;\r\nassert(isequal(spiral_ratio(x),y_correct))\r\n%%\r\nx = 0.25;\r\ny_correct = 99;\r\nassert(isequal(spiral_ratio(x),y_correct))\r\n%%\r\nx = 0.2;\r\ny_correct = 309;\r\nassert(isequal(spiral_ratio(x),y_correct))\r\n%%\r\nx = 0.15;\r\ny_correct = 981;\r\nassert(isequal(spiral_ratio(x),y_correct))\r\n%%\r\nx = 0.14;\r\ny_correct = 1883;\r\nassert(isequal(spiral_ratio(x),y_correct))\r\n%%\r\nx = 0.13;\r\ny_correct = 3593;\r\nassert(isequal(spiral_ratio(x),y_correct))\r\n%%\r\nx = 0.12;\r\ny_correct = 6523;\r\nassert(isequal(spiral_ratio(x),y_correct))\r\n%%\r\nx = 0.11;\r\ny_correct = 12201;\r\nassert(isequal(spiral_ratio(x),y_correct))","published":true,"deleted":false,"likes_count":7,"comments_count":5,"created_by":5390,"edited_by":223089,"edited_at":"2022-09-26T17:42:20.000Z","deleted_by":null,"deleted_at":null,"solvers_count":196,"test_suite_updated_at":"2022-07-09T19:28:50.000Z","rescore_all_solutions":false,"group_id":31,"created_at":"2014-05-31T18:36:25.000Z","updated_at":"2026-03-04T11:06:42.000Z","published_at":"2014-05-31T18:53:35.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\u003eInspired by Project Euler n°28 and 58.\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\u003eA n x n spiral matrix is obtained by starting with the number 1 and moving to the right in a clockwise direction.\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 with n=5, the spiral matrix is :\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[                       21 22 23 24 25\\n                       20  7  8  9 10\\n                       19  6  1  2 11\\n                       18  5  4  3 12\\n                       17 16 15 14 13]]\u003e\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\u003eThe sum of the numbers on the diagonals is 101 (See problem 2340) and you have 5 primes (3, 5, 7, 13, 17) out of the 9 numbers lying along both diagonals. So the prime ratio is 5/9 ≈ 55%.\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\u003eWith a 7x7 spiral matrix, the ratio is 62% (8 primes out of the 13 diagonal numbers).\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\u003eWhat is the side length (always odd and greater than 1) of the square spiral for which the ratio of primes along both diagonals FIRST falls below p% ? (0\u0026lt;p\u0026lt;1)\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":1642,"title":"BULLSEYE Part 2:   Reference Problem 18 BULLSEYE ","description":"Given n (always odd), return output a that has concentric rings of the 1s and 0s  around the center point. Examples:\r\n\r\n Input  n = 3\r\n Output a is [ 1 1 1 \r\n               1 0 1\r\n               1 1 1 ]\r\n\r\n Input  n = 5\r\n Output a is [    1     1     1     1     1\r\n                  1     0     0     0     1\r\n                  1     0     1     0     1\r\n                  1     0     0     0     1\r\n                  1     1     1     1     1]\r\n\r\n","description_html":"\u003cp\u003eGiven n (always odd), return output a that has concentric rings of the 1s and 0s  around the center point. Examples:\u003c/p\u003e\u003cpre\u003e Input  n = 3\r\n Output a is [ 1 1 1 \r\n               1 0 1\r\n               1 1 1 ]\u003c/pre\u003e\u003cpre\u003e Input  n = 5\r\n Output a is [    1     1     1     1     1\r\n                  1     0     0     0     1\r\n                  1     0     1     0     1\r\n                  1     0     0     0     1\r\n                  1     1     1     1     1]\u003c/pre\u003e","function_template":"function y = bullseye2(n)\r\n  y = n;\r\nend","test_suite":"%%\r\nn = 3;\r\na = [1     1     1;\r\n     1     0     1;\r\n     1     1     1;]\r\nassert(isequal(bullseye2(n),a));\r\n\r\n%%\r\nn = 5;\r\na = [1     1     1     1     1;\r\n     1     0     0     0     1;\r\n     1     0     1     0     1;\r\n     1     0     0     0     1;\r\n     1     1     1     1     1;]\r\nassert(isequal(bullseye2(n),a));\r\n\r\n%%\r\nn = 7;\r\na = [1     1     1     1     1     1     1;\r\n     1     0     0     0     0     0     1;\r\n     1     0     1     1     1     0     1;\r\n     1     0     1     0     1     0     1;\r\n     1     0     1     1     1     0     1;\r\n     1     0     0     0     0     0     1;\r\n     1     1     1     1     1     1     1];\r\nassert(isequal(bullseye2(n),a));\r\n\r\n%%\r\nn = 9;\r\na = [1     1     1     1     1     1     1     1     1;\r\n     1     0     0     0     0     0     0     0     1;\r\n     1     0     1     1     1     1     1     0     1;\r\n     1     0     1     0     0     0     1     0     1;\r\n     1     0     1     0     1     0     1     0     1;\r\n     1     0     1     0     0     0     1     0     1;\r\n     1     0     1     1     1     1     1     0     1;\r\n     1     0     0     0     0     0     0     0     1;\r\n     1     1     1     1     1     1     1     1     1];\r\nassert(isequal(bullseye2(n),a));","published":true,"deleted":false,"likes_count":2,"comments_count":2,"created_by":13514,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":158,"test_suite_updated_at":"2017-07-12T16:03:39.000Z","rescore_all_solutions":false,"group_id":31,"created_at":"2013-06-11T06:57:35.000Z","updated_at":"2026-03-06T03:36:18.000Z","published_at":"2013-06-11T06:57:35.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\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\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eGiven n (always odd), return output a that has concentric rings of the 1s and 0s around the center point. Examples:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ Input  n = 3\\n Output a is [ 1 1 1 \\n               1 0 1\\n               1 1 1 ]\\n\\n Input  n = 5\\n Output a is [    1     1     1     1     1\\n                  1     0     0     0     1\\n                  1     0     1     0     1\\n                  1     0     0     0     1\\n                  1     1     1     1     1]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":1704,"title":"Triangular matrices in 3D array","description":"Given a 3D numeric array _x_, return an array _y_ of the same size in which all entries to the right of the main diagonal are zero for _y_(:,:,i).\r\n\r\n*Example*\r\n\r\nIf\r\n\r\n  x(:,:,1) = 1 2 3\r\n             4 5 6\r\n             7 8 9\r\n\r\n  x(:,:,2) = 1 4 7\r\n             2 5 8\r\n             3 6 9\r\n\r\n  x(:,:,3) = 1 2 3\r\n             1 2 3\r\n             1 2 3\r\n\r\nthen\r\n\r\n  y(:,:,1) = 1 0 0\r\n             4 5 0\r\n             7 8 9\r\n\r\n  y(:,:,2) = 1 0 0\r\n             2 5 0\r\n             3 6 9\r\n\r\n  y(:,:,3) = 1 0 0\r\n             1 2 0\r\n             1 2 3","description_html":"\u003cp\u003eGiven a 3D numeric array \u003ci\u003ex\u003c/i\u003e, return an array \u003ci\u003ey\u003c/i\u003e of the same size in which all entries to the right of the main diagonal are zero for \u003ci\u003ey\u003c/i\u003e(:,:,i).\u003c/p\u003e\u003cp\u003e\u003cb\u003eExample\u003c/b\u003e\u003c/p\u003e\u003cp\u003eIf\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003ex(:,:,1) = 1 2 3\r\n           4 5 6\r\n           7 8 9\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003ex(:,:,2) = 1 4 7\r\n           2 5 8\r\n           3 6 9\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003ex(:,:,3) = 1 2 3\r\n           1 2 3\r\n           1 2 3\r\n\u003c/pre\u003e\u003cp\u003ethen\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003ey(:,:,1) = 1 0 0\r\n           4 5 0\r\n           7 8 9\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003ey(:,:,2) = 1 0 0\r\n           2 5 0\r\n           3 6 9\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003ey(:,:,3) = 1 0 0\r\n           1 2 0\r\n           1 2 3\r\n\u003c/pre\u003e","function_template":"function y = tril3(x)\r\n  y = x;\r\nend","test_suite":"%%\r\nx(:,:,1) = [1 2 3;4 5 6;7 8 9];\r\nx(:,:,2) = [1 4 7;2 5 8;3 6 9];\r\nx(:,:,3) = [1 2 3;1 2 3;1 2 3];\r\ny_correct(:,:,1) = [1 0 0;4 5 0;7 8 9];\r\ny_correct(:,:,2) = [1 0 0;2 5 0;3 6 9];\r\ny_correct(:,:,3) = [1 0 0;1 2 0;1 2 3];\r\nassert(isequal(tril3(x),y_correct))\r\n\r\n%%\r\nx = cumsum(ones(3,3,50));\r\ny_correct = repmat([1 0 0;2 2 0; 3 3 3],[1,1,50]);\r\nassert(isequal(tril3(x),y_correct))\r\n\r\n%%\r\nx = cumsum(ones(5,5,100),2);\r\ny_correct = repmat(tril(cumsum(ones(5,5),2)),[1,1,100]);\r\nassert(isequal(tril3(x),y_correct))\r\n\r\n%%\r\nx = rand([1 1 400]);\r\ny_correct = x;\r\nassert(isequal(tril3(x),y_correct))\r\n\r\n%%\r\nx = reshape(1:40,[2,2,10]);\r\ny_correct(:,:,1) = [1 0;2 4];\r\ny_correct(:,:,2) = [5 0;6 8];\r\ny_correct(:,:,3) = [9 0;10 12];\r\ny_correct(:,:,4) = [13 0;14 16];\r\ny_correct(:,:,5) = [17 0;18 20];\r\ny_correct(:,:,6) = [21 0;22 24];\r\ny_correct(:,:,7) = [25 0;26 28];\r\ny_correct(:,:,8) = [29 0;30 32];\r\ny_correct(:,:,9) = [33 0;34 36];\r\ny_correct(:,:,10) = [37 0;38 40];\r\nassert(isequal(tril3(x),y_correct))\r\n\r\n%%\r\nx = randi([0 1],[4,4,5]);\r\ny_correct(:,:,1) = tril(x(:,:,1));\r\ny_correct(:,:,2) = tril(x(:,:,2));\r\ny_correct(:,:,3) = tril(x(:,:,3));\r\ny_correct(:,:,4) = tril(x(:,:,4));\r\ny_correct(:,:,5) = tril(x(:,:,5));\r\nassert(isequal(tril3(x),y_correct))","published":true,"deleted":false,"likes_count":2,"comments_count":0,"created_by":4793,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":169,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":31,"created_at":"2013-07-09T14:02:30.000Z","updated_at":"2026-03-21T07:18:04.000Z","published_at":"2013-07-09T14:02:30.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\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\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eGiven a 3D numeric array\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ex\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, return an array\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ey\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e of the same size in which all entries to the right of the main diagonal are zero for\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ey\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e(:,:,i).\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eExample\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eIf\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[x(:,:,1) = 1 2 3\\n           4 5 6\\n           7 8 9\\n\\nx(:,:,2) = 1 4 7\\n           2 5 8\\n           3 6 9\\n\\nx(:,:,3) = 1 2 3\\n           1 2 3\\n           1 2 3]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ethen\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[y(:,:,1) = 1 0 0\\n           4 5 0\\n           7 8 9\\n\\ny(:,:,2) = 1 0 0\\n           2 5 0\\n           3 6 9\\n\\ny(:,:,3) = 1 0 0\\n           1 2 0\\n           1 2 3]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":1845,"title":"Pascal's pyramid","description":"In Pascal's triangle each number is the sum of the two nearest numbers in the line above:\r\n\r\n           1\r\n         1   1\r\n       1   2   1\r\n     1   3   3   1\r\n   1   4   6   4   1\r\n\r\nA three-dimensional analog of Pascal's triangle can be defined as a square pyramid in which each number is the sum of the four nearest numbers in the layer above.  \r\nDefine a function |pascalp(n)| that returns the nth layer of this pyramid, as follows:\r\n\r\n   pascalp(1)\r\n      1\r\n   pascalp(2)\r\n      1  1\r\n      1  1\r\n   pascalp(3)\r\n      1  2  1\r\n      2  4  2\r\n      1  2  1\r\n   pascalp(4)\r\n      1  3  3  1\r\n      3  9  9  3\r\n      3  9  9  3\r\n      1  3  3  1\r\n   pascalp(5)\r\n      1  4  6  4 1\r\n      4 16 24 16 4\r\n      6 24 36 24 6\r\n      4 16 24 16 4\r\n      1  4  6  4 1\r\n\r\nNote: Pascal's pyramid can also be defined as a tetrahedron (see \u003chttp://en.wikipedia.org/wiki/Pascal%27s_pyramid\u003e), in which case the layers are triangular rather than square, and the numbers are the trinomial coefficients.","description_html":"\u003cp\u003eIn Pascal's triangle each number is the sum of the two nearest numbers in the line above:\u003c/p\u003e\u003cpre\u003e           1\r\n         1   1\r\n       1   2   1\r\n     1   3   3   1\r\n   1   4   6   4   1\u003c/pre\u003e\u003cp\u003eA three-dimensional analog of Pascal's triangle can be defined as a square pyramid in which each number is the sum of the four nearest numbers in the layer above.  \r\nDefine a function \u003ctt\u003epascalp(n)\u003c/tt\u003e that returns the nth layer of this pyramid, as follows:\u003c/p\u003e\u003cpre\u003e   pascalp(1)\r\n      1\r\n   pascalp(2)\r\n      1  1\r\n      1  1\r\n   pascalp(3)\r\n      1  2  1\r\n      2  4  2\r\n      1  2  1\r\n   pascalp(4)\r\n      1  3  3  1\r\n      3  9  9  3\r\n      3  9  9  3\r\n      1  3  3  1\r\n   pascalp(5)\r\n      1  4  6  4 1\r\n      4 16 24 16 4\r\n      6 24 36 24 6\r\n      4 16 24 16 4\r\n      1  4  6  4 1\u003c/pre\u003e\u003cp\u003eNote: Pascal's pyramid can also be defined as a tetrahedron (see \u003ca href = \"http://en.wikipedia.org/wiki/Pascal%27s_pyramid\"\u003ehttp://en.wikipedia.org/wiki/Pascal%27s_pyramid\u003c/a\u003e), in which case the layers are triangular rather than square, and the numbers are the trinomial coefficients.\u003c/p\u003e","function_template":"function p=pascalp(n)\r\np=1;\r\n","test_suite":"%%\r\nn=1;\r\np=1;\r\nassert(isequal(pascalp(n),p))\r\n%%\r\nn=3;\r\np=[1 2 1;2 4 2;1 2 1];\r\nassert(isequal(pascalp(n),p))\r\n%%\r\nn=5;\r\np=[1 4 6 4 1;4 16 24 16 4;6 24 36 24 6;4 16 24 16 4;1 4 6 4 1];\r\nassert(isequal(pascalp(n),p))\r\n%%\r\nn=8;\r\np=[1 7 21 35 35 21 7 1;7 49 147 245 245 147 49 7;21 147 441 735 735 441 147 21;35 245 735 1225 1225 735 245 35;35 245 735 1225 1225 735 245 35;21 147 441 735 735 441 147 21;7 49 147 245 245 147 49 7;1 7 21 35 35 21 7 1];\r\nassert(isequal(pascalp(n),p))\r\n\r\n","published":true,"deleted":false,"likes_count":4,"comments_count":0,"created_by":245,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":135,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":31,"created_at":"2013-08-22T21:45:54.000Z","updated_at":"2026-03-01T09:52:40.000Z","published_at":"2013-08-22T21:59:49.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\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\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eIn Pascal's triangle each number is the sum of the two nearest numbers in the line above:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[           1\\n         1   1\\n       1   2   1\\n     1   3   3   1\\n   1   4   6   4   1]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eA three-dimensional analog of Pascal's triangle can be defined as a square pyramid in which each number is the sum of the four nearest numbers in the layer above. Define a function\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003epascalp(n)\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e that returns the nth layer of this pyramid, as follows:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[   pascalp(1)\\n      1\\n   pascalp(2)\\n      1  1\\n      1  1\\n   pascalp(3)\\n      1  2  1\\n      2  4  2\\n      1  2  1\\n   pascalp(4)\\n      1  3  3  1\\n      3  9  9  3\\n      3  9  9  3\\n      1  3  3  1\\n   pascalp(5)\\n      1  4  6  4 1\\n      4 16 24 16 4\\n      6 24 36 24 6\\n      4 16 24 16 4\\n      1  4  6  4 1]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eNote: Pascal's pyramid can also be defined as a tetrahedron (see\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://en.wikipedia.org/wiki/Pascal%27s_pyramid\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ehttp://en.wikipedia.org/wiki/Pascal%27s_pyramid\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e), in which case the layers are triangular rather than square, and the numbers are the trinomial coefficients.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":2064,"title":"Generate this matrix","description":"Generate the following matrix.\r\n\r\n  n = 2;\r\n  out = [-4    -3    -2    -1     0\r\n         -3    -2    -1     0     1\r\n         -2    -1     0     1     2\r\n         -1     0     1     2     3\r\n          0     1     2     3     4];\r\n  \r\n  n = 5;\r\n  out = [-10    -9    -8    -7    -6    -5    -4    -3    -2    -1     0\r\n          -9    -8    -7    -6    -5    -4    -3    -2    -1     0     1\r\n          -8    -7    -6    -5    -4    -3    -2    -1     0     1     2\r\n          -7    -6    -5    -4    -3    -2    -1     0     1     2     3\r\n          -6    -5    -4    -3    -2    -1     0     1     2     3     4\r\n          -5    -4    -3    -2    -1     0     1     2     3     4     5\r\n          -4    -3    -2    -1     0     1     2     3     4     5     6\r\n          -3    -2    -1     0     1     2     3     4     5     6     7\r\n          -2    -1     0     1     2     3     4     5     6     7     8\r\n          -1     0     1     2     3     4     5     6     7     8     9\r\n           0     1     2     3     4     5     6     7     8     9    10];","description_html":"\u003cp\u003eGenerate the following matrix.\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003en = 2;\r\nout = [-4    -3    -2    -1     0\r\n       -3    -2    -1     0     1\r\n       -2    -1     0     1     2\r\n       -1     0     1     2     3\r\n        0     1     2     3     4];\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003en = 5;\r\nout = [-10    -9    -8    -7    -6    -5    -4    -3    -2    -1     0\r\n        -9    -8    -7    -6    -5    -4    -3    -2    -1     0     1\r\n        -8    -7    -6    -5    -4    -3    -2    -1     0     1     2\r\n        -7    -6    -5    -4    -3    -2    -1     0     1     2     3\r\n        -6    -5    -4    -3    -2    -1     0     1     2     3     4\r\n        -5    -4    -3    -2    -1     0     1     2     3     4     5\r\n        -4    -3    -2    -1     0     1     2     3     4     5     6\r\n        -3    -2    -1     0     1     2     3     4     5     6     7\r\n        -2    -1     0     1     2     3     4     5     6     7     8\r\n        -1     0     1     2     3     4     5     6     7     8     9\r\n         0     1     2     3     4     5     6     7     8     9    10];\r\n\u003c/pre\u003e","function_template":"function y = mystery_matrix(n)\r\n  y = 0;\r\nend","test_suite":"%%\r\nn = 2;\r\ny_correct = [-4    -3    -2    -1     0\r\n             -3    -2    -1     0     1\r\n             -2    -1     0     1     2\r\n             -1     0     1     2     3\r\n              0     1     2     3     4];\r\nassert(isequal(mystery_matrix(n),y_correct))\r\n\r\n%%\r\nn = 3;\r\ny_correct = [-6    -5    -4    -3    -2    -1     0\r\n             -5    -4    -3    -2    -1     0     1\r\n             -4    -3    -2    -1     0     1     2\r\n             -3    -2    -1     0     1     2     3\r\n             -2    -1     0     1     2     3     4\r\n             -1     0     1     2     3     4     5\r\n              0     1     2     3     4     5     6];\r\nassert(isequal(mystery_matrix(n),y_correct))\r\n\r\n%%\r\nn = 5;\r\ny_correct = [-10    -9    -8    -7    -6    -5    -4    -3    -2    -1     0\r\n        -9    -8    -7    -6    -5    -4    -3    -2    -1     0     1\r\n        -8    -7    -6    -5    -4    -3    -2    -1     0     1     2\r\n        -7    -6    -5    -4    -3    -2    -1     0     1     2     3\r\n        -6    -5    -4    -3    -2    -1     0     1     2     3     4\r\n        -5    -4    -3    -2    -1     0     1     2     3     4     5\r\n        -4    -3    -2    -1     0     1     2     3     4     5     6\r\n        -3    -2    -1     0     1     2     3     4     5     6     7\r\n        -2    -1     0     1     2     3     4     5     6     7     8\r\n        -1     0     1     2     3     4     5     6     7     8     9\r\n         0     1     2     3     4     5     6     7     8     9    10];\r\nassert(isequal(mystery_matrix(n),y_correct))\r\n","published":true,"deleted":false,"likes_count":13,"comments_count":1,"created_by":17852,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":813,"test_suite_updated_at":"2017-07-20T19:16:01.000Z","rescore_all_solutions":true,"group_id":31,"created_at":"2013-12-19T11:08:36.000Z","updated_at":"2026-03-20T13:57:32.000Z","published_at":"2013-12-19T11:09:22.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\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\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eGenerate the following matrix.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[n = 2;\\nout = [-4    -3    -2    -1     0\\n       -3    -2    -1     0     1\\n       -2    -1     0     1     2\\n       -1     0     1     2     3\\n        0     1     2     3     4];\\n\\nn = 5;\\nout = [-10    -9    -8    -7    -6    -5    -4    -3    -2    -1     0\\n        -9    -8    -7    -6    -5    -4    -3    -2    -1     0     1\\n        -8    -7    -6    -5    -4    -3    -2    -1     0     1     2\\n        -7    -6    -5    -4    -3    -2    -1     0     1     2     3\\n        -6    -5    -4    -3    -2    -1     0     1     2     3     4\\n        -5    -4    -3    -2    -1     0     1     2     3     4     5\\n        -4    -3    -2    -1     0     1     2     3     4     5     6\\n        -3    -2    -1     0     1     2     3     4     5     6     7\\n        -2    -1     0     1     2     3     4     5     6     7     8\\n        -1     0     1     2     3     4     5     6     7     8     9\\n         0     1     2     3     4     5     6     7     8     9    10];]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":2177,"title":"Rainbow matrix","description":"Create a \"rainbow matrix\" as described in the following examples\r\n\r\n Input = 3\r\n Output = [ 1 2 3\r\n            2 3 2 \r\n            3 2 1 ]\r\n\r\n\r\n Input = 5\r\n Output = [ 1 2 3 4 5\r\n            2 3 4 5 4\r\n            3 4 5 4 3\r\n            4 5 4 3 2\r\n            5 4 3 2 1 ]","description_html":"\u003cp\u003eCreate a \"rainbow matrix\" as described in the following examples\u003c/p\u003e\u003cpre\u003e Input = 3\r\n Output = [ 1 2 3\r\n            2 3 2 \r\n            3 2 1 ]\u003c/pre\u003e\u003cpre\u003e Input = 5\r\n Output = [ 1 2 3 4 5\r\n            2 3 4 5 4\r\n            3 4 5 4 3\r\n            4 5 4 3 2\r\n            5 4 3 2 1 ]\u003c/pre\u003e","function_template":"function y = rainbow(x)\r\n  y = x;\r\nend","test_suite":"%%\r\nx = 1;\r\ny_correct = 1;\r\nassert(isequal(rainbow(x),y_correct))\r\n\r\n%%\r\nx = 3;\r\ny_correct = [1 2 3; 2 3 2; 3 2 1];\r\nassert(isequal(rainbow(x),y_correct))\r\n\r\n%%\r\nx = 5;\r\ny_correct = [1 2 3 4 5; 2 3 4 5 4; 3 4 5 4 3; 4 5 4 3 2; 5 4 3 2 1];\r\nassert(isequal(rainbow(x),y_correct))\r\n\r\n%%\r\nx = 7;\r\ny_correct = [...\r\n     1     2     3     4     5     6     7;\r\n     2     3     4     5     6     7     6;\r\n     3     4     5     6     7     6     5;\r\n     4     5     6     7     6     5     4;\r\n     5     6     7     6     5     4     3;\r\n     6     7     6     5     4     3     2;\r\n     7     6     5     4     3     2     1];\r\nassert(isequal(rainbow(x),y_correct))","published":true,"deleted":false,"likes_count":4,"comments_count":1,"created_by":23122,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":209,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":31,"created_at":"2014-02-12T11:12:45.000Z","updated_at":"2026-02-28T15:56:13.000Z","published_at":"2014-02-12T11:13:02.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\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\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eCreate a \\\"rainbow matrix\\\" as described in the following examples\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ Input = 3\\n Output = [ 1 2 3\\n            2 3 2 \\n            3 2 1 ]\\n\\n Input = 5\\n Output = [ 1 2 3 4 5\\n            2 3 4 5 4\\n            3 4 5 4 3\\n            4 5 4 3 2\\n            5 4 3 2 1 ]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":2336,"title":"Calendar Matrix","description":"Return a calendar matrix for the given values of month and year. Assume that Sunday is the first day of the week. The resulting calendar month will have seven columns and only as many rows (weeks) as needed to display all days of the month. Thus it might have either 4, 5, or 6 rows. Days preceding the first of the month or following the last of the month are given by zeros.\r\n\r\nExamples\r\n\r\n \u003e\u003e mo = 1\r\n \u003e\u003e yr = 2014\r\n \u003e\u003e momat = makeCalendar(mo,yr)\r\n\r\n     0     0     0     1     2     3     4\r\n     5     6     7     8     9    10    11\r\n    12    13    14    15    16    17    18\r\n    19    20    21    22    23    24    25\r\n    26    27    28    29    30    31     0\r\n\r\n \u003e\u003e mo = 2\r\n \u003e\u003e yr = 2015\r\n \u003e\u003e momat = makeCalendar(mo,yr)\r\n\r\n     1     2     3     4     5     6     7\r\n     8     9    10    11    12    13    14\r\n    15    16    17    18    19    20    21\r\n    22    23    24    25    26    27    28\r\n\r\n\r\n \u003e\u003e mo = 10\r\n \u003e\u003e yr = 2010\r\n \u003e\u003e momat = makeCalendar(mo,yr)\r\n\r\n     0     0     0     0     0     1     2\r\n     3     4     5     6     7     8     9\r\n    10    11    12    13    14    15    16\r\n    17    18    19    20    21    22    23\r\n    24    25    26    27    28    29    30\r\n    31     0     0     0     0     0     0","description_html":"\u003cp\u003eReturn a calendar matrix for the given values of month and year. Assume that Sunday is the first day of the week. The resulting calendar month will have seven columns and only as many rows (weeks) as needed to display all days of the month. Thus it might have either 4, 5, or 6 rows. Days preceding the first of the month or following the last of the month are given by zeros.\u003c/p\u003e\u003cp\u003eExamples\u003c/p\u003e\u003cpre\u003e \u0026gt;\u0026gt; mo = 1\r\n \u0026gt;\u0026gt; yr = 2014\r\n \u0026gt;\u0026gt; momat = makeCalendar(mo,yr)\u003c/pre\u003e\u003cpre\u003e     0     0     0     1     2     3     4\r\n     5     6     7     8     9    10    11\r\n    12    13    14    15    16    17    18\r\n    19    20    21    22    23    24    25\r\n    26    27    28    29    30    31     0\u003c/pre\u003e\u003cpre\u003e \u0026gt;\u0026gt; mo = 2\r\n \u0026gt;\u0026gt; yr = 2015\r\n \u0026gt;\u0026gt; momat = makeCalendar(mo,yr)\u003c/pre\u003e\u003cpre\u003e     1     2     3     4     5     6     7\r\n     8     9    10    11    12    13    14\r\n    15    16    17    18    19    20    21\r\n    22    23    24    25    26    27    28\u003c/pre\u003e\u003cpre\u003e \u0026gt;\u0026gt; mo = 10\r\n \u0026gt;\u0026gt; yr = 2010\r\n \u0026gt;\u0026gt; momat = makeCalendar(mo,yr)\u003c/pre\u003e\u003cpre\u003e     0     0     0     0     0     1     2\r\n     3     4     5     6     7     8     9\r\n    10    11    12    13    14    15    16\r\n    17    18    19    20    21    22    23\r\n    24    25    26    27    28    29    30\r\n    31     0     0     0     0     0     0\u003c/pre\u003e","function_template":"function momat = makeCalendar(mo,yr)\r\n  momat = 0;\r\nend","test_suite":"%%\r\n\r\nmo = 1;\r\nyr = 2014;\r\nmomat = makeCalendar(mo,yr);\r\nmomat_correct = [ ...\r\n     0     0     0     1     2     3     4\r\n     5     6     7     8     9    10    11\r\n    12    13    14    15    16    17    18\r\n    19    20    21    22    23    24    25\r\n    26    27    28    29    30    31     0];\r\nassert(isequal(momat,momat_correct)) \r\n\r\n%%\r\n\r\nmo = 2;\r\nyr = 2015;\r\nmomat = makeCalendar(mo,yr);\r\nmomat_correct = [ ...\r\n     1     2     3     4     5     6     7\r\n     8     9    10    11    12    13    14\r\n    15    16    17    18    19    20    21\r\n    22    23    24    25    26    27    28];\r\nassert(isequal(momat,momat_correct)) \r\n\r\n%%\r\n\r\nmo = 10;\r\nyr = 2010;\r\nmomat = makeCalendar(mo,yr);\r\nmomat_correct = [ ...\r\n     0     0     0     0     0     1     2\r\n     3     4     5     6     7     8     9\r\n    10    11    12    13    14    15    16\r\n    17    18    19    20    21    22    23\r\n    24    25    26    27    28    29    30\r\n    31     0     0     0     0     0     0];\r\nassert(isequal(momat,momat_correct))\r\n\r\n%%\r\n\r\nmo = 11;\r\nyr = 1972;\r\nmomat = makeCalendar(mo,yr);\r\nmomat_correct = [0 0 0 1 2 3 4;5 6 7 8 9 10 11;12 13 14 15 16 17 18;19 20 21 22 23 24 25;26 27 28 29 30 0 0];\r\n\r\nassert(isequal(momat,momat_correct))\r\n\r\n","published":true,"deleted":false,"likes_count":3,"comments_count":0,"created_by":7,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":161,"test_suite_updated_at":"2014-05-22T21:48:47.000Z","rescore_all_solutions":false,"group_id":31,"created_at":"2014-05-22T20:36:33.000Z","updated_at":"2026-03-03T16:29:43.000Z","published_at":"2014-05-22T20:47:34.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\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\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eReturn a calendar matrix for the given values of month and year. Assume that Sunday is the first day of the week. The resulting calendar month will have seven columns and only as many rows (weeks) as needed to display all days of the month. Thus it might have either 4, 5, or 6 rows. Days preceding the first of the month or following the last of the month are given by zeros.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExamples\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ \u003e\u003e mo = 1\\n \u003e\u003e yr = 2014\\n \u003e\u003e momat = makeCalendar(mo,yr)\\n\\n     0     0     0     1     2     3     4\\n     5     6     7     8     9    10    11\\n    12    13    14    15    16    17    18\\n    19    20    21    22    23    24    25\\n    26    27    28    29    30    31     0\\n\\n \u003e\u003e mo = 2\\n \u003e\u003e yr = 2015\\n \u003e\u003e momat = makeCalendar(mo,yr)\\n\\n     1     2     3     4     5     6     7\\n     8     9    10    11    12    13    14\\n    15    16    17    18    19    20    21\\n    22    23    24    25    26    27    28\\n\\n \u003e\u003e mo = 10\\n \u003e\u003e yr = 2010\\n \u003e\u003e momat = makeCalendar(mo,yr)\\n\\n     0     0     0     0     0     1     2\\n     3     4     5     6     7     8     9\\n    10    11    12    13    14    15    16\\n    17    18    19    20    21    22    23\\n    24    25    26    27    28    29    30\\n    31     0     0     0     0     0     0]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":2340,"title":"Numbers spiral diagonals (Part 1)","description":"Inspired by Project Euler n°28 et 58.\r\n\r\nA n x n spiral matrix is obtained by starting with the number 1 and moving to the right in a clockwise direction.\r\n\r\nFor exemple with n=5, the spiral matrix is :\r\n\r\n                       21 22 23 24 25\r\n                       20  7  8  9 10\r\n                       19  6  1  2 11\r\n                       18  5  4  3 12\r\n                       17 16 15 14 13\r\n\r\nIn this example, the sum of the numbers on the diagonals is 101.\r\n\r\nWhat is the sum of the numbers on the diagonals in any n by n spiral (n always odd) ?\r\n\r\nHINTS: You want the diagonals, not the whole matrix.","description_html":"\u003cp\u003eInspired by Project Euler n°28 et 58.\u003c/p\u003e\u003cp\u003eA n x n spiral matrix is obtained by starting with the number 1 and moving to the right in a clockwise direction.\u003c/p\u003e\u003cp\u003eFor exemple with n=5, the spiral matrix is :\u003c/p\u003e\u003cpre\u003e                       21 22 23 24 25\r\n                       20  7  8  9 10\r\n                       19  6  1  2 11\r\n                       18  5  4  3 12\r\n                       17 16 15 14 13\u003c/pre\u003e\u003cp\u003eIn this example, the sum of the numbers on the diagonals is 101.\u003c/p\u003e\u003cp\u003eWhat is the sum of the numbers on the diagonals in any n by n spiral (n always odd) ?\u003c/p\u003e\u003cp\u003eHINTS: You want the diagonals, not the whole matrix.\u003c/p\u003e","function_template":"function y = spiral_nb(x)\r\n  y = x;\r\nend","test_suite":"%%\r\nx = 3;\r\ny_correct = 25;\r\nassert(isequal(spiral_nb(x),y_correct))\r\n%%\r\nx = 5;\r\ny_correct = 101;\r\nassert(isequal(spiral_nb(x),y_correct))\r\n%%\r\nx = 9;\r\ny_correct = 537;\r\nassert(isequal(spiral_nb(x),y_correct))\r\n%%\r\nx = 501;\r\ny_correct = 83960501;\r\nassert(isequal(spiral_nb(x),y_correct))\r\n%%\r\nx = 5001;\r\ny_correct = 83395855001;\r\nassert(isequal(spiral_nb(x),y_correct))\r\n%%\r\nx = 10001;\r\ny_correct = 666916710001;\r\nassert(isequal(spiral_nb(x),y_correct))\r\n%%\r\nx = 10003;\r\ny_correct = 667316890025;\r\nassert(isequal(spiral_nb(x),y_correct))","published":true,"deleted":false,"likes_count":4,"comments_count":1,"created_by":5390,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":296,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":31,"created_at":"2014-05-30T22:02:51.000Z","updated_at":"2026-02-01T14:00:50.000Z","published_at":"2014-05-30T22:03:01.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\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\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eInspired by Project Euler n°28 et 58.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eA n x n spiral matrix is obtained by starting with the number 1 and moving to the right in a clockwise direction.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eFor exemple with n=5, the spiral matrix is :\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[                       21 22 23 24 25\\n                       20  7  8  9 10\\n                       19  6  1  2 11\\n                       18  5  4  3 12\\n                       17 16 15 14 13]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eIn this example, the sum of the numbers on the diagonals is 101.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eWhat is the sum of the numbers on the diagonals in any n by n spiral (n always odd) ?\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHINTS: You want the diagonals, not the whole matrix.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":2342,"title":"Numbers spiral diagonals (Part 2)","description":"Inspired by Project Euler n°28 and 58.\r\nA n x n spiral matrix is obtained by starting with the number 1 and moving to the right in a clockwise direction.\r\nFor example with n=5, the spiral matrix is :\r\n                       21 22 23 24 25\r\n                       20  7  8  9 10\r\n                       19  6  1  2 11\r\n                       18  5  4  3 12\r\n                       17 16 15 14 13\r\nThe sum of the numbers on the diagonals is 101 (See problem 2340) and you have 5 primes (3, 5, 7, 13, 17) out of the 9 numbers lying along both diagonals. So the prime ratio is 5/9 ≈ 55%.\r\nWith a 7x7 spiral matrix, the ratio is 62% (8 primes out of the 13 diagonal numbers).\r\nWhat is the side length (always odd and greater than 1) of the square spiral for which the ratio of primes along both diagonals FIRST falls below p% ? (0\u003cp\u003c1)","description_html":"\u003cdiv style = \"text-align: start; line-height: 20.4333px; 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: rgb(0, 0, 0); white-space: normal; \"\u003e\u003cdiv style=\"block-size: 326.167px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 407px 163.083px; transform-origin: 407px 163.083px; vertical-align: baseline; \"\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: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; 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: 120px 8px; transform-origin: 120px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eInspired by Project Euler n°28 and 58.\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: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; 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: 341px 8px; transform-origin: 341px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eA n x n spiral matrix is obtained by starting with the number 1 and moving to the right in a clockwise direction.\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: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; 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: 131.5px 8px; transform-origin: 131.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eFor example with n=5, the spiral matrix is :\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgb(247, 247, 247); block-size: 102.167px; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-end-end-radius: 4px; border-end-start-radius: 4px; border-start-end-radius: 4px; border-start-start-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; margin-block-end: 10px; margin-block-start: 10px; margin-bottom: 10px; margin-inline-end: 3px; margin-inline-start: 3px; margin-left: 3px; margin-right: 3px; margin-top: 10px; perspective-origin: 404px 51.0833px; transform-origin: 404px 51.0833px; margin-left: 3px; margin-top: 10px; margin-bottom: 10px; margin-right: 3px; \"\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; 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; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 148px 8.5px; tab-size: 4; transform-origin: 148px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e                       21 22 23 24 25\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; 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; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 148px 8.5px; tab-size: 4; transform-origin: 148px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e                       20  7  8  9 10\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; 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; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 148px 8.5px; tab-size: 4; transform-origin: 148px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e                       19  6  1  2 11\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; 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; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 148px 8.5px; tab-size: 4; transform-origin: 148px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e                       18  5  4  3 12\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; 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; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 148px 8.5px; tab-size: 4; transform-origin: 148px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e                       17 16 15 14 13\u003c/span\u003e\u003c/span\u003e\u003c/div\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: 10px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 10px; perspective-origin: 384px 21px; text-align: left; transform-origin: 384px 21px; white-space: pre-wrap; margin-left: 4px; margin-top: 10px; 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: 382.5px 8px; transform-origin: 382.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eThe sum of the numbers on the diagonals is 101 (See problem 2340) and you have 5 primes (3, 5, 7, 13, 17) out of the 9 numbers lying along both diagonals. So the prime ratio is 5/9 ≈ 55%.\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: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; 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: 262px 8px; transform-origin: 262px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eWith a 7x7 spiral matrix, the ratio is 62% (8 primes out of the 13 diagonal numbers).\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: 384px 21px; text-align: left; transform-origin: 384px 21px; white-space: pre-wrap; 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: 364.5px 8px; transform-origin: 364.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"perspective-origin: 74px 8px; transform-origin: 74px 8px; \"\u003eWhat is the side length \u003c/span\u003e\u003cspan style=\"border-block-end-style: solid; border-block-end-width: 1px; border-bottom-style: solid; border-bottom-width: 1px; perspective-origin: 2.5px 8.5px; transform-origin: 2.5px 8.5px; \"\u003e(\u003c/span\u003e\u003cspan style=\"perspective-origin: 96px 8px; transform-origin: 96px 8px; \"\u003ealways odd and greater than 1\u003c/span\u003e\u003cspan style=\"border-block-end-style: solid; border-block-end-width: 1px; border-bottom-style: solid; border-bottom-width: 1px; perspective-origin: 2.5px 8.5px; transform-origin: 2.5px 8.5px; \"\u003e)\u003c/span\u003e\u003cspan style=\"perspective-origin: 189.5px 8px; transform-origin: 189.5px 8px; \"\u003e of the square spiral for which the ratio of primes along both diagonals FIRST falls below p% ? (0\u0026lt;p\u0026lt;1)\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function res=spiral_ratio(pourcentage)\r\nres=pourcentage*2;\r\nend","test_suite":"%%\r\nx = 0.8;\r\ny_correct = 3;\r\nassert(isequal(spiral_ratio(x),y_correct))\r\n%%\r\nx = 0.5;\r\ny_correct = 11;\r\nassert(isequal(spiral_ratio(x),y_correct))\r\n%%\r\nx = 0.4;\r\ny_correct = 31;\r\nassert(isequal(spiral_ratio(x),y_correct))\r\n%%\r\nx = 0.3;\r\ny_correct = 49;\r\nassert(isequal(spiral_ratio(x),y_correct))\r\n%%\r\nx = 0.25;\r\ny_correct = 99;\r\nassert(isequal(spiral_ratio(x),y_correct))\r\n%%\r\nx = 0.2;\r\ny_correct = 309;\r\nassert(isequal(spiral_ratio(x),y_correct))\r\n%%\r\nx = 0.15;\r\ny_correct = 981;\r\nassert(isequal(spiral_ratio(x),y_correct))\r\n%%\r\nx = 0.14;\r\ny_correct = 1883;\r\nassert(isequal(spiral_ratio(x),y_correct))\r\n%%\r\nx = 0.13;\r\ny_correct = 3593;\r\nassert(isequal(spiral_ratio(x),y_correct))\r\n%%\r\nx = 0.12;\r\ny_correct = 6523;\r\nassert(isequal(spiral_ratio(x),y_correct))\r\n%%\r\nx = 0.11;\r\ny_correct = 12201;\r\nassert(isequal(spiral_ratio(x),y_correct))","published":true,"deleted":false,"likes_count":7,"comments_count":5,"created_by":5390,"edited_by":223089,"edited_at":"2022-09-26T17:42:20.000Z","deleted_by":null,"deleted_at":null,"solvers_count":196,"test_suite_updated_at":"2022-07-09T19:28:50.000Z","rescore_all_solutions":false,"group_id":31,"created_at":"2014-05-31T18:36:25.000Z","updated_at":"2026-03-04T11:06:42.000Z","published_at":"2014-05-31T18:53:35.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\u003eInspired by Project Euler n°28 and 58.\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\u003eA n x n spiral matrix is obtained by starting with the number 1 and moving to the right in a clockwise direction.\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 with n=5, the spiral matrix is :\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[                       21 22 23 24 25\\n                       20  7  8  9 10\\n                       19  6  1  2 11\\n                       18  5  4  3 12\\n                       17 16 15 14 13]]\u003e\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\u003eThe sum of the numbers on the diagonals is 101 (See problem 2340) and you have 5 primes (3, 5, 7, 13, 17) out of the 9 numbers lying along both diagonals. So the prime ratio is 5/9 ≈ 55%.\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\u003eWith a 7x7 spiral matrix, the ratio is 62% (8 primes out of the 13 diagonal numbers).\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\u003eWhat is the side length (always odd and greater than 1) of the square spiral for which the ratio of primes along both diagonals FIRST falls below p% ? (0\u0026lt;p\u0026lt;1)\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":2365,"title":"Generate this matrix","description":"For a given odd integer n, generate a matrix as follows:\r\n\r\nInput: n = 5;\r\nOutput:\r\n  \r\n\r\n [  2     1     0     0     0\r\n    1     1     1     0     0\r\n    0     1     0     1     0\r\n    0     0     1     1     1\r\n    0     0     0     1     2  ]\r\n\r\n\r\nInput: n=7;\r\nOutput:\r\n   \r\n\r\n [  3     1     0     0     0     0     0\r\n    1     2     1     0     0     0     0\r\n    0     1     1     1     0     0     0\r\n    0     0     1     0     1     0     0\r\n    0     0     0     1     1     1     0\r\n    0     0     0     0     1     2     1\r\n    0     0     0     0     0     1     3 ]\r\n\r\nFor further understanding of the pattern, please check the test suite.","description_html":"\u003cp\u003eFor a given odd integer n, generate a matrix as follows:\u003c/p\u003e\u003cp\u003eInput: n = 5;\r\nOutput:\u003c/p\u003e\u003cpre\u003e [  2     1     0     0     0\r\n    1     1     1     0     0\r\n    0     1     0     1     0\r\n    0     0     1     1     1\r\n    0     0     0     1     2  ]\u003c/pre\u003e\u003cp\u003eInput: n=7;\r\nOutput:\u003c/p\u003e\u003cpre\u003e [  3     1     0     0     0     0     0\r\n    1     2     1     0     0     0     0\r\n    0     1     1     1     0     0     0\r\n    0     0     1     0     1     0     0\r\n    0     0     0     1     1     1     0\r\n    0     0     0     0     1     2     1\r\n    0     0     0     0     0     1     3 ]\u003c/pre\u003e\u003cp\u003eFor further understanding of the pattern, please check the test suite.\u003c/p\u003e","function_template":"function y = your_fcn_name(x)\r\n\r\nend","test_suite":"%%\r\nx = 7;\r\ny_correct = [3     1     0     0     0     0     0;\r\n     1     2     1     0     0     0     0;\r\n     0     1     1     1     0     0     0;\r\n     0     0     1     0     1     0     0;\r\n     0     0     0     1     1     1     0;\r\n     0     0     0     0     1     2     1;\r\n     0     0     0     0     0     1     3];\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n\r\n%%\r\nx = 5;\r\ny_correct =[2     1     0     0     0;\r\n     1     1     1     0     0;\r\n     0     1     0     1     0;\r\n     0     0     1     1     1;\r\n     0     0     0     1     2];\r\nassert(isequal(your_fcn_name(x),y_correct))\r\n\r\n%%\r\nx = 11;\r\ny_correct =[5     1     0     0     0     0     0     0     0     0     0;\r\n     1     4     1     0     0     0     0     0     0     0     0;\r\n     0     1     3     1     0     0     0     0     0     0     0;\r\n     0     0     1     2     1     0     0     0     0     0     0;\r\n     0     0     0     1     1     1     0     0     0     0     0;\r\n     0     0     0     0     1     0     1     0     0     0     0;\r\n     0     0     0     0     0     1     1     1     0     0     0;\r\n     0     0     0     0     0     0     1     2     1     0     0;\r\n     0     0     0     0     0     0     0     1     3     1     0;\r\n     0     0     0     0     0     0     0     0     1     4     1;\r\n     0     0     0     0     0     0     0     0     0     1     5];\r\nassert(isequal(your_fcn_name(x),y_correct));\r\n\r\n%%\r\nx = 5;\r\ny_correct =[2     1     0     0     0;\r\n     1     1     1     0     0;\r\n     0     1     0     1     0;\r\n     0     0     1     1     1;\r\n     0     0     0     1     2];\r\nassert(isequal(your_fcn_name(x),y_correct));","published":true,"deleted":false,"likes_count":4,"comments_count":1,"created_by":17203,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":166,"test_suite_updated_at":"2014-06-17T20:20:00.000Z","rescore_all_solutions":false,"group_id":31,"created_at":"2014-06-17T20:16:19.000Z","updated_at":"2026-02-18T18:06:51.000Z","published_at":"2014-06-17T20:20:00.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\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\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eFor a given odd integer n, generate a matrix as follows:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eInput: n = 5; Output:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ [  2     1     0     0     0\\n    1     1     1     0     0\\n    0     1     0     1     0\\n    0     0     1     1     1\\n    0     0     0     1     2  ]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eInput: n=7; Output:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ [  3     1     0     0     0     0     0\\n    1     2     1     0     0     0     0\\n    0     1     1     1     0     0     0\\n    0     0     1     0     1     0     0\\n    0     0     0     1     1     1     0\\n    0     0     0     0     1     2     1\\n    0     0     0     0     0     1     3 ]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eFor further understanding of the pattern, please check the test suite.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":2455,"title":"Diagonal of a Spiral Matrix","description":"Write a function that will return same output as diag(spiral(n)). The only exception is that spiral and diag functions are not allowed in your function. Also, loops are not allowed.","description_html":"\u003cp\u003eWrite a function that will return same output as diag(spiral(n)). The only exception is that spiral and diag functions are not allowed in your function. Also, loops are not allowed.\u003c/p\u003e","function_template":"function y=digSpi(n)\r\n\r\n\r\n\r\n\r\nend","test_suite":"s = evalc('type digSpi');\r\n\r\nif ~isempty(regexp(s,'error'))\r\n    error('error using error');\r\nend\r\n\r\nif ~isempty(regexp(s,'assert'))\r\n    error('error using assert');\r\nend\r\n\r\nif ~isempty(regexp(s,'diag'))\r\n    error('error using diag');\r\nend\r\n\r\nif ~isempty(regexp(s,'spiral'))\r\n    error('error using spiral');\r\nend\r\n\r\nif ~isempty(regexp(s,'for'))\r\n    error('error using for');\r\nend\r\n\r\nif ~isempty(regexp(s,'while'))\r\n    error('error using while');\r\nend\r\n\r\n%%\r\n\r\na = diag(spiral(4));\r\nb = digSpi(4);\r\nassert(isequal(a,b));\r\n\r\n%%\r\n\r\na = diag(spiral(5));\r\nb = digSpi(5);\r\nassert(isequal(a,b));\r\n\r\n%%\r\n\r\na = diag(spiral(6));\r\nb = digSpi(6);\r\nassert(isequal(a,b));\r\n\r\n%%\r\n\r\na = diag(spiral(7));\r\nb = digSpi(7);\r\nassert(isequal(a,b));\r\n\r\n%%\r\n\r\na = diag(spiral(8));\r\nb = digSpi(8);\r\nassert(isequal(a,b));","published":true,"deleted":false,"likes_count":2,"comments_count":0,"created_by":17203,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":111,"test_suite_updated_at":"2014-07-21T18:30:07.000Z","rescore_all_solutions":false,"group_id":31,"created_at":"2014-07-20T08:15:09.000Z","updated_at":"2026-03-04T17:27:33.000Z","published_at":"2014-07-20T08:15:09.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"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\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eWrite a function that will return same output as diag(spiral(n)). The only exception is that spiral and diag functions are not allowed in your function. Also, loops are not allowed.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":2694,"title":"The Matrix Construction","description":"Given two input ,first one is CN (Column Number), Second one is Dim \r\nCan you produce such a matrix for example\r\n\r\nCN=6; Dim=2\r\n\r\n K = [1     1     0     0     0     0\r\n      0     0     1     1     0     0\r\n      0     0     0     0     1     1]\r\n\r\nCN=7; Dim=2\r\n\r\n   K =[  1     1     0     0     0     0     0\r\n         0     0     1     1     0     0     0\r\n         0     0     0     0     1     1     0\r\n         1     0     0     0     0     0     1]","description_html":"\u003cp\u003eGiven two input ,first one is CN (Column Number), Second one is Dim \r\nCan you produce such a matrix for example\u003c/p\u003e\u003cp\u003eCN=6; Dim=2\u003c/p\u003e\u003cpre\u003e K = [1     1     0     0     0     0\r\n      0     0     1     1     0     0\r\n      0     0     0     0     1     1]\u003c/pre\u003e\u003cp\u003eCN=7; Dim=2\u003c/p\u003e\u003cpre\u003e   K =[  1     1     0     0     0     0     0\r\n         0     0     1     1     0     0     0\r\n         0     0     0     0     1     1     0\r\n         1     0     0     0     0     0     1]\u003c/pre\u003e","function_template":"function y = MatrixProduction(CN,Dim)\r\n  y = x;\r\nend","test_suite":"%%\r\nCN   = 7;\r\nDim  = 2;\r\nY=   [1     1     0     0     0     0     0\r\n      0     0     1     1     0     0     0\r\n      0     0     0     0     1     1     0\r\n      1     0     0     0     0     0     1]\r\n\r\nassert(isequal(MatrixProduction(CN,Dim),Y))\r\n%%\r\nCN   = 6;\r\nDim  = 2;\r\nY=   [1     1     0     0     0     0    \r\n      0     0     1     1     0     0     \r\n      0     0     0     0     1     1 ]\r\nassert(isequal(MatrixProduction(CN,Dim),Y))\r\n%%\r\nCN   = 10;\r\nDim  = 1;\r\nY=   eye(10)\r\nassert(isequal(MatrixProduction(CN,Dim),Y))\r\n\r\n%%\r\nCN   = 12;\r\nDim  = 3;\r\nY=  [ 1     1     1     0     0     0     0     0     0     0     0     0\r\n      0     0     0     1     1     1     0     0     0     0     0     0\r\n      0     0     0     0     0     0     1     1     1     0     0     0\r\n      0     0     0     0     0     0     0     0     0     1     1     1]\r\nassert(isequal(MatrixProduction(CN,Dim),Y))\r\n\r\n%%\r\nCN   = 17;\r\nDim  = 7;\r\nY= [1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 \r\n    0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 \r\n    1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 ];\r\nassert(isequal(MatrixProduction(CN,Dim),Y))\r\n\r\n%%\r\nCN   = 20;\r\nDim  = 10;\r\nY= [ones(1,10) zeros(1,10); zeros(1,10) ones(1,10)];\r\nassert(isequal(MatrixProduction(CN,Dim),Y))","published":true,"deleted":false,"likes_count":3,"comments_count":0,"created_by":22216,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":135,"test_suite_updated_at":"2014-11-27T14:34:57.000Z","rescore_all_solutions":false,"group_id":31,"created_at":"2014-11-27T14:11:37.000Z","updated_at":"2026-03-01T10:28:38.000Z","published_at":"2014-11-27T14:34:57.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\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\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eGiven two input ,first one is CN (Column Number), Second one is Dim Can you produce such a matrix for example\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eCN=6; Dim=2\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ K = [1     1     0     0     0     0\\n      0     0     1     1     0     0\\n      0     0     0     0     1     1]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eCN=7; Dim=2\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[   K =[  1     1     0     0     0     0     0\\n         0     0     1     1     0     0     0\\n         0     0     0     0     1     1     0\\n         1     0     0     0     0     0     1]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":2695,"title":"Toeplitz Matrix","description":"For a given square matrix of order n-by-n check whether this is a Toeplitz matrix or not. Return true if it is.","description_html":"\u003cdiv style = \"text-align: start; line-height: 20.4333px; 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: rgb(0, 0, 0); white-space: normal; \"\u003e\u003cdiv style=\"block-size: 21px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 407px 10.5px; transform-origin: 407px 10.5px; vertical-align: baseline; \"\u003e\u003cdiv style=\"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: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; 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: 330px 8px; transform-origin: 330px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eFor a given square matrix of order n-by-n check whether this is a Toeplitz matrix or not. Return true if it is.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function y = toeplitzcheck(a)\r\n  y = a;\r\nend","test_suite":"%%\r\na = [6 5 4;3 6 5;2 3 6];\r\ny_correct = 1;\r\nassert(isequal(toeplitzcheck(a),y_correct))\r\n\r\n%%\r\na =[3 2 1 0;4 3 2 1;5 4 3 2;6 5 4 3];\r\ny_correct = 1;\r\nassert(isequal(toeplitzcheck(a),y_correct))\r\n\r\n%%\r\na =[1 2 3;4 5 6;7 8 9];\r\ny_correct = 0;\r\nassert(isequal(toeplitzcheck(a),y_correct))\r\n\r\n%%\r\na =[2 0 7 8 9;3 2 0 7 8;6 3 2 0 7;4 6 3 2 0;1 4 6 3 2];\r\ny_correct = 1;\r\nassert(isequal(toeplitzcheck(a),y_correct))\r\n\r\n%%\r\na =[2 0 5 8 7;3 1 0 2 8;6 4 2 0 3;4 6 1 2 0;7 4 8 3 9];\r\ny_correct = 0;\r\nassert(isequal(toeplitzcheck(a),y_correct))\r\n\r\n%%\r\na =[1 1 1;0 1 0;1 1 1];\r\ny_correct = 0;\r\nassert(isequal(toeplitzcheck(a),y_correct))\r\n","published":true,"deleted":false,"likes_count":3,"comments_count":2,"created_by":32100,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":294,"test_suite_updated_at":"2021-05-08T15:50:24.000Z","rescore_all_solutions":false,"group_id":31,"created_at":"2014-11-29T11:26:11.000Z","updated_at":"2026-03-18T07:34:28.000Z","published_at":"2014-11-29T11:26:59.000Z","restored_at":null,"restored_by":null,"spam":false,"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\u003eFor a given square matrix of order n-by-n check whether this is a Toeplitz matrix or not. Return true if it is.\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":2813,"title":"Create a block diagonal matrix","description":"A block diagonal matrix is a square matrix that can be written as\r\n\r\n   A = [a  0  0  0\r\n        0  b  0  0\r\n        0  0  c  0\r\n        0  0  0 ...]\r\n\r\nwhere |a|, |b|, |c| etc. are all square matrices.\r\n\r\nConstruct |A| such that\r\n\r\n   A = [a  0  0  0\r\n        0  a  0  0\r\n        0  0  a  0\r\n        0  0  0 ...]\r\n\r\nwhere |a| is allowed to be non-square or empty and occurs |n| times. |n| is always an integer greater than or equal to 0.\r\n\r\n*Examples:*\r\n\r\n   a = [1 2 3], n = 3\r\n\r\ngives\r\n\r\n   A = [1 2 3 0 0 0 0 0 0\r\n        0 0 0 1 2 3 0 0 0\r\n        0 0 0 0 0 0 1 2 3]\r\n","description_html":"\u003cp\u003eA block diagonal matrix is a square matrix that can be written as\u003c/p\u003e\u003cpre\u003e   A = [a  0  0  0\r\n        0  b  0  0\r\n        0  0  c  0\r\n        0  0  0 ...]\u003c/pre\u003e\u003cp\u003ewhere \u003ctt\u003ea\u003c/tt\u003e, \u003ctt\u003eb\u003c/tt\u003e, \u003ctt\u003ec\u003c/tt\u003e etc. are all square matrices.\u003c/p\u003e\u003cp\u003eConstruct \u003ctt\u003eA\u003c/tt\u003e such that\u003c/p\u003e\u003cpre\u003e   A = [a  0  0  0\r\n        0  a  0  0\r\n        0  0  a  0\r\n        0  0  0 ...]\u003c/pre\u003e\u003cp\u003ewhere \u003ctt\u003ea\u003c/tt\u003e is allowed to be non-square or empty and occurs \u003ctt\u003en\u003c/tt\u003e times. \u003ctt\u003en\u003c/tt\u003e is always an integer greater than or equal to 0.\u003c/p\u003e\u003cp\u003e\u003cb\u003eExamples:\u003c/b\u003e\u003c/p\u003e\u003cpre\u003e   a = [1 2 3], n = 3\u003c/pre\u003e\u003cp\u003egives\u003c/p\u003e\u003cpre\u003e   A = [1 2 3 0 0 0 0 0 0\r\n        0 0 0 1 2 3 0 0 0\r\n        0 0 0 0 0 0 1 2 3]\u003c/pre\u003e","function_template":"function A = block_diagonal(a,n)\r\n  A = a^n;\r\nend","test_suite":"%%\r\na = [1 2 3];\r\nn = 3;\r\nA_correct = [1 2 3 0 0 0 0 0 0; 0 0 0 1 2 3 0 0 0; 0 0 0 0 0 0 1 2 3];\r\nassert(isequal(block_diagonal(a,n),A_correct))\r\n\r\n%%\r\na = [];\r\nn = 3;\r\nA_correct = [];\r\nassert(isequal(block_diagonal(a,n),A_correct))\r\n\r\n%%\r\na = [1 2 -3]';\r\nn = 0;\r\nA_correct = [];\r\nassert(isequal(block_diagonal(a,n),A_correct))\r\n\r\n%%\r\na = [3 -2]';\r\nn = 4;\r\nA_correct = [3 -2 0 0 0 0 0 0; 0 0 3 -2 0 0 0 0; 0 0 0 0 3 -2 0 0; 0 0 0 0 0 0 3 -2]';\r\nassert(isequal(block_diagonal(a,n),A_correct))\r\n\r\n%%\r\na = 6;\r\nn = 23;\r\nA_correct = a*eye(n);\r\nassert(isequal(block_diagonal(a,n),A_correct))\r\n\r\n%%\r\na = magic(5);\r\nn = 2;\r\nA_correct = [a zeros(5); zeros(5) a];\r\nassert(isequal(block_diagonal(a,n),A_correct))\r\n\r\n%%\r\na = randi(100,13,8);\r\nn = 1;\r\nA_correct = a;\r\nassert(isequal(block_diagonal(a,n),A_correct))\r\n","published":true,"deleted":false,"likes_count":5,"comments_count":2,"created_by":33611,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":168,"test_suite_updated_at":"2015-01-06T13:07:33.000Z","rescore_all_solutions":false,"group_id":31,"created_at":"2015-01-06T12:10:41.000Z","updated_at":"2026-03-04T16:27:51.000Z","published_at":"2015-01-06T13:05:19.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\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\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eA block diagonal matrix is a square matrix that can be written as\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[   A = [a  0  0  0\\n        0  b  0  0\\n        0  0  c  0\\n        0  0  0 ...]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ewhere\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ea\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e,\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eb\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e,\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ec\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e etc. are all square matrices.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eConstruct\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eA\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e such that\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[   A = [a  0  0  0\\n        0  a  0  0\\n        0  0  a  0\\n        0  0  0 ...]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ewhere\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ea\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e is allowed to be non-square or empty and occurs\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003en\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e times.\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003en\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e is always an integer greater than or equal to 0.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eExamples:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[   a = [1 2 3], n = 3]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003egives\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[   A = [1 2 3 0 0 0 0 0 0\\n        0 0 0 1 2 3 0 0 0\\n        0 0 0 0 0 0 1 2 3]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":8073,"title":"Matrix Construction I","description":"Given n, construct a matrix as shown in the example below.\r\n\r\nExample\r\n\r\nFor n=8, the output should look like this:\r\n\r\n 1 2 3 4 0 0 0 0\r\n 1 2 3 4 0 0 0 0\r\n 1 2 3 4 0 0 0 0\r\n 1 2 3 4 0 0 0 0\r\n 0 0 0 0 4 3 2 1\r\n 0 0 0 0 4 3 2 1\r\n 0 0 0 0 4 3 2 1\r\n 0 0 0 0 4 3 2 1","description_html":"\u003cp\u003eGiven n, construct a matrix as shown in the example below.\u003c/p\u003e\u003cp\u003eExample\u003c/p\u003e\u003cp\u003eFor n=8, the output should look like this:\u003c/p\u003e\u003cpre\u003e 1 2 3 4 0 0 0 0\r\n 1 2 3 4 0 0 0 0\r\n 1 2 3 4 0 0 0 0\r\n 1 2 3 4 0 0 0 0\r\n 0 0 0 0 4 3 2 1\r\n 0 0 0 0 4 3 2 1\r\n 0 0 0 0 4 3 2 1\r\n 0 0 0 0 4 3 2 1\u003c/pre\u003e","function_template":"function y = M_Const(n)\r\n\r\n\r\n\r\nend","test_suite":"%%\r\nn = 2;\r\ny = [1 0;0 1];\r\nassert(isequal(M_Const(n),y))\r\n%%\r\nn = 4;\r\ny = [1 2 0 0\r\n     1 2 0 0;\r\n     0 0 2 1\r\n     0 0 2 1];\r\nassert(isequal(M_Const(n),y))\r\n%%\r\nn = 6;\r\ny = [1 2 3 0 0 0\r\n     1 2 3 0 0 0\r\n     1 2 3 0 0 0\r\n     0 0 0 3 2 1\r\n     0 0 0 3 2 1\r\n     0 0 0 3 2 1];\r\nassert(isequal(M_Const(n),y))\r\n%%\r\nn = 8;\r\ny = [ 1 2 3 4 0 0 0 0\r\n      1 2 3 4 0 0 0 0\r\n      1 2 3 4 0 0 0 0\r\n      1 2 3 4 0 0 0 0\r\n      0 0 0 0 4 3 2 1\r\n      0 0 0 0 4 3 2 1\r\n      0 0 0 0 4 3 2 1\r\n      0 0 0 0 4 3 2 1];\r\nassert(isequal(M_Const(n),y))\r\n\r\n","published":true,"deleted":false,"likes_count":5,"comments_count":1,"created_by":22216,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":274,"test_suite_updated_at":"2015-04-08T14:45:46.000Z","rescore_all_solutions":false,"group_id":31,"created_at":"2015-04-08T14:32:07.000Z","updated_at":"2026-03-22T07:56:14.000Z","published_at":"2015-04-08T14:45:46.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\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\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eGiven n, construct a matrix as shown in the example below.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExample\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eFor n=8, the output should look like this:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ 1 2 3 4 0 0 0 0\\n 1 2 3 4 0 0 0 0\\n 1 2 3 4 0 0 0 0\\n 1 2 3 4 0 0 0 0\\n 0 0 0 0 4 3 2 1\\n 0 0 0 0 4 3 2 1\\n 0 0 0 0 4 3 2 1\\n 0 0 0 0 4 3 2 1]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"}],"no_progress_badge":{"id":53,"name":"Unknown","symbol":"unknown","description":"Partially completed groups","description_html":null,"image_location":"/images/responsive/supporting/matlabcentral/cody/badges/problem_groups_unknown_2.png","bonus":null,"players_count":0,"active":false,"created_by":null,"updated_by":null,"deleted_by":null,"deleted_at":null,"restored_by":null,"restored_at":null,"created_at":"2018-01-10T23:20:29.000Z","updated_at":"2018-01-10T23:20:29.000Z","community_badge_id":null,"award_multiples":false}}