{"group":{"id":1,"name":"Community","lockable":false,"created_at":"2012-01-18T18:02:15.000Z","updated_at":"2026-04-06T14:01:22.000Z","description":"Problems submitted by members of the MATLAB Central community.","is_default":true,"created_by":161519,"badge_id":null,"featured":false,"trending":false,"solution_count_in_trending_period":0,"trending_last_calculated":"2026-04-06T00:00:00.000Z","image_id":null,"published":true,"community_created":false,"status_id":2,"is_default_group_for_player":false,"deleted_by":null,"deleted_at":null,"restored_by":null,"restored_at":null,"description_opc":null,"description_html":null,"published_at":null},"problems":[{"id":1763,"title":"Primes for Large N:  2^30, System Memory Limit","description":"This Challenge is to further improve the \"primes\" function for speed/memory usage to the Limit of Cody Memory.\r\n\r\nThe Matlab function \"primes\" has a very efficient sieving method but it suffers from memory usage issues, especially on limited memory systems.\r\n\r\nThe \u003chttp://www.mathworks.com/matlabcentral/cody/problems/1761-primes-faster-for-large-n Primes Faster Challenge\u003e gives the first half to maximizing the primes function for optimal memory usage and speed.\r\n\r\nCody appears to have 2GB of RAM based upon \"out of memory\" messages observed.\r\n\r\nThe test case of 2^30 pushes the upper limits of Cody memory.\r\n\r\nThe reference solution can process N=2^32 on an 8GB machine in 68sec.\r\n\r\n*Input:* N  (max of primes to find)\r\n\r\n*Output:* vector of primes  (all primes less than or equal to N)\r\n\r\n*Scoring:* Time to find all primes \u003c 2^30\r\n\r\n*Hints:*\r\n\r\n  1) Doubles use 8 bytes;\r\n  2) The primes method  p(((k*k+1)/2):k:q) creates a vector using q/k*8 bytes. This is 8/3 the size of p for k=3 because k is a double. \r\n  3) Segmentation will keep memory usage below limits at the cost of time.\r\n  4) Cody Available Memory appears to be 537.27MB. XP Operating system eats memory.\r\n  5) Usage of profiler and Task Manager combined give performance insights.","description_html":"\u003cp\u003eThis Challenge is to further improve the \"primes\" function for speed/memory usage to the Limit of Cody Memory.\u003c/p\u003e\u003cp\u003eThe Matlab function \"primes\" has a very efficient sieving method but it suffers from memory usage issues, especially on limited memory systems.\u003c/p\u003e\u003cp\u003eThe \u003ca href = \"http://www.mathworks.com/matlabcentral/cody/problems/1761-primes-faster-for-large-n\"\u003ePrimes Faster Challenge\u003c/a\u003e gives the first half to maximizing the primes function for optimal memory usage and speed.\u003c/p\u003e\u003cp\u003eCody appears to have 2GB of RAM based upon \"out of memory\" messages observed.\u003c/p\u003e\u003cp\u003eThe test case of 2^30 pushes the upper limits of Cody memory.\u003c/p\u003e\u003cp\u003eThe reference solution can process N=2^32 on an 8GB machine in 68sec.\u003c/p\u003e\u003cp\u003e\u003cb\u003eInput:\u003c/b\u003e N  (max of primes to find)\u003c/p\u003e\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e vector of primes  (all primes less than or equal to N)\u003c/p\u003e\u003cp\u003e\u003cb\u003eScoring:\u003c/b\u003e Time to find all primes \u0026lt; 2^30\u003c/p\u003e\u003cp\u003e\u003cb\u003eHints:\u003c/b\u003e\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e1) Doubles use 8 bytes;\r\n2) The primes method  p(((k*k+1)/2):k:q) creates a vector using q/k*8 bytes. This is 8/3 the size of p for k=3 because k is a double. \r\n3) Segmentation will keep memory usage below limits at the cost of time.\r\n4) Cody Available Memory appears to be 537.27MB. XP Operating system eats memory.\r\n5) Usage of profiler and Task Manager combined give performance insights.\r\n\u003c/pre\u003e","function_template":"function p = primes_faster_large(n)\r\n% Standard primes will not succeed\r\n  p=[];\r\nreturn\r\n\r\n%PRIMES Generate list of prime numbers.\r\n%   PRIMES(N) is a row vector of the prime numbers less than or \r\n%   equal to N.  A prime number is one that has no factors other\r\n%   than 1 and itself.\r\n%   Class support for input N:\r\n%      float: double, single\r\n%   See also FACTOR, ISPRIME.\r\n%   Copyright 1984-2004 The MathWorks, Inc. \r\n%   $Revision: 1.16.4.3 $  $Date: 2010/08/23 23:13:13 $\r\n\r\nif length(n)~=1 \r\n  error(message('MATLAB:primes:InputNotScalar')); \r\nend\r\nif n \u003c 2, p = zeros(1,0,class(n)); return, end\r\np = 1:2:n;\r\nq = length(p);\r\np(1) = 2;\r\nfor k = 3:2:sqrt(n)\r\n  if p((k+1)/2)\r\n     p(((k*k+1)/2):k:q) = 0;\r\n  end\r\nend\r\np = p(p\u003e0);\r\n\r\nend","test_suite":"feval(@assignin,'caller','score',55);\r\n%%\r\ntic\r\nassert(isequal(primes_faster_large(1),primes(1)))\r\nassert(isequal(primes_faster_large(2),primes(2)))\r\nfor i=1:10\r\n n=randi(2000,1);\r\n assert(isequal(primes_faster_large(n),primes(n)))\r\nend\r\ntoc\r\n%%\r\ntic\r\nta=clock;\r\n p = primes_faster_large(2^30);\r\nt1=etime(clock,ta); % time in sec\r\nfprintf('P 2^30 %12i %10.3f\\n',length(p),t1)\r\nassert(isequal(length(p),54400028))\r\nassert(all(diff(p)\u003e0))\r\n%assert(isequal(size(unique(p),2),54400028))\r\nptr=randi(7603553,1,10); % small to avoid timeout\r\npchk=double(p(ptr));\r\nassert(all(isprime(pchk)))\r\nfeval(  @assignin,'caller','score',floor(min(55,t1))  );\r\ntoc\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":7,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":26,"test_suite_updated_at":"2013-08-07T00:23:25.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2013-07-30T19:43:11.000Z","updated_at":"2025-06-07T08:19:24.000Z","published_at":"2013-07-31T04:12:38.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\u003eThis Challenge is to further improve the \\\"primes\\\" function for speed/memory usage to the Limit of Cody Memory.\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\u003eThe Matlab function \\\"primes\\\" has a very efficient sieving method but it suffers from memory usage issues, especially on limited memory systems.\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\u003eThe\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/cody/problems/1761-primes-faster-for-large-n\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ePrimes Faster Challenge\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e gives the first half to maximizing the primes function for optimal memory usage and speed.\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\u003eCody appears to have 2GB of RAM based upon \\\"out of memory\\\" messages observed.\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\u003eThe test case of 2^30 pushes the upper limits of Cody memory.\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\u003eThe reference solution can process N=2^32 on an 8GB machine in 68sec.\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\u003eInput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e N (max of primes to find)\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\u003eOutput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e vector of primes (all primes less than or equal to N)\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\u003eScoring:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Time to find all primes \u0026lt; 2^30\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\u003eHints:\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) Doubles use 8 bytes;\\n2) The primes method  p(((k*k+1)/2):k:q) creates a vector using q/k*8 bytes. This is 8/3 the size of p for k=3 because k is a double. \\n3) Segmentation will keep memory usage below limits at the cost of time.\\n4) Cody Available Memory appears to be 537.27MB. XP Operating system eats memory.\\n5) Usage of profiler and Task Manager combined give performance insights.]]\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":2260,"title":"GJam 2011 Africa Qual A: Maximum Loop Size","description":"This Challenge is derived from \u003chttp://code.google.com/codejam/contest/837485/dashboard GJam 2011 Africa: Closing the Loop\u003e. Small Case\r\nGoogle Code Jam 2014 Kicks Off its Qualifier round April 11. \u003chttp://code.google.com/codejam GJam Registration\u003e. The Test Suite, at the bottom, contains a full GJam file input read, process, and Output example.\r\n\r\nCreate the maximum length Loop of alternating Segment types (0 or 1). Return Maximum Length. Segment connection Knots reduce the Length by 1, including the end to the start knot. \r\nIf no valid loop can be created then return 0. \r\n\r\n*Input:* M, 2xN matrix of [Lengths(1:N);Types(1:N)], Types are 0 or 1\r\n\r\n*Output:* L, maximum Loop Length of valid alternating segment types \r\n\r\n*Example:*\r\nM=[6 1 7 3;1 0 1 0], L=13\r\n\r\n\r\n\r\nAdditional GJam solutions can be found at \u003chttp://go-hero.net/jam Example GJam Matlab solutions\u003e. Select Find Solutions, change Language to Matlab. There were only 50 Qualifier Matlab entrants in 2013 and a mere 2 Matlab users achieved round 2.","description_html":"\u003cp\u003eThis Challenge is derived from \u003ca href = \"http://code.google.com/codejam/contest/837485/dashboard\"\u003eGJam 2011 Africa: Closing the Loop\u003c/a\u003e. Small Case\r\nGoogle Code Jam 2014 Kicks Off its Qualifier round April 11. \u003ca href = \"http://code.google.com/codejam\"\u003eGJam Registration\u003c/a\u003e. The Test Suite, at the bottom, contains a full GJam file input read, process, and Output example.\u003c/p\u003e\u003cp\u003eCreate the maximum length Loop of alternating Segment types (0 or 1). Return Maximum Length. Segment connection Knots reduce the Length by 1, including the end to the start knot. \r\nIf no valid loop can be created then return 0.\u003c/p\u003e\u003cp\u003e\u003cb\u003eInput:\u003c/b\u003e M, 2xN matrix of [Lengths(1:N);Types(1:N)], Types are 0 or 1\u003c/p\u003e\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e L, maximum Loop Length of valid alternating segment types\u003c/p\u003e\u003cp\u003e\u003cb\u003eExample:\u003c/b\u003e\r\nM=[6 1 7 3;1 0 1 0], L=13\u003c/p\u003e\u003cp\u003eAdditional GJam solutions can be found at \u003ca href = \"http://go-hero.net/jam\"\u003eExample GJam Matlab solutions\u003c/a\u003e. Select Find Solutions, change Language to Matlab. There were only 50 Qualifier Matlab entrants in 2013 and a mere 2 Matlab users achieved round 2.\u003c/p\u003e","function_template":"function L=Loop(m)\r\n% L, Length of valid Loop with knots subtracted\r\n% m, [2,N] array\r\n\r\n L=0;\r\nend","test_suite":"%\r\nm=[5 ;1 ];\r\nLexp= 0;\r\nL=Loop(m);\r\nassert(isequal(Lexp,L))\r\n%%\r\nm=[6 1 7 3 ;0 1 0 1 ];\r\nLexp= 13;\r\nL=Loop(m);\r\nassert(isequal(Lexp,L))\r\n%%\r\nm=[5 4 3 2 5 4 3 ;1 0 0 0 0 0 0 ];\r\nLexp= 8;\r\nL=Loop(m);\r\nassert(isequal(Lexp,L))\r\n%%\r\nm=[46 39 3 40 12 36 83 24 61 66 19 87 38 62 27 12 66 39 48 25 89 3 13 80 61 4 76 26 99 89 62 79 25 45 19 5 3 97 1 20 97 86 73 89 26 62 22 41 13 55 28 87 95 16 41 30 62 44 11 34 48 47 33 51 22 15 42 32 33 91 21 57 26 78 66 65 78 97 57 97 19 78 27 23 8 14 37 47 59 38 43 90 96 3 92 59 42 99 92 32 30 6 19 20 62 84 25 87 83 42 51 80 10 16 15 23 40 3 76 13 39 78 94 3 21 94 87 21 74 64 55 43 8 4 46 24 93 69 78 53 28 59 95 29 79 54 64 81 26 5 61 51 47 92 31 53 43 28 79 63 68 14 69 95 35 16 30 34 77 67 65 69 82 79 63 65 82 82 62 98 7 58 29 70 93 14 7 82 20 99 88 77 39 59 65 35 62 51 13 51 37 59 91 84 34 50 62 88 67 12 82 13 75 66 31 69 97 71 21 70 6 53 71 47 40 39 56 5 76 80 60 38 63 89 18 79 55 16 53 7 61 97 88 19 68 97 52 94 90 60 60 29 53 88 50 11 57 81 75 50 28 58 92 1 14 74 20 92 45 52 97 37 10 52 8 93 17 34 76 40 59 34 67 73 73 36 4 71 90 33 71 12 92 98 88 10 6 6 90 15 4 66 72 39 60 69 38 7 20 81 11 98 51 79 71 25 36 60 96 70 46 22 35 27 88 3 30 81 28 53 88 16 57 25 23 14 98 38 16 66 10 5 59 72 19 4 63 73 30 22 61 1 10 73 1 18 49 32 91 12 18 83 42 34 50 36 75 37 34 69 30 10 25 36 38 18 13 5 22 89 56 95 32 68 54 86 80 3 37 30 98 49 85 49 2 7 68 9 96 54 43 1 87 56 66 30 43 54 46 71 90 76 1 35 26 16 97 48 52 73 8 3 5 21 89 15 40 60 12 14 90 15 42 37 27 3 75 52 82 16 77 39 12 85 18 76 81 81 19 6 2 19 33 41 58 17 54 41 2 37 39 43 69 76 37 45 77 67 96 38 34 52 67 1 84 77 11 8 36 19 18 79 13 89 67 99 77 15 47 16 9 89 36 84 56 33 97 95 80 93 47 90 75 88 76 61 16 26 8 75 60 61 23 37 98 29 62 17 13 59 8 38 12 4 50 64 44 9 4 55 53 91 79 29 49 23 8 42 76 82 43 55 46 39 59 12 5 40 36 94 29 89 13 39 91 92 71 35 97 6 25 42 95 38 57 88 84 80 17 38 39 77 45 66 27 42 51 72 35 7 88 18 60 20 98 78 10 32 56 14 25 82 58 3 65 19 11 75 66 21 51 28 58 92 95 16 77 37 89 36 44 70 72 40 4 64 50 69 8 31 16 64 69 6 17 71 82 31 18 68 59 80 51 24 82 63 40 36 33 40 42 48 86 48 10 88 61 17 3 53 69 85 39 44 79 39 61 53 96 32 59 52 79 96 97 86 43 34 35 28 79 17 13 ;1 0 0 0 1 1 0 0 0 1 1 1 1 0 0 1 0 1 0 0 1 0 0 0 0 0 0 1 1 0 0 0 1 1 1 1 1 1 0 0 0 0 1 1 0 1 0 0 0 1 0 0 1 1 0 0 0 0 1 0 0 0 0 1 1 0 0 0 0 1 1 1 1 1 0 0 0 0 1 1 0 1 1 1 1 0 1 1 0 0 1 0 0 0 1 0 1 0 0 1 1 0 1 1 0 0 0 1 1 0 1 1 1 0 1 1 0 1 1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 1 1 0 0 1 0 1 1 0 1 0 0 0 1 1 1 0 1 1 1 1 0 1 1 0 0 1 0 1 1 0 1 0 0 0 1 0 0 1 0 1 1 0 1 1 1 1 0 0 1 0 1 1 1 0 1 0 0 0 1 1 1 0 1 1 0 0 0 1 1 0 0 1 1 1 0 0 1 1 0 0 0 0 1 1 0 0 1 0 0 0 0 1 1 1 0 0 0 1 0 0 1 1 0 1 1 1 1 1 1 0 1 1 1 0 1 1 0 0 1 0 0 0 1 1 0 1 1 0 1 0 1 1 1 1 1 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 0 0 0 1 1 1 1 1 1 1 1 0 0 1 0 1 0 1 0 1 1 0 1 1 1 0 1 0 0 0 1 1 1 1 0 0 0 0 1 1 0 1 1 0 0 1 1 1 1 0 0 1 1 0 1 1 0 1 0 1 0 0 1 1 0 1 1 1 0 1 0 1 0 0 0 0 1 1 1 1 0 0 0 0 0 0 1 1 1 0 0 1 0 1 1 0 0 1 1 0 0 0 1 1 0 1 0 1 0 1 0 0 0 0 1 0 1 0 0 1 1 0 0 1 0 1 1 1 0 0 1 0 1 0 1 1 0 0 0 1 0 0 1 1 0 1 1 0 1 1 1 1 1 1 1 0 1 0 0 0 0 1 1 1 0 1 0 1 1 0 1 0 1 1 0 1 1 0 0 0 1 1 0 0 1 0 1 0 1 0 0 1 0 0 0 1 0 1 1 1 1 0 1 0 1 0 1 1 1 0 1 1 0 1 0 0 0 1 0 0 0 0 0 0 0 1 1 0 1 1 1 1 0 1 0 0 1 1 1 1 0 0 0 0 0 1 0 1 0 1 1 1 1 1 1 1 1 0 1 1 0 1 0 1 0 0 1 1 0 1 1 1 0 1 0 0 0 1 1 1 1 0 0 0 0 0 0 1 0 0 0 1 1 1 0 1 0 1 1 0 0 0 1 1 1 1 0 0 0 1 1 1 1 0 1 0 0 1 1 1 0 1 1 0 0 1 0 1 1 0 1 1 1 1 0 1 0 1 0 0 1 0 1 0 0 1 0 0 1 0 1 0 0 1 0 1 0 0 1 1 0 0 1 1 0 1 1 0 0 1 1 1 1 1 1 1 0 0 0 1 1 1 1 0 0 ];\r\nLexp= 32240;\r\nL=Loop(m);\r\nassert(isequal(Lexp,L))\r\n%%\r\nm=[60 44 41 80 31 96 12 58 31 13 61 58 55 64 83 6 13 23 18 47 22 44 70 8 31 99 85 80 80 39 33 65 43 67 33 25 82 37 79 84 28 66 74 26 68 54 33 35 2 3 64 8 44 17 70 7 24 78 36 78 46 38 91 26 95 98 58 90 11 32 96 5 63 31 7 93 10 35 12 70 85 2 9 24 83 64 22 74 53 77 98 57 52 88 6 15 90 77 94 94 12 69 80 50 78 10 71 80 95 95 58 79 6 90 15 59 22 96 56 53 65 53 46 23 3 81 34 63 50 95 2 87 40 38 73 1 23 91 42 57 73 71 57 32 25 60 1 79 98 27 71 77 32 93 31 74 53 40 33 39 72 93 43 59 58 90 91 52 29 2 37 8 13 84 29 33 79 23 85 78 75 48 83 80 35 88 61 2 37 72 1 7 78 69 32 76 26 53 93 40 50 95 27 25 86 7 83 14 63 66 52 44 54 83 61 48 31 40 61 98 57 69 32 44 42 76 41 74 64 87 32 19 21 92 6 80 54 85 17 66 75 57 95 81 61 60 62 11 51 36 51 62 10 55 60 12 54 35 50 16 28 94 31 91 6 76 7 26 44 16 58 41 96 99 94 73 53 46 63 6 54 60 96 13 74 76 18 55 34 27 67 67 73 82 35 72 56 27 78 51 49 65 79 73 45 32 40 9 97 43 12 68 25 65 2 86 46 63 70 74 88 18 69 58 37 77 76 31 57 19 91 87 86 45 31 83 55 85 26 56 94 10 48 86 34 85 52 55 20 12 83 39 39 34 17 64 94 72 33 55 87 64 16 78 13 28 53 2 23 92 70 69 45 57 55 3 32 5 5 22 23 45 55 45 79 92 7 68 52 52 4 79 58 96 34 63 74 36 17 73 93 30 94 66 71 82 79 70 7 67 35 41 3 7 79 38 92 84 74 32 92 45 47 66 88 19 14 59 23 42 26 82 4 35 65 54 87 49 87 39 88 59 30 11 56 6 85 66 16 5 56 65 71 36 66 26 29 97 19 51 71 65 71 46 68 47 64 82 29 27 85 22 98 11 28 97 11 14 95 5 52 22 90 2 64 28 54 93 45 90 88 61 3 74 22 57 24 24 26 85 11 76 67 85 48 60 15 23 68 19 68 8 43 16 73 51 62 45 70 59 39 59 39 44 27 72 80 66 20 86 12 27 63 80 41 12 1 92 65 96 80 22 49 29 78 51 69 97 23 4 79 38 30 88 52 53 95 17 81 22 48 33 47 38 88 39 84 84 67 79 38 90 84 79 21 89 58 13 58 36 53 16 51 5 84 97 76 44 54 92 38 62 50 76 99 85 96 95 56 52 57 14 19 83 87 6 56 76 17 83 48 75 76 53 74 58 87 68 5 35 58 30 44 48 44 68 6 76 61 22 60 28 93 65 6 34 2 21 54 21 78 65 30 79 6 53 67 53 76 43 84 9 22 82 24 56 14 39 58 48 57 59 52 31 47 10 71 13 24 45 14 4 80 68 81 56 11 80 94 82 15 65 97 72 31 54 27 15 63 10 32 77 16 5 9 99 50 81 33 90 78 51 55 25 32 35 21 11 3 44 70 90 18 67 67 69 12 59 88 94 88 41 67 46 62 41 38 34 52 97 13 69 86 60 91 11 31 19 15 41 78 46 85 59 36 20 35 25 27 61 16 44 30 34 3 70 72 21 35 5 12 26 20 59 12 83 64 78 30 19 38 70 24 3 94 27 80 15 69 67 33 28 39 80 60 99 44 63 1 8 1 91 58 98 6 15 43 37 4 53 55 51 74 61 58 96 60 75 97 13 41 76 5 71 25 29 16 48 14 61 55 62 3 95 86 75 41 16 22 68 63 33 62 78 67 37 32 93 31 38 97 32 40 90 72 11 10 6 6 45 14 86 12 66 82 89 19 68 72 2 29 71 88 90 55 99 40 98 64 97 86 94 37 94 43 69 57 92 4 64 25 20 65 71 87 26 26 11 97 4 16 53 25 23 47 51 57 68 36 54 50 65 15 83 34 ;1 1 1 1 0 1 1 0 1 1 0 0 0 0 0 1 0 1 1 0 0 0 0 1 0 0 1 0 0 1 1 0 0 0 0 1 1 0 0 1 0 1 1 0 0 0 0 0 0 1 0 0 1 0 0 1 1 0 1 1 0 0 0 1 0 0 1 0 0 0 0 0 0 1 1 0 1 0 1 0 0 1 1 0 0 0 1 1 1 0 1 0 0 0 1 0 1 0 1 0 0 0 1 0 1 0 0 1 1 1 0 1 0 1 1 0 1 1 0 0 1 0 1 1 0 1 0 1 1 0 1 0 0 0 1 0 0 1 1 0 0 1 1 1 0 0 0 1 1 1 1 1 0 0 0 1 0 1 0 1 1 0 1 1 1 1 1 1 1 1 1 0 0 0 0 1 0 1 1 0 1 1 0 1 0 0 0 0 1 1 1 1 1 1 1 1 1 0 1 0 1 1 1 1 0 0 0 0 1 1 0 0 1 0 0 1 0 1 0 1 0 1 0 0 1 1 1 1 1 0 1 0 1 0 1 0 0 0 1 0 0 1 0 1 1 0 0 1 1 1 0 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 0 1 1 0 1 1 0 0 0 0 1 0 1 1 1 0 1 0 1 1 1 0 1 0 0 1 0 0 1 0 1 0 1 0 0 0 1 1 1 0 0 0 1 1 1 1 1 1 1 0 1 0 0 1 0 0 0 0 1 1 0 1 1 0 0 1 1 0 0 1 1 0 1 0 1 1 0 0 0 0 0 0 0 1 1 0 1 0 0 1 1 1 1 0 0 1 0 0 0 0 0 0 0 1 0 1 0 0 1 0 0 0 1 0 1 0 0 1 0 1 1 0 0 1 0 1 0 0 1 1 1 0 1 1 1 1 0 1 1 1 0 1 1 0 1 1 0 1 1 1 0 1 0 1 0 1 1 0 0 1 0 0 0 0 0 0 0 0 0 1 1 0 1 0 1 1 1 0 1 1 1 1 0 0 0 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 0 0 1 0 1 0 1 1 1 0 0 0 1 0 0 1 0 0 0 1 0 0 1 0 0 0 0 1 0 1 1 0 1 1 1 0 1 1 0 0 1 1 0 1 1 0 0 1 1 0 1 0 0 0 1 0 0 1 1 0 0 0 0 0 1 1 0 1 0 1 0 0 0 1 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 1 1 0 1 1 1 0 1 1 1 1 0 0 0 1 1 1 0 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 0 1 1 0 0 1 0 1 0 0 1 0 0 1 1 0 0 0 0 0 0 1 0 0 1 1 1 0 0 1 0 1 1 0 0 0 0 0 0 1 0 0 0 1 0 0 1 0 1 0 0 1 0 1 0 0 1 1 0 0 1 0 1 0 0 0 0 1 1 1 1 1 1 0 0 1 0 0 1 0 0 1 1 1 0 1 0 1 1 1 0 1 1 1 1 1 0 1 0 1 0 0 1 1 1 0 0 1 1 0 1 1 1 0 1 0 1 1 0 0 1 1 0 1 0 1 0 0 0 1 0 0 1 0 0 1 0 0 1 1 0 1 1 1 1 0 0 1 0 0 1 0 0 1 0 0 0 0 1 1 0 0 1 1 0 1 0 0 0 1 1 1 0 1 0 0 1 1 1 1 1 0 1 1 1 0 0 0 1 1 0 0 1 1 1 1 0 1 1 1 0 1 0 0 0 0 1 0 0 1 0 1 1 1 0 1 0 1 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 1 1 1 1 1 0 1 1 1 1 1 0 1 0 1 1 0 0 0 1 0 0 1 1 0 0 1 1 0 1 1 1 0 0 0 1 0 0 1 0 1 0 0 0 0 0 1 1 1 0 ];\r\nLexp= 45294;\r\nL=Loop(m);\r\nassert(isequal(Lexp,L))\r\n%%\r\nm=[94 61 98 11 2 12 99 37 26 10 86 18 88 42 43 38 62 5 36 56 5 86 78 29 64 27 54 72 96 73 36 99 81 55 90 55 73 51 94 11 82 15 45 67 63 79 75 22 4 18 39 1 75 57 92 49 24 16 68 52 70 88 56 38 25 88 86 29 15 89 37 96 85 40 86 76 26 73 42 24 45 22 40 36 35 11 65 22 69 3 9 27 28 78 40 68 62 75 32 65 49 15 35 32 88 73 20 95 67 94 60 73 81 93 76 67 56 37 1 74 59 35 51 38 92 56 37 89 54 67 78 11 62 68 99 61 45 48 74 92 15 74 96 31 33 21 50 98 7 31 51 62 27 26 47 81 66 3 13 98 83 94 ;0 1 1 1 1 0 1 1 0 1 1 0 0 1 1 1 0 1 0 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 1 0 1 0 0 1 0 1 1 0 0 1 0 0 1 0 1 1 0 0 1 1 1 1 0 0 1 1 0 0 0 1 0 0 1 1 0 1 1 0 1 0 1 1 1 0 0 0 0 1 1 0 0 1 0 0 1 0 1 0 0 0 1 1 0 1 1 0 1 0 0 0 1 1 1 0 1 0 0 0 0 0 0 1 1 0 0 1 1 1 0 0 1 1 1 1 1 0 1 1 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 1 0 1 1 0 0 0 0 0 0 1 1 0 ];\r\nLexp= 8346;\r\nL=Loop(m);\r\nassert(isequal(Lexp,L))\r\n%%\r\n% A Method to Read, Process, and Output a GJam Entry\r\n% Read a file into a Cell Array variable for each case\r\n% Open Output file\r\n% Loop thru Cases using cell array loaded\r\n% Write each case's solution\r\n% Generalized function to Process a Concise Data set\r\n\r\n% function GJam_SA_2011A\r\n% %Segment Loop\r\n% %fn='A-small-practice.in';\r\n% fn='A-large-practice.in';\r\n% [data] = read_file(fn);\r\n% \r\n% %fidG = fopen('A-small-output.out', 'w');\r\n% fidG = fopen('A-large-output.out', 'w');\r\n% tic\r\n% for i=1:size(data,2)\r\n%  L = Loop(data{i}) ;% data 2xN, row 1 Length row 2 Type\r\n%  toc\r\n%   fprintf(fidG,'Case #%i: %i\\n', i,L);\r\n%   fprintf('Case #%i: %i\\n', i,L);\r\n%   \r\n% end\r\n% toc\r\n% \r\n% fclose(fidG);\r\n% \r\n% end\r\n% \r\n% function [d] = read_file(fn)\r\n% %Read a numeric of lines to count\r\n% fid=fopen(fn);\r\n% fgetl(fid); % Total Count ignore\r\n% set=0;\r\n% while ~feof(fid)\r\n%  fgetl(fid); % Qty in a row, ignore\r\n%  strv=fgetl(fid); % read line of data as a string since mixed numeric/char\r\n%  m=str2num(strrep(strrep(strv,'R',''),'B','')); % Remove R and B to get Lengths\r\n%  % Extract R and B and convert to a binary vector\r\n%  m(2,:)=str2num(strrep(strrep(strv(regexp(strv,'[RB]','start')),'R','0 '),'B','1 '));\r\n%  \r\n%  set=set+1;\r\n%  d{set}=m;\r\n% end\r\n% \r\n% fclose(fid);\r\n% \r\n% end\r\n% \r\n% function L=Loop(m)\r\n% % Create two descending vectors\r\n% % L=sum of paired sets - qty of paired sets\r\n%  L=0;\r\n%  m0=m(1,m(2,:)==0);\r\n%  m1=m(1,m(2,:)==1);\r\n%  if isempty(m0),return;end\r\n%  if isempty(m1),return;end\r\n%  m0=sort(m0,'descend');\r\n%  m1=sort(m1,'descend');\r\n%  q=min(length(m0),length(m1));\r\n%  L=sum([m0(1:q) m1(1:q)])-2*q;\r\n%  \r\n% end\r\n% \r\n% Google File Input\r\n%5\r\n%1\r\n%5B\r\n%4\r\n%6R 1B 7R 3B\r\n%7\r\n%5B 4R 3R 2R 5R 4R 3R\r\n%673\r\n% Data line of 673 values not included\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":7,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":15,"test_suite_updated_at":"2014-03-29T20:39:55.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2014-03-29T18:45:27.000Z","updated_at":"2014-03-29T20:39:55.000Z","published_at":"2014-03-29T19:21: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\u003eThis Challenge is derived from\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://code.google.com/codejam/contest/837485/dashboard\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eGJam 2011 Africa: Closing the Loop\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e. Small Case Google Code Jam 2014 Kicks Off its Qualifier round April 11.\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://code.google.com/codejam\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eGJam Registration\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e. The Test Suite, at the bottom, contains a full GJam file input read, process, and Output example.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eCreate the maximum length Loop of alternating Segment types (0 or 1). Return Maximum Length. Segment connection Knots reduce the Length by 1, including the end to the start knot. If no valid loop can be created then return 0.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eInput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e M, 2xN matrix of [Lengths(1:N);Types(1:N)], Types are 0 or 1\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\u003eOutput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e L, maximum Loop Length of valid alternating segment types\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eExample:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e M=[6 1 7 3;1 0 1 0], L=13\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\u003eAdditional GJam solutions can be found at\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://go-hero.net/jam\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eExample GJam Matlab solutions\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e. Select Find Solutions, change Language to Matlab. There were only 50 Qualifier Matlab entrants in 2013 and a mere 2 Matlab users achieved round 2.\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\"}]}"}],"problem_search":{"errors":[],"problems":[{"id":1763,"title":"Primes for Large N:  2^30, System Memory Limit","description":"This Challenge is to further improve the \"primes\" function for speed/memory usage to the Limit of Cody Memory.\r\n\r\nThe Matlab function \"primes\" has a very efficient sieving method but it suffers from memory usage issues, especially on limited memory systems.\r\n\r\nThe \u003chttp://www.mathworks.com/matlabcentral/cody/problems/1761-primes-faster-for-large-n Primes Faster Challenge\u003e gives the first half to maximizing the primes function for optimal memory usage and speed.\r\n\r\nCody appears to have 2GB of RAM based upon \"out of memory\" messages observed.\r\n\r\nThe test case of 2^30 pushes the upper limits of Cody memory.\r\n\r\nThe reference solution can process N=2^32 on an 8GB machine in 68sec.\r\n\r\n*Input:* N  (max of primes to find)\r\n\r\n*Output:* vector of primes  (all primes less than or equal to N)\r\n\r\n*Scoring:* Time to find all primes \u003c 2^30\r\n\r\n*Hints:*\r\n\r\n  1) Doubles use 8 bytes;\r\n  2) The primes method  p(((k*k+1)/2):k:q) creates a vector using q/k*8 bytes. This is 8/3 the size of p for k=3 because k is a double. \r\n  3) Segmentation will keep memory usage below limits at the cost of time.\r\n  4) Cody Available Memory appears to be 537.27MB. XP Operating system eats memory.\r\n  5) Usage of profiler and Task Manager combined give performance insights.","description_html":"\u003cp\u003eThis Challenge is to further improve the \"primes\" function for speed/memory usage to the Limit of Cody Memory.\u003c/p\u003e\u003cp\u003eThe Matlab function \"primes\" has a very efficient sieving method but it suffers from memory usage issues, especially on limited memory systems.\u003c/p\u003e\u003cp\u003eThe \u003ca href = \"http://www.mathworks.com/matlabcentral/cody/problems/1761-primes-faster-for-large-n\"\u003ePrimes Faster Challenge\u003c/a\u003e gives the first half to maximizing the primes function for optimal memory usage and speed.\u003c/p\u003e\u003cp\u003eCody appears to have 2GB of RAM based upon \"out of memory\" messages observed.\u003c/p\u003e\u003cp\u003eThe test case of 2^30 pushes the upper limits of Cody memory.\u003c/p\u003e\u003cp\u003eThe reference solution can process N=2^32 on an 8GB machine in 68sec.\u003c/p\u003e\u003cp\u003e\u003cb\u003eInput:\u003c/b\u003e N  (max of primes to find)\u003c/p\u003e\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e vector of primes  (all primes less than or equal to N)\u003c/p\u003e\u003cp\u003e\u003cb\u003eScoring:\u003c/b\u003e Time to find all primes \u0026lt; 2^30\u003c/p\u003e\u003cp\u003e\u003cb\u003eHints:\u003c/b\u003e\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e1) Doubles use 8 bytes;\r\n2) The primes method  p(((k*k+1)/2):k:q) creates a vector using q/k*8 bytes. This is 8/3 the size of p for k=3 because k is a double. \r\n3) Segmentation will keep memory usage below limits at the cost of time.\r\n4) Cody Available Memory appears to be 537.27MB. XP Operating system eats memory.\r\n5) Usage of profiler and Task Manager combined give performance insights.\r\n\u003c/pre\u003e","function_template":"function p = primes_faster_large(n)\r\n% Standard primes will not succeed\r\n  p=[];\r\nreturn\r\n\r\n%PRIMES Generate list of prime numbers.\r\n%   PRIMES(N) is a row vector of the prime numbers less than or \r\n%   equal to N.  A prime number is one that has no factors other\r\n%   than 1 and itself.\r\n%   Class support for input N:\r\n%      float: double, single\r\n%   See also FACTOR, ISPRIME.\r\n%   Copyright 1984-2004 The MathWorks, Inc. \r\n%   $Revision: 1.16.4.3 $  $Date: 2010/08/23 23:13:13 $\r\n\r\nif length(n)~=1 \r\n  error(message('MATLAB:primes:InputNotScalar')); \r\nend\r\nif n \u003c 2, p = zeros(1,0,class(n)); return, end\r\np = 1:2:n;\r\nq = length(p);\r\np(1) = 2;\r\nfor k = 3:2:sqrt(n)\r\n  if p((k+1)/2)\r\n     p(((k*k+1)/2):k:q) = 0;\r\n  end\r\nend\r\np = p(p\u003e0);\r\n\r\nend","test_suite":"feval(@assignin,'caller','score',55);\r\n%%\r\ntic\r\nassert(isequal(primes_faster_large(1),primes(1)))\r\nassert(isequal(primes_faster_large(2),primes(2)))\r\nfor i=1:10\r\n n=randi(2000,1);\r\n assert(isequal(primes_faster_large(n),primes(n)))\r\nend\r\ntoc\r\n%%\r\ntic\r\nta=clock;\r\n p = primes_faster_large(2^30);\r\nt1=etime(clock,ta); % time in sec\r\nfprintf('P 2^30 %12i %10.3f\\n',length(p),t1)\r\nassert(isequal(length(p),54400028))\r\nassert(all(diff(p)\u003e0))\r\n%assert(isequal(size(unique(p),2),54400028))\r\nptr=randi(7603553,1,10); % small to avoid timeout\r\npchk=double(p(ptr));\r\nassert(all(isprime(pchk)))\r\nfeval(  @assignin,'caller','score',floor(min(55,t1))  );\r\ntoc\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":7,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":26,"test_suite_updated_at":"2013-08-07T00:23:25.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2013-07-30T19:43:11.000Z","updated_at":"2025-06-07T08:19:24.000Z","published_at":"2013-07-31T04:12:38.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\u003eThis Challenge is to further improve the \\\"primes\\\" function for speed/memory usage to the Limit of Cody Memory.\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\u003eThe Matlab function \\\"primes\\\" has a very efficient sieving method but it suffers from memory usage issues, especially on limited memory systems.\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\u003eThe\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/cody/problems/1761-primes-faster-for-large-n\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ePrimes Faster Challenge\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e gives the first half to maximizing the primes function for optimal memory usage and speed.\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\u003eCody appears to have 2GB of RAM based upon \\\"out of memory\\\" messages observed.\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\u003eThe test case of 2^30 pushes the upper limits of Cody memory.\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\u003eThe reference solution can process N=2^32 on an 8GB machine in 68sec.\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\u003eInput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e N (max of primes to find)\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\u003eOutput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e vector of primes (all primes less than or equal to N)\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\u003eScoring:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Time to find all primes \u0026lt; 2^30\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\u003eHints:\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) Doubles use 8 bytes;\\n2) The primes method  p(((k*k+1)/2):k:q) creates a vector using q/k*8 bytes. This is 8/3 the size of p for k=3 because k is a double. \\n3) Segmentation will keep memory usage below limits at the cost of time.\\n4) Cody Available Memory appears to be 537.27MB. XP Operating system eats memory.\\n5) Usage of profiler and Task Manager combined give performance insights.]]\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":2260,"title":"GJam 2011 Africa Qual A: Maximum Loop Size","description":"This Challenge is derived from \u003chttp://code.google.com/codejam/contest/837485/dashboard GJam 2011 Africa: Closing the Loop\u003e. Small Case\r\nGoogle Code Jam 2014 Kicks Off its Qualifier round April 11. \u003chttp://code.google.com/codejam GJam Registration\u003e. The Test Suite, at the bottom, contains a full GJam file input read, process, and Output example.\r\n\r\nCreate the maximum length Loop of alternating Segment types (0 or 1). Return Maximum Length. Segment connection Knots reduce the Length by 1, including the end to the start knot. \r\nIf no valid loop can be created then return 0. \r\n\r\n*Input:* M, 2xN matrix of [Lengths(1:N);Types(1:N)], Types are 0 or 1\r\n\r\n*Output:* L, maximum Loop Length of valid alternating segment types \r\n\r\n*Example:*\r\nM=[6 1 7 3;1 0 1 0], L=13\r\n\r\n\r\n\r\nAdditional GJam solutions can be found at \u003chttp://go-hero.net/jam Example GJam Matlab solutions\u003e. Select Find Solutions, change Language to Matlab. There were only 50 Qualifier Matlab entrants in 2013 and a mere 2 Matlab users achieved round 2.","description_html":"\u003cp\u003eThis Challenge is derived from \u003ca href = \"http://code.google.com/codejam/contest/837485/dashboard\"\u003eGJam 2011 Africa: Closing the Loop\u003c/a\u003e. Small Case\r\nGoogle Code Jam 2014 Kicks Off its Qualifier round April 11. \u003ca href = \"http://code.google.com/codejam\"\u003eGJam Registration\u003c/a\u003e. The Test Suite, at the bottom, contains a full GJam file input read, process, and Output example.\u003c/p\u003e\u003cp\u003eCreate the maximum length Loop of alternating Segment types (0 or 1). Return Maximum Length. Segment connection Knots reduce the Length by 1, including the end to the start knot. \r\nIf no valid loop can be created then return 0.\u003c/p\u003e\u003cp\u003e\u003cb\u003eInput:\u003c/b\u003e M, 2xN matrix of [Lengths(1:N);Types(1:N)], Types are 0 or 1\u003c/p\u003e\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e L, maximum Loop Length of valid alternating segment types\u003c/p\u003e\u003cp\u003e\u003cb\u003eExample:\u003c/b\u003e\r\nM=[6 1 7 3;1 0 1 0], L=13\u003c/p\u003e\u003cp\u003eAdditional GJam solutions can be found at \u003ca href = \"http://go-hero.net/jam\"\u003eExample GJam Matlab solutions\u003c/a\u003e. Select Find Solutions, change Language to Matlab. There were only 50 Qualifier Matlab entrants in 2013 and a mere 2 Matlab users achieved round 2.\u003c/p\u003e","function_template":"function L=Loop(m)\r\n% L, Length of valid Loop with knots subtracted\r\n% m, [2,N] array\r\n\r\n L=0;\r\nend","test_suite":"%\r\nm=[5 ;1 ];\r\nLexp= 0;\r\nL=Loop(m);\r\nassert(isequal(Lexp,L))\r\n%%\r\nm=[6 1 7 3 ;0 1 0 1 ];\r\nLexp= 13;\r\nL=Loop(m);\r\nassert(isequal(Lexp,L))\r\n%%\r\nm=[5 4 3 2 5 4 3 ;1 0 0 0 0 0 0 ];\r\nLexp= 8;\r\nL=Loop(m);\r\nassert(isequal(Lexp,L))\r\n%%\r\nm=[46 39 3 40 12 36 83 24 61 66 19 87 38 62 27 12 66 39 48 25 89 3 13 80 61 4 76 26 99 89 62 79 25 45 19 5 3 97 1 20 97 86 73 89 26 62 22 41 13 55 28 87 95 16 41 30 62 44 11 34 48 47 33 51 22 15 42 32 33 91 21 57 26 78 66 65 78 97 57 97 19 78 27 23 8 14 37 47 59 38 43 90 96 3 92 59 42 99 92 32 30 6 19 20 62 84 25 87 83 42 51 80 10 16 15 23 40 3 76 13 39 78 94 3 21 94 87 21 74 64 55 43 8 4 46 24 93 69 78 53 28 59 95 29 79 54 64 81 26 5 61 51 47 92 31 53 43 28 79 63 68 14 69 95 35 16 30 34 77 67 65 69 82 79 63 65 82 82 62 98 7 58 29 70 93 14 7 82 20 99 88 77 39 59 65 35 62 51 13 51 37 59 91 84 34 50 62 88 67 12 82 13 75 66 31 69 97 71 21 70 6 53 71 47 40 39 56 5 76 80 60 38 63 89 18 79 55 16 53 7 61 97 88 19 68 97 52 94 90 60 60 29 53 88 50 11 57 81 75 50 28 58 92 1 14 74 20 92 45 52 97 37 10 52 8 93 17 34 76 40 59 34 67 73 73 36 4 71 90 33 71 12 92 98 88 10 6 6 90 15 4 66 72 39 60 69 38 7 20 81 11 98 51 79 71 25 36 60 96 70 46 22 35 27 88 3 30 81 28 53 88 16 57 25 23 14 98 38 16 66 10 5 59 72 19 4 63 73 30 22 61 1 10 73 1 18 49 32 91 12 18 83 42 34 50 36 75 37 34 69 30 10 25 36 38 18 13 5 22 89 56 95 32 68 54 86 80 3 37 30 98 49 85 49 2 7 68 9 96 54 43 1 87 56 66 30 43 54 46 71 90 76 1 35 26 16 97 48 52 73 8 3 5 21 89 15 40 60 12 14 90 15 42 37 27 3 75 52 82 16 77 39 12 85 18 76 81 81 19 6 2 19 33 41 58 17 54 41 2 37 39 43 69 76 37 45 77 67 96 38 34 52 67 1 84 77 11 8 36 19 18 79 13 89 67 99 77 15 47 16 9 89 36 84 56 33 97 95 80 93 47 90 75 88 76 61 16 26 8 75 60 61 23 37 98 29 62 17 13 59 8 38 12 4 50 64 44 9 4 55 53 91 79 29 49 23 8 42 76 82 43 55 46 39 59 12 5 40 36 94 29 89 13 39 91 92 71 35 97 6 25 42 95 38 57 88 84 80 17 38 39 77 45 66 27 42 51 72 35 7 88 18 60 20 98 78 10 32 56 14 25 82 58 3 65 19 11 75 66 21 51 28 58 92 95 16 77 37 89 36 44 70 72 40 4 64 50 69 8 31 16 64 69 6 17 71 82 31 18 68 59 80 51 24 82 63 40 36 33 40 42 48 86 48 10 88 61 17 3 53 69 85 39 44 79 39 61 53 96 32 59 52 79 96 97 86 43 34 35 28 79 17 13 ;1 0 0 0 1 1 0 0 0 1 1 1 1 0 0 1 0 1 0 0 1 0 0 0 0 0 0 1 1 0 0 0 1 1 1 1 1 1 0 0 0 0 1 1 0 1 0 0 0 1 0 0 1 1 0 0 0 0 1 0 0 0 0 1 1 0 0 0 0 1 1 1 1 1 0 0 0 0 1 1 0 1 1 1 1 0 1 1 0 0 1 0 0 0 1 0 1 0 0 1 1 0 1 1 0 0 0 1 1 0 1 1 1 0 1 1 0 1 1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 1 1 0 0 1 0 1 1 0 1 0 0 0 1 1 1 0 1 1 1 1 0 1 1 0 0 1 0 1 1 0 1 0 0 0 1 0 0 1 0 1 1 0 1 1 1 1 0 0 1 0 1 1 1 0 1 0 0 0 1 1 1 0 1 1 0 0 0 1 1 0 0 1 1 1 0 0 1 1 0 0 0 0 1 1 0 0 1 0 0 0 0 1 1 1 0 0 0 1 0 0 1 1 0 1 1 1 1 1 1 0 1 1 1 0 1 1 0 0 1 0 0 0 1 1 0 1 1 0 1 0 1 1 1 1 1 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 0 0 0 1 1 1 1 1 1 1 1 0 0 1 0 1 0 1 0 1 1 0 1 1 1 0 1 0 0 0 1 1 1 1 0 0 0 0 1 1 0 1 1 0 0 1 1 1 1 0 0 1 1 0 1 1 0 1 0 1 0 0 1 1 0 1 1 1 0 1 0 1 0 0 0 0 1 1 1 1 0 0 0 0 0 0 1 1 1 0 0 1 0 1 1 0 0 1 1 0 0 0 1 1 0 1 0 1 0 1 0 0 0 0 1 0 1 0 0 1 1 0 0 1 0 1 1 1 0 0 1 0 1 0 1 1 0 0 0 1 0 0 1 1 0 1 1 0 1 1 1 1 1 1 1 0 1 0 0 0 0 1 1 1 0 1 0 1 1 0 1 0 1 1 0 1 1 0 0 0 1 1 0 0 1 0 1 0 1 0 0 1 0 0 0 1 0 1 1 1 1 0 1 0 1 0 1 1 1 0 1 1 0 1 0 0 0 1 0 0 0 0 0 0 0 1 1 0 1 1 1 1 0 1 0 0 1 1 1 1 0 0 0 0 0 1 0 1 0 1 1 1 1 1 1 1 1 0 1 1 0 1 0 1 0 0 1 1 0 1 1 1 0 1 0 0 0 1 1 1 1 0 0 0 0 0 0 1 0 0 0 1 1 1 0 1 0 1 1 0 0 0 1 1 1 1 0 0 0 1 1 1 1 0 1 0 0 1 1 1 0 1 1 0 0 1 0 1 1 0 1 1 1 1 0 1 0 1 0 0 1 0 1 0 0 1 0 0 1 0 1 0 0 1 0 1 0 0 1 1 0 0 1 1 0 1 1 0 0 1 1 1 1 1 1 1 0 0 0 1 1 1 1 0 0 ];\r\nLexp= 32240;\r\nL=Loop(m);\r\nassert(isequal(Lexp,L))\r\n%%\r\nm=[60 44 41 80 31 96 12 58 31 13 61 58 55 64 83 6 13 23 18 47 22 44 70 8 31 99 85 80 80 39 33 65 43 67 33 25 82 37 79 84 28 66 74 26 68 54 33 35 2 3 64 8 44 17 70 7 24 78 36 78 46 38 91 26 95 98 58 90 11 32 96 5 63 31 7 93 10 35 12 70 85 2 9 24 83 64 22 74 53 77 98 57 52 88 6 15 90 77 94 94 12 69 80 50 78 10 71 80 95 95 58 79 6 90 15 59 22 96 56 53 65 53 46 23 3 81 34 63 50 95 2 87 40 38 73 1 23 91 42 57 73 71 57 32 25 60 1 79 98 27 71 77 32 93 31 74 53 40 33 39 72 93 43 59 58 90 91 52 29 2 37 8 13 84 29 33 79 23 85 78 75 48 83 80 35 88 61 2 37 72 1 7 78 69 32 76 26 53 93 40 50 95 27 25 86 7 83 14 63 66 52 44 54 83 61 48 31 40 61 98 57 69 32 44 42 76 41 74 64 87 32 19 21 92 6 80 54 85 17 66 75 57 95 81 61 60 62 11 51 36 51 62 10 55 60 12 54 35 50 16 28 94 31 91 6 76 7 26 44 16 58 41 96 99 94 73 53 46 63 6 54 60 96 13 74 76 18 55 34 27 67 67 73 82 35 72 56 27 78 51 49 65 79 73 45 32 40 9 97 43 12 68 25 65 2 86 46 63 70 74 88 18 69 58 37 77 76 31 57 19 91 87 86 45 31 83 55 85 26 56 94 10 48 86 34 85 52 55 20 12 83 39 39 34 17 64 94 72 33 55 87 64 16 78 13 28 53 2 23 92 70 69 45 57 55 3 32 5 5 22 23 45 55 45 79 92 7 68 52 52 4 79 58 96 34 63 74 36 17 73 93 30 94 66 71 82 79 70 7 67 35 41 3 7 79 38 92 84 74 32 92 45 47 66 88 19 14 59 23 42 26 82 4 35 65 54 87 49 87 39 88 59 30 11 56 6 85 66 16 5 56 65 71 36 66 26 29 97 19 51 71 65 71 46 68 47 64 82 29 27 85 22 98 11 28 97 11 14 95 5 52 22 90 2 64 28 54 93 45 90 88 61 3 74 22 57 24 24 26 85 11 76 67 85 48 60 15 23 68 19 68 8 43 16 73 51 62 45 70 59 39 59 39 44 27 72 80 66 20 86 12 27 63 80 41 12 1 92 65 96 80 22 49 29 78 51 69 97 23 4 79 38 30 88 52 53 95 17 81 22 48 33 47 38 88 39 84 84 67 79 38 90 84 79 21 89 58 13 58 36 53 16 51 5 84 97 76 44 54 92 38 62 50 76 99 85 96 95 56 52 57 14 19 83 87 6 56 76 17 83 48 75 76 53 74 58 87 68 5 35 58 30 44 48 44 68 6 76 61 22 60 28 93 65 6 34 2 21 54 21 78 65 30 79 6 53 67 53 76 43 84 9 22 82 24 56 14 39 58 48 57 59 52 31 47 10 71 13 24 45 14 4 80 68 81 56 11 80 94 82 15 65 97 72 31 54 27 15 63 10 32 77 16 5 9 99 50 81 33 90 78 51 55 25 32 35 21 11 3 44 70 90 18 67 67 69 12 59 88 94 88 41 67 46 62 41 38 34 52 97 13 69 86 60 91 11 31 19 15 41 78 46 85 59 36 20 35 25 27 61 16 44 30 34 3 70 72 21 35 5 12 26 20 59 12 83 64 78 30 19 38 70 24 3 94 27 80 15 69 67 33 28 39 80 60 99 44 63 1 8 1 91 58 98 6 15 43 37 4 53 55 51 74 61 58 96 60 75 97 13 41 76 5 71 25 29 16 48 14 61 55 62 3 95 86 75 41 16 22 68 63 33 62 78 67 37 32 93 31 38 97 32 40 90 72 11 10 6 6 45 14 86 12 66 82 89 19 68 72 2 29 71 88 90 55 99 40 98 64 97 86 94 37 94 43 69 57 92 4 64 25 20 65 71 87 26 26 11 97 4 16 53 25 23 47 51 57 68 36 54 50 65 15 83 34 ;1 1 1 1 0 1 1 0 1 1 0 0 0 0 0 1 0 1 1 0 0 0 0 1 0 0 1 0 0 1 1 0 0 0 0 1 1 0 0 1 0 1 1 0 0 0 0 0 0 1 0 0 1 0 0 1 1 0 1 1 0 0 0 1 0 0 1 0 0 0 0 0 0 1 1 0 1 0 1 0 0 1 1 0 0 0 1 1 1 0 1 0 0 0 1 0 1 0 1 0 0 0 1 0 1 0 0 1 1 1 0 1 0 1 1 0 1 1 0 0 1 0 1 1 0 1 0 1 1 0 1 0 0 0 1 0 0 1 1 0 0 1 1 1 0 0 0 1 1 1 1 1 0 0 0 1 0 1 0 1 1 0 1 1 1 1 1 1 1 1 1 0 0 0 0 1 0 1 1 0 1 1 0 1 0 0 0 0 1 1 1 1 1 1 1 1 1 0 1 0 1 1 1 1 0 0 0 0 1 1 0 0 1 0 0 1 0 1 0 1 0 1 0 0 1 1 1 1 1 0 1 0 1 0 1 0 0 0 1 0 0 1 0 1 1 0 0 1 1 1 0 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 0 1 1 0 1 1 0 0 0 0 1 0 1 1 1 0 1 0 1 1 1 0 1 0 0 1 0 0 1 0 1 0 1 0 0 0 1 1 1 0 0 0 1 1 1 1 1 1 1 0 1 0 0 1 0 0 0 0 1 1 0 1 1 0 0 1 1 0 0 1 1 0 1 0 1 1 0 0 0 0 0 0 0 1 1 0 1 0 0 1 1 1 1 0 0 1 0 0 0 0 0 0 0 1 0 1 0 0 1 0 0 0 1 0 1 0 0 1 0 1 1 0 0 1 0 1 0 0 1 1 1 0 1 1 1 1 0 1 1 1 0 1 1 0 1 1 0 1 1 1 0 1 0 1 0 1 1 0 0 1 0 0 0 0 0 0 0 0 0 1 1 0 1 0 1 1 1 0 1 1 1 1 0 0 0 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 0 0 1 0 1 0 1 1 1 0 0 0 1 0 0 1 0 0 0 1 0 0 1 0 0 0 0 1 0 1 1 0 1 1 1 0 1 1 0 0 1 1 0 1 1 0 0 1 1 0 1 0 0 0 1 0 0 1 1 0 0 0 0 0 1 1 0 1 0 1 0 0 0 1 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 1 1 0 1 1 1 0 1 1 1 1 0 0 0 1 1 1 0 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 0 1 1 0 0 1 0 1 0 0 1 0 0 1 1 0 0 0 0 0 0 1 0 0 1 1 1 0 0 1 0 1 1 0 0 0 0 0 0 1 0 0 0 1 0 0 1 0 1 0 0 1 0 1 0 0 1 1 0 0 1 0 1 0 0 0 0 1 1 1 1 1 1 0 0 1 0 0 1 0 0 1 1 1 0 1 0 1 1 1 0 1 1 1 1 1 0 1 0 1 0 0 1 1 1 0 0 1 1 0 1 1 1 0 1 0 1 1 0 0 1 1 0 1 0 1 0 0 0 1 0 0 1 0 0 1 0 0 1 1 0 1 1 1 1 0 0 1 0 0 1 0 0 1 0 0 0 0 1 1 0 0 1 1 0 1 0 0 0 1 1 1 0 1 0 0 1 1 1 1 1 0 1 1 1 0 0 0 1 1 0 0 1 1 1 1 0 1 1 1 0 1 0 0 0 0 1 0 0 1 0 1 1 1 0 1 0 1 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 1 1 1 1 1 0 1 1 1 1 1 0 1 0 1 1 0 0 0 1 0 0 1 1 0 0 1 1 0 1 1 1 0 0 0 1 0 0 1 0 1 0 0 0 0 0 1 1 1 0 ];\r\nLexp= 45294;\r\nL=Loop(m);\r\nassert(isequal(Lexp,L))\r\n%%\r\nm=[94 61 98 11 2 12 99 37 26 10 86 18 88 42 43 38 62 5 36 56 5 86 78 29 64 27 54 72 96 73 36 99 81 55 90 55 73 51 94 11 82 15 45 67 63 79 75 22 4 18 39 1 75 57 92 49 24 16 68 52 70 88 56 38 25 88 86 29 15 89 37 96 85 40 86 76 26 73 42 24 45 22 40 36 35 11 65 22 69 3 9 27 28 78 40 68 62 75 32 65 49 15 35 32 88 73 20 95 67 94 60 73 81 93 76 67 56 37 1 74 59 35 51 38 92 56 37 89 54 67 78 11 62 68 99 61 45 48 74 92 15 74 96 31 33 21 50 98 7 31 51 62 27 26 47 81 66 3 13 98 83 94 ;0 1 1 1 1 0 1 1 0 1 1 0 0 1 1 1 0 1 0 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 1 0 1 0 0 1 0 1 1 0 0 1 0 0 1 0 1 1 0 0 1 1 1 1 0 0 1 1 0 0 0 1 0 0 1 1 0 1 1 0 1 0 1 1 1 0 0 0 0 1 1 0 0 1 0 0 1 0 1 0 0 0 1 1 0 1 1 0 1 0 0 0 1 1 1 0 1 0 0 0 0 0 0 1 1 0 0 1 1 1 0 0 1 1 1 1 1 0 1 1 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 1 0 1 1 0 0 0 0 0 0 1 1 0 ];\r\nLexp= 8346;\r\nL=Loop(m);\r\nassert(isequal(Lexp,L))\r\n%%\r\n% A Method to Read, Process, and Output a GJam Entry\r\n% Read a file into a Cell Array variable for each case\r\n% Open Output file\r\n% Loop thru Cases using cell array loaded\r\n% Write each case's solution\r\n% Generalized function to Process a Concise Data set\r\n\r\n% function GJam_SA_2011A\r\n% %Segment Loop\r\n% %fn='A-small-practice.in';\r\n% fn='A-large-practice.in';\r\n% [data] = read_file(fn);\r\n% \r\n% %fidG = fopen('A-small-output.out', 'w');\r\n% fidG = fopen('A-large-output.out', 'w');\r\n% tic\r\n% for i=1:size(data,2)\r\n%  L = Loop(data{i}) ;% data 2xN, row 1 Length row 2 Type\r\n%  toc\r\n%   fprintf(fidG,'Case #%i: %i\\n', i,L);\r\n%   fprintf('Case #%i: %i\\n', i,L);\r\n%   \r\n% end\r\n% toc\r\n% \r\n% fclose(fidG);\r\n% \r\n% end\r\n% \r\n% function [d] = read_file(fn)\r\n% %Read a numeric of lines to count\r\n% fid=fopen(fn);\r\n% fgetl(fid); % Total Count ignore\r\n% set=0;\r\n% while ~feof(fid)\r\n%  fgetl(fid); % Qty in a row, ignore\r\n%  strv=fgetl(fid); % read line of data as a string since mixed numeric/char\r\n%  m=str2num(strrep(strrep(strv,'R',''),'B','')); % Remove R and B to get Lengths\r\n%  % Extract R and B and convert to a binary vector\r\n%  m(2,:)=str2num(strrep(strrep(strv(regexp(strv,'[RB]','start')),'R','0 '),'B','1 '));\r\n%  \r\n%  set=set+1;\r\n%  d{set}=m;\r\n% end\r\n% \r\n% fclose(fid);\r\n% \r\n% end\r\n% \r\n% function L=Loop(m)\r\n% % Create two descending vectors\r\n% % L=sum of paired sets - qty of paired sets\r\n%  L=0;\r\n%  m0=m(1,m(2,:)==0);\r\n%  m1=m(1,m(2,:)==1);\r\n%  if isempty(m0),return;end\r\n%  if isempty(m1),return;end\r\n%  m0=sort(m0,'descend');\r\n%  m1=sort(m1,'descend');\r\n%  q=min(length(m0),length(m1));\r\n%  L=sum([m0(1:q) m1(1:q)])-2*q;\r\n%  \r\n% end\r\n% \r\n% Google File Input\r\n%5\r\n%1\r\n%5B\r\n%4\r\n%6R 1B 7R 3B\r\n%7\r\n%5B 4R 3R 2R 5R 4R 3R\r\n%673\r\n% Data line of 673 values not included\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":7,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":15,"test_suite_updated_at":"2014-03-29T20:39:55.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2014-03-29T18:45:27.000Z","updated_at":"2014-03-29T20:39:55.000Z","published_at":"2014-03-29T19:21: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\u003eThis Challenge is derived from\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://code.google.com/codejam/contest/837485/dashboard\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eGJam 2011 Africa: Closing the Loop\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e. Small Case Google Code Jam 2014 Kicks Off its Qualifier round April 11.\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://code.google.com/codejam\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eGJam Registration\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e. The Test Suite, at the bottom, contains a full GJam file input read, process, and Output example.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eCreate the maximum length Loop of alternating Segment types (0 or 1). Return Maximum Length. Segment connection Knots reduce the Length by 1, including the end to the start knot. If no valid loop can be created then return 0.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eInput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e M, 2xN matrix of [Lengths(1:N);Types(1:N)], Types are 0 or 1\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\u003eOutput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e L, maximum Loop Length of valid alternating segment types\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eExample:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e M=[6 1 7 3;1 0 1 0], L=13\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\u003eAdditional GJam solutions can be found at\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://go-hero.net/jam\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eExample GJam Matlab solutions\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e. Select Find Solutions, change Language to Matlab. There were only 50 Qualifier Matlab entrants in 2013 and a mere 2 Matlab users achieved round 2.\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\"}]}"}],"term":"tag:\"segments\"","current_player_id":null,"fields":[{"name":"page","type":"integer","callback":null,"default":1,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"per_page","type":"integer","callback":null,"default":50,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"sort","type":"string","callback":null,"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"body","type":"text","callback":null,"default":"*:*","directive":null,"facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":false},{"name":"group","type":"string","callback":null,"default":null,"directive":"group","facet":true,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"difficulty_rating_bin","type":"string","callback":null,"default":null,"directive":"difficulty_rating_bin","facet":true,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"id","type":"integer","callback":null,"default":null,"directive":"id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"tag","type":"string","callback":null,"default":null,"directive":"tag","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"product","type":"string","callback":null,"default":null,"directive":"product","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"created_at","type":"timeframe","callback":{},"default":null,"directive":"created_at","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"profile_id","type":"integer","callback":null,"default":null,"directive":"author_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"created_by","type":"string","callback":null,"default":null,"directive":"author","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"player_id","type":"integer","callback":null,"default":null,"directive":"solver_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"player","type":"string","callback":null,"default":null,"directive":"solver","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"solvers_count","type":"integer","callback":null,"default":null,"directive":"solvers_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"comments_count","type":"integer","callback":null,"default":null,"directive":"comments_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"likes_count","type":"integer","callback":null,"default":null,"directive":"likes_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"leader_id","type":"integer","callback":null,"default":null,"directive":"leader_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"leading_solution","type":"integer","callback":null,"default":null,"directive":"leading_solution","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true}],"filters":[{"name":"asset_type","type":"string","callback":null,"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":"\"cody:problem\"","prepend":true},{"name":"profile_id","type":"integer","callback":{},"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":"author_id","static":null,"prepend":true}],"query":{"params":{"per_page":50,"term":"tag:\"segments\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"segments\"","","\"","segments","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007fdd46c77128\u003e":null,"#\u003cMathWorks::Search::Field:0x00007fdd46c77088\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007fdd46c767c8\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007fdd46c773a8\u003e":1,"#\u003cMathWorks::Search::Field:0x00007fdd46c77308\u003e":50,"#\u003cMathWorks::Search::Field:0x00007fdd46c77268\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007fdd46c771c8\u003e":"tag:\"segments\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007fdd46c771c8\u003e":"tag:\"segments\""},"queried_facets":{}},"query_backend":{"connection":{"configuration":{"index_url":"http://index-op-v2/solr/","query_url":"http://search-op-v2/solr/","direct_access_index_urls":["http://index-op-v2/solr/"],"direct_access_query_urls":["http://search-op-v2/solr/"],"timeout":10,"vhost":"search","exchange":"search.topic","heartbeat":30,"pre_index_mode":false,"host":"rabbitmq-eks","port":5672,"username":"cody-search","password":"78X075ddcV44","virtual_host":"search","indexer":"amqp","http_logging":"true","core":"cody"},"query_connection":{"uri":"http://search-op-v2/solr/cody/","proxy":null,"connection":{"parallel_manager":null,"headers":{"User-Agent":"Faraday v1.0.1"},"params":{},"options":{"params_encoder":"Faraday::FlatParamsEncoder","proxy":null,"bind":null,"timeout":null,"open_timeout":null,"read_timeout":null,"write_timeout":null,"boundary":null,"oauth":null,"context":null,"on_data":null},"ssl":{"verify":true,"ca_file":null,"ca_path":null,"verify_mode":null,"cert_store":null,"client_cert":null,"client_key":null,"certificate":null,"private_key":null,"verify_depth":null,"version":null,"min_version":null,"max_version":null},"default_parallel_manager":null,"builder":{"adapter":{"name":"Faraday::Adapter::NetHttp","args":[],"block":null},"handlers":[{"name":"Faraday::Response::RaiseError","args":[],"block":null}],"app":{"app":{"ssl_cert_store":{"verify_callback":null,"error":null,"error_string":null,"chain":null,"time":null},"app":{},"connection_options":{},"config_block":null}}},"url_prefix":"http://search-op-v2/solr/cody/","manual_proxy":false,"proxy":null},"update_format":"RSolr::JSON::Generator","update_path":"update","options":{"url":"http://search-op-v2/solr/cody"}}},"query":{"params":{"per_page":50,"term":"tag:\"segments\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"segments\"","","\"","segments","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007fdd46c77128\u003e":null,"#\u003cMathWorks::Search::Field:0x00007fdd46c77088\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007fdd46c767c8\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007fdd46c773a8\u003e":1,"#\u003cMathWorks::Search::Field:0x00007fdd46c77308\u003e":50,"#\u003cMathWorks::Search::Field:0x00007fdd46c77268\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007fdd46c771c8\u003e":"tag:\"segments\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007fdd46c771c8\u003e":"tag:\"segments\""},"queried_facets":{}},"options":{"fields":["id","difficulty_rating"]},"join":" "},"results":[{"id":1763,"difficulty_rating":"easy"},{"id":2260,"difficulty_rating":"easy-medium"}]}}