{"group":{"group":{"id":29,"name":"Sequences \u0026 Series II","lockable":false,"created_at":"2017-06-13T17:54:56.000Z","updated_at":"2025-12-14T01:33:56.000Z","description":"Incrementally solve more problems based on sequences and series.","is_default":false,"created_by":26769,"badge_id":43,"featured":false,"trending":false,"solution_count_in_trending_period":382,"trending_last_calculated":"2025-12-14T00:00:00.000Z","image_id":408,"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\u003eIncrementally solve more problems based on sequences and series.\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=\"\"\u003eIncrementally solve more problems based on sequences and series.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","published_at":"2019-05-08T19:56:54.000Z"},"current_player":null},"problems":[{"id":2575,"title":"Sum of series I","description":"What is the sum of the following sequence:\r\n\r\n Σ(2k-1) for k=1...n\r\n\r\nfor different n?","description_html":"\u003cp\u003eWhat is the sum of the following sequence:\u003c/p\u003e\u003cpre\u003e Σ(2k-1) for k=1...n\u003c/pre\u003e\u003cp\u003efor different n?\u003c/p\u003e","function_template":"function s = sumOfSeriesI(n)\r\ns = n;\r\nend","test_suite":"%%\r\nn = 1;\r\ns_correct = 1;\r\nassert(isequal(sumOfSeriesI(n),s_correct))\r\n\r\n%%\r\nn = 2;\r\ns_correct = 4;\r\nassert(isequal(sumOfSeriesI(n),s_correct))\r\n\r\n%%\r\nn = 10;\r\ns_correct = 100;\r\nassert(isequal(sumOfSeriesI(n),s_correct))\r\n\r\n%%\r\nn = 15;\r\ns_correct = 225;\r\nassert(isequal(sumOfSeriesI(n),s_correct))\r\n\r\n%%\r\nn = 42;\r\ns_correct = 1764;\r\nassert(isequal(sumOfSeriesI(n),s_correct))\r\n\r\n%%\r\nn = 101;\r\ns_correct = 10201;\r\nassert(isequal(sumOfSeriesI(n),s_correct))\r\n\r\n%%\r\nn = 12345;\r\ns_correct = 152399025;\r\nassert(isequal(sumOfSeriesI(n),s_correct))","published":true,"deleted":false,"likes_count":14,"comments_count":1,"created_by":3062,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":2248,"test_suite_updated_at":"2017-06-13T17:57:57.000Z","rescore_all_solutions":false,"group_id":29,"created_at":"2014-09-10T09:37:47.000Z","updated_at":"2026-04-01T18:01:00.000Z","published_at":"2014-09-10T09:38:59.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\u003eWhat is the sum of the following sequence:\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[ Σ(2k-1) for k=1...n]]\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 different n?\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":2672,"title":"Largest Geometric Series","description":"Extension of Ned Gulley's wonderful Problem 317.\r\nIn a geometric series, ratio of adjacent elements is always a constant value. For example, [2 6 18 54] is a geometric series with a constant adjacent-element ratio of 3.\r\nA vector will be given. Find the largest geometric series that can be formed from the vector.\r\nExample:\r\n input = [2 4 8 16 1000 2000];\r\n output = [2 4 8 16]; \r\nUpdate - Test case added on 21/8/22\r\n","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: 243.867px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 407px 121.933px; transform-origin: 407px 121.933px; 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: 112.5px 8px; transform-origin: 112.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eExtension of Ned Gulley's wonderful\u003c/span\u003e\u003c/span\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: 2px 8px; transform-origin: 2px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://in.mathworks.com/matlabcentral/cody/problems/317\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003eProblem 317\u003c/span\u003e\u003c/span\u003e\u003c/a\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: 2px 8px; transform-origin: 2px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e.\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: 384px 8px; transform-origin: 384px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eIn a geometric series, ratio of adjacent elements is always a constant value. For example, [2 6 18 54] is a geometric series with a constant adjacent-element ratio of 3.\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: 286px 8px; transform-origin: 286px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eA vector will be given. Find the largest geometric series that can be formed from the vector.\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: 28.5px 8px; transform-origin: 28.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eExample:\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgb(247, 247, 247); block-size: 40.8667px; 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 20.4333px; transform-origin: 404px 20.4333px; 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: 120px 8.5px; tab-size: 4; transform-origin: 120px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e input = [2 4 8 16 1000 2000];\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: 88px 8.5px; tab-size: 4; transform-origin: 88px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e output = [2 4 8 16]; \u003c/span\u003e\u003c/span\u003e\u003c/div\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: 10px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 10px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; 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: 117px 8px; transform-origin: 117px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eUpdate - Test case added on 21/8/22\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: 0px 8px; transform-origin: 0px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function y = gSeries(x)\r\n\r\nend","test_suite":"%%\r\na = 2*3.^(1:3);\r\nb = 3*4.^(0:5);\r\nvec = [a b];\r\noutput = b;\r\ntest = gSeries(vec);\r\nassert(isequal(test,output));\r\n\r\n%%\r\na = ones(1,50);\r\nb = 3*4.^(1:5);\r\nvec = [a b];\r\noutput = a;\r\ntest = gSeries(vec);\r\nassert(isequal(test,output));\r\n\r\n%%\r\na = ones(1,50);\r\nb = randi(5,[1 10]);\r\np = randperm(60);\r\nvec = [a b];\r\nvec = vec(p);\r\noutput = nonzeros(vec==1)';\r\ntest = gSeries(vec);\r\nassert(isequal(test,output));\r\n\r\n%%\r\na = 2.^(1:15);\r\nb = 3.^(1:10);\r\nc = 5.^(1:10);\r\nvec = [a b c];\r\np = randperm(35);\r\nvec = vec(p);\r\noutput = a;\r\ntest = gSeries(vec);\r\nassert(isequal(test,output));\r\n\r\n%%\r\na = 2*3.^(1:10);\r\nvec = [a a];\r\np = randperm(20);\r\nvec = vec(p);\r\noutput = a;\r\ntest = gSeries(vec);\r\nassert(isequal(test,output));\r\n\r\n%%\r\na = 7.^(1:4);\r\nb = 2*3.^(1:2);\r\nc = 11.^(1:3);\r\nvec = [a b c];\r\nvec = vec(randperm(numel(vec)));\r\noutput = a;\r\ntest = gSeries(vec);\r\nassert(isequal(test,output));","published":true,"deleted":false,"likes_count":6,"comments_count":5,"created_by":17203,"edited_by":223089,"edited_at":"2022-10-31T05:02:40.000Z","deleted_by":null,"deleted_at":null,"solvers_count":126,"test_suite_updated_at":"2022-08-21T10:31:57.000Z","rescore_all_solutions":false,"group_id":29,"created_at":"2014-11-15T07:46:53.000Z","updated_at":"2026-03-27T18:35:36.000Z","published_at":"2014-11-15T07:47:30.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\u003eExtension of Ned Gulley's wonderful\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=\\\"https://in.mathworks.com/matlabcentral/cody/problems/317\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eProblem 317\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\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\u003eIn a geometric series, ratio of adjacent elements is always a constant value. For example, [2 6 18 54] is a geometric series with a constant adjacent-element ratio of 3.\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 vector will be given. Find the largest geometric series that can be formed from the vector.\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\u003eExample:\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 = [2 4 8 16 1000 2000];\\n output = [2 4 8 16]; ]]\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\u003eUpdate - Test case added on 21/8/22\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\u003e\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":2908,"title":"Approximation of Pi","description":"Pi (divided by 4) can be approximated by the following infinite series:\r\npi/4 = 1 - 1/3 + 1/5 - 1/7 + ...\r\nFor a given number of terms (n), return the difference between the actual value of pi and this approximation of the constant.\r\nAlso, try Problem 2909, a slightly harder variant of this problem.","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: 132px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 407px 66px; transform-origin: 407px 66px; 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: 215px 8px; transform-origin: 215px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003ePi (divided by 4) can be approximated by the following infinite series:\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: 88.5px 8px; transform-origin: 88.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003epi/4 = 1 - 1/3 + 1/5 - 1/7 + ...\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: 360px 8px; transform-origin: 360px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eFor a given number of terms (n), return the difference between the actual value of pi and this approximation of the constant.\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: 25.5px 8px; transform-origin: 25.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eAlso, try\u003c/span\u003e\u003c/span\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: 2px 8px; transform-origin: 2px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://www.mathworks.com/matlabcentral/cody/problems/2909-approximation-of-pi-vector-inputs\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003eProblem 2909\u003c/span\u003e\u003c/span\u003e\u003c/a\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: 128.5px 8px; transform-origin: 128.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e, a slightly harder variant of this problem.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function y = pi_approx(n)\r\n y = n;\r\nend","test_suite":"%%\r\nn = 1;\r\ny_correct = -0.858407346410207;\r\nassert(abs(pi_approx(n)-y_correct)\u003c(100*eps))\r\n\r\n%%\r\nn = 2;\r\ny_correct = 0.474925986923126;\r\nassert(abs(pi_approx(n)-y_correct)\u003c(100*eps)) \r\n\r\n%%\r\nn = 4;\r\ny_correct = 0.246354558351698;\r\nassert(abs(pi_approx(n)-y_correct)\u003c(100*eps))\r\n\r\n%%\r\nn = 7;\r\ny_correct = -0.142145830148691;\r\nassert(abs(pi_approx(n)-y_correct)\u003c(100*eps))\r\n\r\n%%\r\nn = 10;\r\ny_correct = 0.099753034660390;\r\nassert(abs(pi_approx(n)-y_correct)\u003c(100*eps))\r\n\r\n%%\r\nn = 14;\r\ny_correct = 0.071338035810608;\r\nassert(abs(pi_approx(n)-y_correct)\u003c(100*eps))\r\n\r\n%%\r\nn = 17;\r\ny_correct = -0.058772861819756;\r\nassert(abs(pi_approx(n)-y_correct)\u003c(100*eps))\r\n\r\n%%\r\nn = 20;\r\ny_correct = 0.049968846921953;\r\nassert(abs(pi_approx(n)-y_correct)\u003c(100*eps))\r\n\r\n%%\r\nn = 25;\r\ny_correct = -0.039984031845239;\r\nassert(abs(pi_approx(n)-y_correct)\u003c(100*eps))\r\n\r\n%%\r\nn = 42;\r\ny_correct = 0.023806151830915;\r\nassert(abs(pi_approx(n)-y_correct)\u003c(100*eps))\r\n","published":true,"deleted":false,"likes_count":18,"comments_count":0,"created_by":26769,"edited_by":223089,"edited_at":"2022-09-05T17:21:56.000Z","deleted_by":null,"deleted_at":null,"solvers_count":1406,"test_suite_updated_at":"2022-09-05T17:21:56.000Z","rescore_all_solutions":false,"group_id":29,"created_at":"2015-02-01T03:29:10.000Z","updated_at":"2026-04-04T17:54:57.000Z","published_at":"2015-02-01T03:29:10.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\u003ePi (divided by 4) can be approximated by the following infinite series:\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\u003epi/4 = 1 - 1/3 + 1/5 - 1/7 + ...\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 a given number of terms (n), return the difference between the actual value of pi and this approximation of the constant.\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\u003eAlso, try\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/2909-approximation-of-pi-vector-inputs\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eProblem 2909\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e, a slightly harder variant of this problem.\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":2576,"title":"Sum of series II","description":"What is the sum of the following sequence:\r\n\r\n Σ(2k-1)^2 for k=1...n\r\n\r\nfor different n?","description_html":"\u003cp\u003eWhat is the sum of the following sequence:\u003c/p\u003e\u003cpre\u003e Σ(2k-1)^2 for k=1...n\u003c/pre\u003e\u003cp\u003efor different n?\u003c/p\u003e","function_template":"function s = sumOfSeriesII(n)\r\n  s = n;\r\nend","test_suite":"%%\r\nn = 1;\r\ns_correct = 1;\r\nassert(isequal(sumOfSeriesII(n),s_correct))\r\n\r\n%%\r\nn = 2;\r\ns_correct = 10;\r\nassert(isequal(sumOfSeriesII(n),s_correct))\r\n\r\n%%\r\nn = 3;\r\ns_correct = 35;\r\nassert(isequal(sumOfSeriesII(n),s_correct))\r\n\r\n%%\r\nn = 4;\r\ns_correct = 84;\r\nassert(isequal(sumOfSeriesII(n),s_correct))\r\n\r\n%%\r\nn = 5;\r\ns_correct = 165;\r\nassert(isequal(sumOfSeriesII(n),s_correct))\r\n\r\n%%\r\nn = 11;\r\ns_correct = 1771;\r\nassert(isequal(sumOfSeriesII(n),s_correct))\r\n\r\n%%\r\nn = 42;\r\ns_correct = 98770;\r\nassert(isequal(sumOfSeriesII(n),s_correct))\r\n\r\n%%\r\nn = 71;\r\ns_correct = 477191;\r\nassert(isequal(sumOfSeriesII(n),s_correct))\r\n\r\n%%\r\nn = 123;\r\ns_correct = 2481115;\r\nassert(isequal(sumOfSeriesII(n),s_correct))","published":true,"deleted":false,"likes_count":11,"comments_count":2,"created_by":3062,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":1920,"test_suite_updated_at":"2017-06-13T18:00:48.000Z","rescore_all_solutions":false,"group_id":29,"created_at":"2014-09-10T09:44:53.000Z","updated_at":"2026-04-01T18:57:31.000Z","published_at":"2014-09-10T09:45:55.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\u003eWhat is the sum of the following sequence:\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[ Σ(2k-1)^2 for k=1...n]]\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 different n?\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":2577,"title":"Sum of series III","description":"What is the sum of the following sequence:\r\n\r\n Σ(2k-1)^3 for k=1...n\r\n\r\nfor different n?","description_html":"\u003cp\u003eWhat is the sum of the following sequence:\u003c/p\u003e\u003cpre\u003e Σ(2k-1)^3 for k=1...n\u003c/pre\u003e\u003cp\u003efor different n?\u003c/p\u003e","function_template":"function s = sumOfSeriesIII(n)\r\ns = n;\r\nend","test_suite":"%%\r\nn = 1;\r\ns_correct = 1;\r\nassert(isequal(sumOfSeriesIII(n),s_correct))\r\n\r\n%%\r\nn = 2;\r\ns_correct = 28;\r\nassert(isequal(sumOfSeriesIII(n),s_correct))\r\n\r\n%%\r\nn = 5;\r\ns_correct = 1225;\r\nassert(isequal(sumOfSeriesIII(n),s_correct))\r\n\r\n%%\r\nn = 10;\r\ns_correct = 19900;\r\nassert(isequal(sumOfSeriesIII(n),s_correct))\r\n\r\n%%\r\nn = 42;\r\ns_correct = 6221628;\r\nassert(isequal(sumOfSeriesIII(n),s_correct))\r\n\r\n%%\r\nn = 99;\r\ns_correct = 192109401;\r\nassert(isequal(sumOfSeriesIII(n),s_correct))\r\n\r\n%%\r\nn = 124;\r\ns_correct = 472827376;\r\nassert(isequal(sumOfSeriesIII(n),s_correct))\r\n\r\n%%\r\nn = 222;\r\ns_correct = 4857776028;\r\nassert(isequal(sumOfSeriesIII(n),s_correct))","published":true,"deleted":false,"likes_count":4,"comments_count":2,"created_by":3062,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":1823,"test_suite_updated_at":"2017-06-13T18:03:10.000Z","rescore_all_solutions":false,"group_id":29,"created_at":"2014-09-10T09:51:33.000Z","updated_at":"2026-04-01T18:58:28.000Z","published_at":"2014-09-10T09:52:07.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\u003eWhat is the sum of the following sequence:\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[ Σ(2k-1)^3 for k=1...n]]\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 different n?\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":2578,"title":"Sum of series IV","description":"What is the sum of the following sequence:\r\n\r\n Σ(-1)^(k+1) (2k-1)^2 for k=1...n\r\n\r\nfor different n?","description_html":"\u003cp\u003eWhat is the sum of the following sequence:\u003c/p\u003e\u003cpre\u003e Σ(-1)^(k+1) (2k-1)^2 for k=1...n\u003c/pre\u003e\u003cp\u003efor different n?\u003c/p\u003e","function_template":"function s = sumOfSeriesIV(n)\r\n  s = n;\r\nend","test_suite":"%%\r\nn = 1;\r\ns_correct = 1;\r\nassert(isequal(sumOfSeriesIV(n),s_correct))\r\n\r\n%%\r\nn = 2;\r\ns_correct = -8;\r\nassert(isequal(sumOfSeriesIV(n),s_correct))\r\n\r\n%%\r\nn = 3;\r\ns_correct = 17;\r\nassert(isequal(sumOfSeriesIV(n),s_correct))\r\n\r\n%%\r\nn = 4;\r\ns_correct = -32;\r\nassert(isequal(sumOfSeriesIV(n),s_correct))\r\n\r\n%%\r\nn = 7;\r\ns_correct = 97;\r\nassert(isequal(sumOfSeriesIV(n),s_correct))\r\n\r\n%%\r\nn = 12;\r\ns_correct = -288;\r\nassert(isequal(sumOfSeriesIV(n),s_correct))\r\n\r\n%%\r\nn = 33;\r\ns_correct = 2177;\r\nassert(isequal(sumOfSeriesIV(n),s_correct))\r\n\r\n%%\r\nn = 59;\r\ns_correct = 6961;\r\nassert(isequal(sumOfSeriesIV(n),s_correct))","published":true,"deleted":false,"likes_count":7,"comments_count":1,"created_by":3062,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":1718,"test_suite_updated_at":"2017-06-13T18:05:44.000Z","rescore_all_solutions":false,"group_id":29,"created_at":"2014-09-10T10:01:03.000Z","updated_at":"2026-03-26T01:04:02.000Z","published_at":"2014-09-10T10:01:33.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\u003eWhat is the sum of the following sequence:\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)^(k+1) (2k-1)^2 for k=1...n]]\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 different n?\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":2579,"title":"Sum of series V","description":"What is the sum of the following sequence:\r\n\r\n Σk(k+1) for k=1...n\r\n\r\nfor different n?","description_html":"\u003cp\u003eWhat is the sum of the following sequence:\u003c/p\u003e\u003cpre\u003e Σk(k+1) for k=1...n\u003c/pre\u003e\u003cp\u003efor different n?\u003c/p\u003e","function_template":"function s = sumOfSeriesV(n)\r\ns = n;\r\nend","test_suite":"%%\r\nn = 1;\r\ns_correct = 2;\r\nassert(isequal(sumOfSeriesV(n),s_correct))\r\n\r\n%%\r\nn = 3;\r\ns_correct = 20;\r\nassert(isequal(sumOfSeriesV(n),s_correct))\r\n\r\n%%\r\nn = 4;\r\ns_correct = 40;\r\nassert(isequal(sumOfSeriesV(n),s_correct))\r\n\r\n%%\r\nn = 10;\r\ns_correct = 440;\r\nassert(isequal(sumOfSeriesV(n),s_correct))\r\n\r\n%%\r\nn = 21;\r\ns_correct = 3542;\r\nassert(isequal(sumOfSeriesV(n),s_correct))\r\n\r\n%%\r\nn = 42;\r\ns_correct = 26488;\r\nassert(isequal(sumOfSeriesV(n),s_correct))\r\n\r\n%%\r\nn = 88;\r\ns_correct = 234960;\r\nassert(isequal(sumOfSeriesV(n),s_correct))\r\n\r\n%%\r\nn = 99;\r\ns_correct = 333300;\r\nassert(isequal(sumOfSeriesV(n),s_correct))","published":true,"deleted":false,"likes_count":7,"comments_count":1,"created_by":3062,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":1715,"test_suite_updated_at":"2017-06-13T18:07:53.000Z","rescore_all_solutions":false,"group_id":29,"created_at":"2014-09-10T10:08:14.000Z","updated_at":"2026-03-28T09:27:09.000Z","published_at":"2014-09-10T10:08:47.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\u003eWhat is the sum of the following sequence:\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(k+1) for k=1...n]]\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 different n?\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":2580,"title":"Sum of series VI","description":"What is the sum of the following sequence:\r\n\r\n Σk⋅k! for k=1...n\r\n\r\nfor different n?","description_html":"\u003cp\u003eWhat is the sum of the following sequence:\u003c/p\u003e\u003cpre\u003e Σk⋅k! for k=1...n\u003c/pre\u003e\u003cp\u003efor different n?\u003c/p\u003e","function_template":"function s = sumOfSeriesVI(n)\r\ns = n;\r\nend","test_suite":"%%\r\nn = 1;\r\ns_correct = 1;\r\nassert(isequal(sumOfSeriesVI(n),s_correct))\r\n\r\n%%\r\nn = 2;\r\ns_correct = 5;\r\nassert(isequal(sumOfSeriesVI(n),s_correct))\r\n\r\n%%\r\nn = 3;\r\ns_correct = 23;\r\nassert(isequal(sumOfSeriesVI(n),s_correct))\r\n\r\n%%\r\nn = 5;\r\ns_correct = 719;\r\nassert(isequal(sumOfSeriesVI(n),s_correct))\r\n\r\n%%\r\nn = 8;\r\ns_correct = 362879;\r\nassert(isequal(sumOfSeriesVI(n),s_correct))\r\n\r\n%%\r\nn = 10;\r\ns_correct = 39916799;\r\nassert(isequal(sumOfSeriesVI(n),s_correct))\r\n\r\n%%\r\nn = 12;\r\ns_correct = 6227020799;\r\nassert(isequal(sumOfSeriesVI(n),s_correct))\r\n\r\n%%\r\nn = 15;\r\ns_correct = 20922789887999;\r\nassert(isequal(sumOfSeriesVI(n),s_correct))","published":true,"deleted":false,"likes_count":7,"comments_count":1,"created_by":3062,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":1627,"test_suite_updated_at":"2017-06-13T18:10:27.000Z","rescore_all_solutions":false,"group_id":29,"created_at":"2014-09-10T10:14:09.000Z","updated_at":"2026-03-28T09:16:37.000Z","published_at":"2014-09-10T10:14:54.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\u003eWhat is the sum of the following sequence:\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⋅k! for k=1...n]]\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 different n?\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":2581,"title":"Sum of series VII","description":"What is the sum of the following sequence:\r\n\r\n Σ(km^k)/(k+m)! for k=1...n\r\n\r\nfor different n and m?","description_html":"\u003cp\u003eWhat is the sum of the following sequence:\u003c/p\u003e\u003cpre\u003e Σ(km^k)/(k+m)! for k=1...n\u003c/pre\u003e\u003cp\u003efor different n and m?\u003c/p\u003e","function_template":"function s = sumOfSeriesVII(n,m)\r\ns = n+m;\r\nend","test_suite":"%%\r\nn = 1; m = 1;\r\ns_correct = 1/2;\r\nassert(isequal(sumOfSeriesVII(n,m),s_correct))\r\n\r\n%%\r\nn = 1; m = 2;\r\ns_correct = 1/3;\r\nassert(abs(sumOfSeriesVII(n,m)-s_correct)\u003ceps)\r\n\r\n%%\r\nn = 3; m = 3;\r\ns_correct = 0.3875;\r\nassert(abs(sumOfSeriesVII(n,m)-s_correct)\u003ceps)\r\n\r\n%%\r\nn = 4; m = 2;\r\ns_correct = 0.955555555555556;\r\nassert(abs(sumOfSeriesVII(n,m)-s_correct)\u003c10*eps)\r\n\r\n%%\r\nn = 7; m = 5;\r\ns_correct = 0.0408511683468281;\r\nassert(abs(sumOfSeriesVII(n,m)-s_correct)\u003c10*eps)\r\n\r\n%%\r\nn = 5; m = 7;\r\ns_correct = 0.00114327593060232;\r\nassert(abs(sumOfSeriesVII(n,m)-s_correct)\u003c10*eps)\r\n\r\n%%\r\nn = 10; m = 3;\r\ns_correct = 0.499971551885614;\r\nassert(abs(sumOfSeriesVII(n,m)-s_correct)\u003c10*eps)\r\n\r\n%%\r\nn = 14; m = 4;\r\ns_correct = 0.166666498956709;\r\nassert(abs(sumOfSeriesVII(n,m)-s_correct)\u003c10*eps)\r\n\r\n%%\r\nn = 15; m = 11;\r\ns_correct = 2.75459255461393e-07;\r\nassert(abs(sumOfSeriesVII(n,m)-s_correct)\u003c10*eps)","published":true,"deleted":false,"likes_count":17,"comments_count":6,"created_by":3062,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":1601,"test_suite_updated_at":"2017-06-13T18:13:20.000Z","rescore_all_solutions":false,"group_id":29,"created_at":"2014-09-10T10:26:13.000Z","updated_at":"2026-04-03T06:43:32.000Z","published_at":"2014-09-10T10:26:41.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\u003eWhat is the sum of the following sequence:\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[ Σ(km^k)/(k+m)! for k=1...n]]\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 different n and m?\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":2607,"title":"Generate Square Wave","description":"Generate a square wave of desired length, number of complete cycles and duty cycle. Here, duty cycle is defined as the fraction of a complete cycle for which the cycle is at high value. Loops are not allowed.","description_html":"\u003cp\u003eGenerate a square wave of desired length, number of complete cycles and duty cycle. Here, duty cycle is defined as the fraction of a complete cycle for which the cycle is at high value. Loops are not allowed.\u003c/p\u003e","function_template":"function y = genSq(len,number_of_cycle,duty)\r\n\r\n\r\n  \r\nend","test_suite":"%%\r\nlen = 10;\r\nnum_cycle = 5;\r\nduty = 0.5;\r\ny_correct = [1 0 1 0 1 0 1 0 1 0];\r\nassert(isequal(genSq(len,num_cycle,duty),y_correct))\r\n\r\n%%\r\nlen = 20;\r\nnum_cycle = 4;\r\nduty = .2;\r\ny_correct = [1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0];\r\nassert(isequal(genSq(len,num_cycle,duty),y_correct))\r\n\r\n%%\r\nlen = 10;\r\nnum_cycle = 1;\r\nduty = 1;\r\ny_correct = ones(1,10);\r\nassert(isequal(genSq(len,num_cycle,duty),y_correct))\r\n\r\n%%\r\nlen = 10;\r\nnum_cycle = 1;\r\nduty = 0;\r\ny_correct = zeros(1,10);\r\nassert(isequal(genSq(len,num_cycle,duty),y_correct))\r\n\r\n%%\r\ntxt = fileread('genSq.m');\r\nassert(isempty(strfind(txt,'for')));\r\nassert(isempty(strfind(txt,'while')));\r\nassert(isempty(strfind(txt,'if')));","published":true,"deleted":false,"likes_count":7,"comments_count":0,"created_by":17203,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":261,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":29,"created_at":"2014-09-27T12:46:30.000Z","updated_at":"2026-04-01T09:51:59.000Z","published_at":"2014-09-27T12:46: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\",\"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\u003eGenerate a square wave of desired length, number of complete cycles and duty cycle. Here, duty cycle is defined as the fraction of a complete cycle for which the cycle is at high value. 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":2640,"title":"Find similar sequences","description":"Another problem inspired by a question on the \u003chttp://www.mathworks.com/matlabcentral/answers answers\u003e forum.\r\n\r\nGiven a matrix of positive integer numbers, find all the rows that are similar to the first rows and return these rows as a new matrix.\r\n\r\nRows are considered similar if the numbers common to both rows are in the exact same order with no other numbers in between. 0s in a row are always ignored and only occur at the end of the row.\r\n\r\nFor example:\r\n\r\n [3 1 5 0 0] and [4 2 1 5 0] are similar (1 5 are the common numbers and occur in the same order)\r\n [3 1 5 0 0] and [3 4 1 5 0] are not similar (3 1 5 are the common numbers, there's a 4 in between)\r\n ","description_html":"\u003cp\u003eAnother problem inspired by a question on the \u003ca href = \"http://www.mathworks.com/matlabcentral/answers\"\u003eanswers\u003c/a\u003e forum.\u003c/p\u003e\u003cp\u003eGiven a matrix of positive integer numbers, find all the rows that are similar to the first rows and return these rows as a new matrix.\u003c/p\u003e\u003cp\u003eRows are considered similar if the numbers common to both rows are in the exact same order with no other numbers in between. 0s in a row are always ignored and only occur at the end of the row.\u003c/p\u003e\u003cp\u003eFor example:\u003c/p\u003e\u003cpre\u003e [3 1 5 0 0] and [4 2 1 5 0] are similar (1 5 are the common numbers and occur in the same order)\r\n [3 1 5 0 0] and [3 4 1 5 0] are not similar (3 1 5 are the common numbers, there's a 4 in between)\u003c/pre\u003e","function_template":"function rows = findsimilar(m)\r\n  rows = [];\r\nend","test_suite":"%%\r\nm = [3 1 5 0 0\r\n     3 4 1 5 0\r\n     4 2 1 5 0];\r\nsrows = [3 1 5 0 0;4 2 1 5 0];\r\nassert(isequal(findsimilar(m),srows))\r\n\r\n%%\r\nm = [3 1 5 0 0\r\n     1 2 5 0 0\r\n     1 3 4 1 5\r\n     2 1 5 0 0];\r\nsrows = [3 1 5 0 0; 2 1 5 0 0];\r\nassert(isequal(findsimilar(m),srows))\r\n\r\n%%\r\nm = [3 1 5 7 0\r\n     3 2 5 7 0\r\n     3 5 7 2 0\r\n     1 5 7 2 0\r\n     4 6 7 8 9\r\n     4 5 7 8 0\r\n     4 5 6 7 8];\r\nsrows = [3 1 5 7 0;1 5 7 2 0;4 6 7 8 9;4 5 7 8 0];\r\nassert(isequal(findsimilar(m),srows))\r\n\r\n%%\r\nm = [3 1 5 0 0\r\n     3 1 6 0 0\r\n     3 2 6 0 0\r\n     2 1 5 6 0];\r\nsrows = m;\r\nassert(isequal(findsimilar(m), srows))","published":true,"deleted":false,"likes_count":1,"comments_count":8,"created_by":999,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":140,"test_suite_updated_at":"2014-10-24T06:14:05.000Z","rescore_all_solutions":false,"group_id":29,"created_at":"2014-10-23T08:06:10.000Z","updated_at":"2026-03-17T14:55:43.000Z","published_at":"2014-10-23T08:06:26.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\u003eAnother problem inspired by a question on the\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://www.mathworks.com/matlabcentral/answers\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eanswers\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e forum.\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\u003eGiven a matrix of positive integer numbers, find all the rows that are similar to the first rows and return these rows as a new matrix.\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\u003eRows are considered similar if the numbers common to both rows are in the exact same order with no other numbers in between. 0s in a row are always ignored and only occur at the end of the row.\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 example:\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 5 0 0] and [4 2 1 5 0] are similar (1 5 are the common numbers and occur in the same order)\\n [3 1 5 0 0] and [3 4 1 5 0] are not similar (3 1 5 are the common numbers, there's a 4 in between)]]\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":2672,"title":"Largest Geometric Series","description":"Extension of Ned Gulley's wonderful Problem 317.\r\nIn a geometric series, ratio of adjacent elements is always a constant value. For example, [2 6 18 54] is a geometric series with a constant adjacent-element ratio of 3.\r\nA vector will be given. Find the largest geometric series that can be formed from the vector.\r\nExample:\r\n input = [2 4 8 16 1000 2000];\r\n output = [2 4 8 16]; \r\nUpdate - Test case added on 21/8/22\r\n","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: 243.867px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 407px 121.933px; transform-origin: 407px 121.933px; 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: 112.5px 8px; transform-origin: 112.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eExtension of Ned Gulley's wonderful\u003c/span\u003e\u003c/span\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: 2px 8px; transform-origin: 2px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://in.mathworks.com/matlabcentral/cody/problems/317\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003eProblem 317\u003c/span\u003e\u003c/span\u003e\u003c/a\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: 2px 8px; transform-origin: 2px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e.\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: 384px 8px; transform-origin: 384px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eIn a geometric series, ratio of adjacent elements is always a constant value. For example, [2 6 18 54] is a geometric series with a constant adjacent-element ratio of 3.\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: 286px 8px; transform-origin: 286px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eA vector will be given. Find the largest geometric series that can be formed from the vector.\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: 28.5px 8px; transform-origin: 28.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eExample:\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgb(247, 247, 247); block-size: 40.8667px; 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 20.4333px; transform-origin: 404px 20.4333px; 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: 120px 8.5px; tab-size: 4; transform-origin: 120px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e input = [2 4 8 16 1000 2000];\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: 88px 8.5px; tab-size: 4; transform-origin: 88px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e output = [2 4 8 16]; \u003c/span\u003e\u003c/span\u003e\u003c/div\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: 10px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 10px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; 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: 117px 8px; transform-origin: 117px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eUpdate - Test case added on 21/8/22\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: 0px 8px; transform-origin: 0px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function y = gSeries(x)\r\n\r\nend","test_suite":"%%\r\na = 2*3.^(1:3);\r\nb = 3*4.^(0:5);\r\nvec = [a b];\r\noutput = b;\r\ntest = gSeries(vec);\r\nassert(isequal(test,output));\r\n\r\n%%\r\na = ones(1,50);\r\nb = 3*4.^(1:5);\r\nvec = [a b];\r\noutput = a;\r\ntest = gSeries(vec);\r\nassert(isequal(test,output));\r\n\r\n%%\r\na = ones(1,50);\r\nb = randi(5,[1 10]);\r\np = randperm(60);\r\nvec = [a b];\r\nvec = vec(p);\r\noutput = nonzeros(vec==1)';\r\ntest = gSeries(vec);\r\nassert(isequal(test,output));\r\n\r\n%%\r\na = 2.^(1:15);\r\nb = 3.^(1:10);\r\nc = 5.^(1:10);\r\nvec = [a b c];\r\np = randperm(35);\r\nvec = vec(p);\r\noutput = a;\r\ntest = gSeries(vec);\r\nassert(isequal(test,output));\r\n\r\n%%\r\na = 2*3.^(1:10);\r\nvec = [a a];\r\np = randperm(20);\r\nvec = vec(p);\r\noutput = a;\r\ntest = gSeries(vec);\r\nassert(isequal(test,output));\r\n\r\n%%\r\na = 7.^(1:4);\r\nb = 2*3.^(1:2);\r\nc = 11.^(1:3);\r\nvec = [a b c];\r\nvec = vec(randperm(numel(vec)));\r\noutput = a;\r\ntest = gSeries(vec);\r\nassert(isequal(test,output));","published":true,"deleted":false,"likes_count":6,"comments_count":5,"created_by":17203,"edited_by":223089,"edited_at":"2022-10-31T05:02:40.000Z","deleted_by":null,"deleted_at":null,"solvers_count":126,"test_suite_updated_at":"2022-08-21T10:31:57.000Z","rescore_all_solutions":false,"group_id":29,"created_at":"2014-11-15T07:46:53.000Z","updated_at":"2026-03-27T18:35:36.000Z","published_at":"2014-11-15T07:47:30.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\u003eExtension of Ned Gulley's wonderful\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=\\\"https://in.mathworks.com/matlabcentral/cody/problems/317\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eProblem 317\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\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\u003eIn a geometric series, ratio of adjacent elements is always a constant value. For example, [2 6 18 54] is a geometric series with a constant adjacent-element ratio of 3.\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 vector will be given. Find the largest geometric series that can be formed from the vector.\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\u003eExample:\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 = [2 4 8 16 1000 2000];\\n output = [2 4 8 16]; ]]\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\u003eUpdate - Test case added on 21/8/22\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\u003e\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":2800,"title":"arithmetic progression","description":"I've written a program to generate the first few terms of \u003chttps://en.wikipedia.org/wiki/Arithmetic_progression arithmetic progressions\u003e. I've noticed something odd though, there's always one wrong term. Surely, there couldn't be a bug in my code, could it?\r\n\r\nCan you tell me the position of the wrong term, and return the correct sequence?\r\n\r\nFor example, given\r\n\r\n  errorsequence = [2 4 7 8 10]; %arithmetic sequence starting at 2 with increment 2\r\n\r\nthen\r\n\r\n  errorposition = 3;\r\n  truesequence = [2 4 6 8 10]; ","description_html":"\u003cp\u003eI've written a program to generate the first few terms of \u003ca href = \"https://en.wikipedia.org/wiki/Arithmetic_progression\"\u003earithmetic progressions\u003c/a\u003e. I've noticed something odd though, there's always one wrong term. Surely, there couldn't be a bug in my code, could it?\u003c/p\u003e\u003cp\u003eCan you tell me the position of the wrong term, and return the correct sequence?\u003c/p\u003e\u003cp\u003eFor example, given\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eerrorsequence = [2 4 7 8 10]; %arithmetic sequence starting at 2 with increment 2\r\n\u003c/pre\u003e\u003cp\u003ethen\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eerrorposition = 3;\r\ntruesequence = [2 4 6 8 10]; \r\n\u003c/pre\u003e","function_template":"function [errorposition, truesequence] = find_error(errorsequence)\r\n  errorposition = Inf;\r\n  truesequence = errorsequence;\r\nend","test_suite":"%% test 1\r\nnterms = 10;\r\nterm0 = randi(10);\r\nincrement = (-1)^randi(2)*randi(10);\r\ncorrectsequence = term0:increment:term0+(nterms-1)*increment;\r\nfor position = 1:nterms\r\n   errorsequence = correctsequence;\r\n   errorsequence(position) = errorsequence(position) + (-1)^randi(2)*randi(50);\r\n   [errorposition, truesequence] = find_error(errorsequence);\r\n   assert(errorposition == position \u0026\u0026 isequal(truesequence, correctsequence), 'failed test 1 at position %d', position);\r\nend\r\n\r\n%%test 2\r\nnterms = 201;\r\nterm0 = randi(10);\r\nincrement = (-1)^randi(2)*randi(10);\r\ncorrectsequence = term0:increment:term0+(nterms-1)*increment;\r\nfor position = 1:10:nterms\r\n   errorsequence = correctsequence;\r\n   errorsequence(position) = errorsequence(position) + (-1)^randi(2)*randi(50);\r\n   [errorposition, truesequence] = find_error(errorsequence);\r\n   assert(errorposition == position \u0026\u0026 isequal(truesequence, correctsequence), 'failed test 2 at position %d', position);\r\nend\r\n","published":true,"deleted":false,"likes_count":10,"comments_count":0,"created_by":999,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":154,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":29,"created_at":"2014-12-27T08:02:39.000Z","updated_at":"2026-03-17T15:10:07.000Z","published_at":"2014-12-27T08:03:23.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\u003eI've written a program to generate the first few terms of\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=\\\"https://en.wikipedia.org/wiki/Arithmetic_progression\\\"\u003e\u003cw:r\u003e\u003cw:t\u003earithmetic progressions\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e. I've noticed something odd though, there's always one wrong term. Surely, there couldn't be a bug in my code, could it?\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\u003eCan you tell me the position of the wrong term, and return the correct sequence?\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 example, given\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[errorsequence = [2 4 7 8 10]; %arithmetic sequence starting at 2 with increment 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\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[errorposition = 3;\\ntruesequence = [2 4 6 8 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":2801,"title":"geometric progression","description":"I've modified my \u003chttp://uk.mathworks.com/matlabcentral/cody/problems/2800-arithmetic-progression previous program\u003e so that it now generates \u003chttps://en.wikipedia.org/wiki/Geometric_progression geometric progressions\u003e. For some reason, there's still always one wrong term. I'll blame it on my computer memory, it can't be my programming.\r\n\r\nOnce again, can you tell me the position of the wrong term, and return the correct sequence?\r\n\r\nFor example, given\r\n\r\n  errorsequence = [2 6 18 25 162]; %geometric sequence starting at 2 with ratio of 3\r\n\r\nthen\r\n\r\n  errorposition = 4;\r\n  truesequence = [2 6 18 54 162]; \r\n\r\n*Note:* The progression starts and ratio are integer only, so the sequence you return is expected to be made up of integers only.\r\n","description_html":"\u003cp\u003eI've modified my \u003ca href = \"http://uk.mathworks.com/matlabcentral/cody/problems/2800-arithmetic-progression\"\u003eprevious program\u003c/a\u003e so that it now generates \u003ca href = \"https://en.wikipedia.org/wiki/Geometric_progression\"\u003egeometric progressions\u003c/a\u003e. For some reason, there's still always one wrong term. I'll blame it on my computer memory, it can't be my programming.\u003c/p\u003e\u003cp\u003eOnce again, can you tell me the position of the wrong term, and return the correct sequence?\u003c/p\u003e\u003cp\u003eFor example, given\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eerrorsequence = [2 6 18 25 162]; %geometric sequence starting at 2 with ratio of 3\r\n\u003c/pre\u003e\u003cp\u003ethen\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eerrorposition = 4;\r\ntruesequence = [2 6 18 54 162]; \r\n\u003c/pre\u003e\u003cp\u003e\u003cb\u003eNote:\u003c/b\u003e The progression starts and ratio are integer only, so the sequence you return is expected to be made up of integers only.\u003c/p\u003e","function_template":"function [errorposition, truesequence] = find_error(errorsequence)\r\n  errorposition = Inf;\r\n  truesequence = errorsequence;\r\nend","test_suite":"%% test 1\r\nnterms = 10;\r\nterm0 = randi(10)\r\nratio = (-1)^randi(2)*randi(10)\r\ncorrectsequence = term0*ratio.^(0:nterms-1);\r\nfor position = 1:nterms\r\n   errorsequence = correctsequence;\r\n   errorsequence(position) = errorsequence(position) + (-1)^randi(2)*randi(50);\r\n   [errorposition, truesequence] = find_error(errorsequence);\r\n   assert(errorposition == position \u0026\u0026 isequal(truesequence, correctsequence), 'failed test 1 at position %d', position);\r\nend\r\n\r\n%%test 2, fractional ratio\r\nnterms = 15;\r\nterm0 = randi(10)\r\nratio = (-1)^randi(2)*randi(10)\r\ncorrectsequence = term0*ratio.^(0:nterms-1);\r\nfor position = 1:nterms\r\n   errorsequence = correctsequence;\r\n   errorsequence(position) = errorsequence(position) + (-1)^randi(2)*randi(50);\r\n   [errorposition, truesequence] = find_error(errorsequence);\r\n   assert(errorposition == position \u0026\u0026 isequal(truesequence, correctsequence), 'failed test 2 at position %d', position);\r\nend\r\n","published":true,"deleted":false,"likes_count":3,"comments_count":0,"created_by":999,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":132,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":29,"created_at":"2014-12-27T08:50:19.000Z","updated_at":"2026-03-28T09:56:35.000Z","published_at":"2014-12-27T08:50: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\u003eI've modified my\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://uk.mathworks.com/matlabcentral/cody/problems/2800-arithmetic-progression\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eprevious program\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e so that it now generates\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=\\\"https://en.wikipedia.org/wiki/Geometric_progression\\\"\u003e\u003cw:r\u003e\u003cw:t\u003egeometric progressions\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e. For some reason, there's still always one wrong term. I'll blame it on my computer memory, it can't be my programming.\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\u003eOnce again, can you tell me the position of the wrong term, and return the correct sequence?\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 example, given\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[errorsequence = [2 6 18 25 162]; %geometric sequence starting at 2 with ratio of 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[errorposition = 4;\\ntruesequence = [2 6 18 54 162];]]\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:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eNote:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e The progression starts and ratio are integer only, so the sequence you return is expected to be made up of integers only.\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":2908,"title":"Approximation of Pi","description":"Pi (divided by 4) can be approximated by the following infinite series:\r\npi/4 = 1 - 1/3 + 1/5 - 1/7 + ...\r\nFor a given number of terms (n), return the difference between the actual value of pi and this approximation of the constant.\r\nAlso, try Problem 2909, a slightly harder variant of this problem.","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: 132px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 407px 66px; transform-origin: 407px 66px; 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: 215px 8px; transform-origin: 215px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003ePi (divided by 4) can be approximated by the following infinite series:\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: 88.5px 8px; transform-origin: 88.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003epi/4 = 1 - 1/3 + 1/5 - 1/7 + ...\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: 360px 8px; transform-origin: 360px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eFor a given number of terms (n), return the difference between the actual value of pi and this approximation of the constant.\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: 25.5px 8px; transform-origin: 25.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eAlso, try\u003c/span\u003e\u003c/span\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: 2px 8px; transform-origin: 2px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://www.mathworks.com/matlabcentral/cody/problems/2909-approximation-of-pi-vector-inputs\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003eProblem 2909\u003c/span\u003e\u003c/span\u003e\u003c/a\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: 128.5px 8px; transform-origin: 128.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e, a slightly harder variant of this problem.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function y = pi_approx(n)\r\n y = n;\r\nend","test_suite":"%%\r\nn = 1;\r\ny_correct = -0.858407346410207;\r\nassert(abs(pi_approx(n)-y_correct)\u003c(100*eps))\r\n\r\n%%\r\nn = 2;\r\ny_correct = 0.474925986923126;\r\nassert(abs(pi_approx(n)-y_correct)\u003c(100*eps)) \r\n\r\n%%\r\nn = 4;\r\ny_correct = 0.246354558351698;\r\nassert(abs(pi_approx(n)-y_correct)\u003c(100*eps))\r\n\r\n%%\r\nn = 7;\r\ny_correct = -0.142145830148691;\r\nassert(abs(pi_approx(n)-y_correct)\u003c(100*eps))\r\n\r\n%%\r\nn = 10;\r\ny_correct = 0.099753034660390;\r\nassert(abs(pi_approx(n)-y_correct)\u003c(100*eps))\r\n\r\n%%\r\nn = 14;\r\ny_correct = 0.071338035810608;\r\nassert(abs(pi_approx(n)-y_correct)\u003c(100*eps))\r\n\r\n%%\r\nn = 17;\r\ny_correct = -0.058772861819756;\r\nassert(abs(pi_approx(n)-y_correct)\u003c(100*eps))\r\n\r\n%%\r\nn = 20;\r\ny_correct = 0.049968846921953;\r\nassert(abs(pi_approx(n)-y_correct)\u003c(100*eps))\r\n\r\n%%\r\nn = 25;\r\ny_correct = -0.039984031845239;\r\nassert(abs(pi_approx(n)-y_correct)\u003c(100*eps))\r\n\r\n%%\r\nn = 42;\r\ny_correct = 0.023806151830915;\r\nassert(abs(pi_approx(n)-y_correct)\u003c(100*eps))\r\n","published":true,"deleted":false,"likes_count":18,"comments_count":0,"created_by":26769,"edited_by":223089,"edited_at":"2022-09-05T17:21:56.000Z","deleted_by":null,"deleted_at":null,"solvers_count":1406,"test_suite_updated_at":"2022-09-05T17:21:56.000Z","rescore_all_solutions":false,"group_id":29,"created_at":"2015-02-01T03:29:10.000Z","updated_at":"2026-04-04T17:54:57.000Z","published_at":"2015-02-01T03:29:10.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\u003ePi (divided by 4) can be approximated by the following infinite series:\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\u003epi/4 = 1 - 1/3 + 1/5 - 1/7 + ...\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 a given number of terms (n), return the difference between the actual value of pi and this approximation of the constant.\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\u003eAlso, try\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/2909-approximation-of-pi-vector-inputs\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eProblem 2909\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e, a slightly harder variant of this problem.\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":2909,"title":"Approximation of Pi (vector inputs)","description":"Pi (divided by 4) can be approximated by the following infinite series:\r\n\r\npi/4 = 1 - 1/3 + 1/5 - 1/7 + ...\r\n\r\nFor a given number of terms (n), return the difference between the actual value of pi and this approximation of the constant.\r\n\r\nThis problem is the same as \u003chttps://www.mathworks.com/matlabcentral/cody/problems/2908-approximation-of-pi Problem 2908\u003e, except that the test suite will pass vectors for the number of terms, rather than breaking each truncated infinite series into a separate test.","description_html":"\u003cp\u003ePi (divided by 4) can be approximated by the following infinite series:\u003c/p\u003e\u003cp\u003epi/4 = 1 - 1/3 + 1/5 - 1/7 + ...\u003c/p\u003e\u003cp\u003eFor a given number of terms (n), return the difference between the actual value of pi and this approximation of the constant.\u003c/p\u003e\u003cp\u003eThis problem is the same as \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/2908-approximation-of-pi\"\u003eProblem 2908\u003c/a\u003e, except that the test suite will pass vectors for the number of terms, rather than breaking each truncated infinite series into a separate test.\u003c/p\u003e","function_template":"function y = pi_approx(n)\r\n y = n;\r\nend","test_suite":"%%\r\nn = 1:5;\r\ny_correct = [-0.858407346410207 0.474925986923126 -0.325074013076874 0.246354558351698 -0.198089886092747];\r\nanswers = pi_approx(n);\r\nfor i = 1:numel(n)\r\n assert(abs(answers(i)-y_correct(i))\u003c(100*eps))\r\nend\r\n\r\n%%\r\nn = 2:2:10;\r\ny_correct = [0.474925986923126 0.246354558351698 0.165546477543617 0.124520836517975 0.099753034660390];\r\nanswers = pi_approx(n);\r\nfor i = 1:numel(n)\r\n assert(abs(answers(i)-y_correct(i))\u003c(100*eps))\r\nend\r\n\r\n%%\r\nn = 5:5:25;\r\ny_correct = [-0.198089886092747 0.099753034660390 -0.066592998672151 0.049968846921953 -0.039984031845239];\r\nanswers = pi_approx(n);\r\nfor i = 1:numel(n)\r\n assert(abs(answers(i)-y_correct(i))\u003c(100*eps))\r\nend\r\n\r\n%%\r\nn = 10:10:100;\r\ny_correct = [0.099753034660390 0.049968846921953 0.033324086890846 0.024996096795960 0.019998000998782 0.016665509660796 0.014284985608559 0.012499511814072 0.011110768228485 0.009999750031239];\r\nanswers = pi_approx(n);\r\nfor i = 1:numel(n)\r\n assert(abs(answers(i)-y_correct(i))\u003c(100*eps))\r\nend\r\n","published":true,"deleted":false,"likes_count":2,"comments_count":0,"created_by":26769,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":276,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":29,"created_at":"2015-02-01T03:45:00.000Z","updated_at":"2026-04-01T09:59:49.000Z","published_at":"2015-02-01T03:45: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\u003ePi (divided by 4) can be approximated by the following infinite series:\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\u003epi/4 = 1 - 1/3 + 1/5 - 1/7 + ...\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 a given number of terms (n), return the difference between the actual value of pi and this approximation of the constant.\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\u003eThis problem is the same as\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/2908-approximation-of-pi\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eProblem 2908\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e, except that the test suite will pass vectors for the number of terms, rather than breaking each truncated infinite series into a separate test.\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":2910,"title":"Mersenne Primes vs. All Primes","description":"A Mersenne prime (M) is a prime number of the form M = 2^p - 1, where p is another prime number. \u003chttps://www.mathworks.com/matlabcentral/cody/problems/525-mersenne-primes Problem 525\u003e asks the user to determine if a number is a Mersenne prime. In this problem, you are tasked with returning the number of primes numbers below the input number, n, that are Mersenne primes and the fraction of all primes below that input number that the Mersenne primes represent.\r\n\r\nFor example, for n = 100, there are 25 primes numbers: 2, 3, 5, 7, ..., 89, 97. As far as Mersenne primes go, there are only three that are less than 100: 2^2 - 1 = 3, 2^3 - 1 = 7, and 2^5 - 1 = 31. The corresponding fraction would be 3/25.","description_html":"\u003cp\u003eA Mersenne prime (M) is a prime number of the form M = 2^p - 1, where p is another prime number. \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/525-mersenne-primes\"\u003eProblem 525\u003c/a\u003e asks the user to determine if a number is a Mersenne prime. In this problem, you are tasked with returning the number of primes numbers below the input number, n, that are Mersenne primes and the fraction of all primes below that input number that the Mersenne primes represent.\u003c/p\u003e\u003cp\u003eFor example, for n = 100, there are 25 primes numbers: 2, 3, 5, 7, ..., 89, 97. As far as Mersenne primes go, there are only three that are less than 100: 2^2 - 1 = 3, 2^3 - 1 = 7, and 2^5 - 1 = 31. The corresponding fraction would be 3/25.\u003c/p\u003e","function_template":"function [y,f] = Mersenne_prime_comp(n)\r\n y = 1;\r\n f = 0;\r\nend","test_suite":"%%\r\nn = 1e2;\r\ny_correct = 3;\r\nf_correct = 3/25;\r\n[y,f] = Mersenne_prime_comp(n);\r\nassert(isequal(y,y_correct))\r\nassert(isequal(f,f_correct))\r\n\r\n%%\r\nn = 1e3;\r\ny_correct = 4;\r\nf_correct = 0.023809523809524;\r\n[y,f] = Mersenne_prime_comp(n);\r\nassert(isequal(y,y_correct))\r\nassert(abs(f-f_correct)\u003c(10*eps))\r\n\r\n%%\r\nn = 1e4;\r\ny_correct = 5;\r\nf_correct = 0.004068348250610;\r\n[y,f] = Mersenne_prime_comp(n);\r\nassert(isequal(y,y_correct))\r\nassert(abs(f-f_correct)\u003c(10*eps))\r\n\r\n%%\r\nn = 1e5;\r\ny_correct = 5;\r\nf_correct = 5.212677231025855e-04;\r\n[y,f] = Mersenne_prime_comp(n);\r\nassert(isequal(y,y_correct))\r\nassert(abs(f-f_correct)\u003c(10*eps))\r\n\r\n%%\r\nn = 1e6;\r\ny_correct = 7;\r\nf_correct = 8.917424647761727e-05;\r\n[y,f] = Mersenne_prime_comp(n);\r\nassert(isequal(y,y_correct))\r\nassert(abs(f-f_correct)\u003c(10*eps))\r\n","published":true,"deleted":false,"likes_count":8,"comments_count":2,"created_by":26769,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":846,"test_suite_updated_at":"2015-02-01T04:14:08.000Z","rescore_all_solutions":false,"group_id":29,"created_at":"2015-02-01T03:56:07.000Z","updated_at":"2026-04-01T10:02:01.000Z","published_at":"2015-02-01T04:14:08.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 Mersenne prime (M) is a prime number of the form M = 2^p - 1, where p is another prime number.\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=\\\"https://www.mathworks.com/matlabcentral/cody/problems/525-mersenne-primes\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eProblem 525\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e asks the user to determine if a number is a Mersenne prime. In this problem, you are tasked with returning the number of primes numbers below the input number, n, that are Mersenne primes and the fraction of all primes below that input number that the Mersenne primes represent.\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 example, for n = 100, there are 25 primes numbers: 2, 3, 5, 7, ..., 89, 97. As far as Mersenne primes go, there are only three that are less than 100: 2^2 - 1 = 3, 2^3 - 1 = 7, and 2^5 - 1 = 31. The corresponding fraction would be 3/25.\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}}