{"group":{"id":1,"name":"Community","lockable":false,"created_at":"2012-01-18T18:02:15.000Z","updated_at":"2025-12-14T01:33:56.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":"2025-12-14T00: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":808,"title":"Hamming Weight - Fast","description":"The Hamming Weight, \u003chttp://en.wikipedia.org/wiki/Hamming_weight wiki Hamming Weight\u003e, in its most simple form is the number of ones in the binary representation of a value.\r\n\r\nThe task here is to create a fast Hamming Weight function/method such that processing many 4K x 4K images of 32 bit integer can be evaluated rapidly. Saw this question posed on Stack Overflow.\r\n\r\n*Input:* Vector of length N of 32 bit integer values.\r\n\r\n*Output:* Vector of number of ones of the binary representation\r\n\r\n*Scoring:* Time in milliseconds to process a [4096*4096,1] vector\r\n\r\n*Examples:* Input [7 ; 3], output=[3;2];  [16 32], output [1;1]; [0 4294967295] output [0;32]\r\n\r\n*Timing Test vector:* uint32(randi(2^32,[4096*4096,1])-1)\r\n\r\n*Minimum vector length/increment:* 65536\r\n\r\nHelpful, possibly, global variables.\r\n\r\nb1=uint32(1431655765); b2=uint32(858993459); b3=uint32(252645135) b4=uint32(16711935); b5=uint32(65535);\r\n\r\nHex: b1=55555555 b2=33333333 b3=0F0F0F0F b4=00FF00FF b5=0000FFFF \r\n\r\nThe array num_ones is created for values 0-65535 (0:2^16-1).\r\nnum_ones(1)=0, num_ones(2)=1, num_ones(3)=1,num_ones(4)=2,...num_ones(65536)=15\r\n\r\nDue to lack of zero indexing num_ones(value+1) is number of ones for value.\r\n\r\n\r\nHint: Globals are not good for time performance.\r\n\r\nHint: Segmentation appears to provide significant time optimization potential.\r\n\r\n\r\n\r\n","description_html":"\u003cp\u003eThe Hamming Weight, \u003ca href=\"http://en.wikipedia.org/wiki/Hamming_weight\"\u003ewiki Hamming Weight\u003c/a\u003e, in its most simple form is the number of ones in the binary representation of a value.\u003c/p\u003e\u003cp\u003eThe task here is to create a fast Hamming Weight function/method such that processing many 4K x 4K images of 32 bit integer can be evaluated rapidly. Saw this question posed on Stack Overflow.\u003c/p\u003e\u003cp\u003e\u003cb\u003eInput:\u003c/b\u003e Vector of length N of 32 bit integer values.\u003c/p\u003e\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e Vector of number of ones of the binary representation\u003c/p\u003e\u003cp\u003e\u003cb\u003eScoring:\u003c/b\u003e Time in milliseconds to process a [4096*4096,1] vector\u003c/p\u003e\u003cp\u003e\u003cb\u003eExamples:\u003c/b\u003e Input [7 ; 3], output=[3;2];  [16 32], output [1;1]; [0 4294967295] output [0;32]\u003c/p\u003e\u003cp\u003e\u003cb\u003eTiming Test vector:\u003c/b\u003e uint32(randi(2^32,[4096*4096,1])-1)\u003c/p\u003e\u003cp\u003e\u003cb\u003eMinimum vector length/increment:\u003c/b\u003e 65536\u003c/p\u003e\u003cp\u003eHelpful, possibly, global variables.\u003c/p\u003e\u003cp\u003eb1=uint32(1431655765); b2=uint32(858993459); b3=uint32(252645135) b4=uint32(16711935); b5=uint32(65535);\u003c/p\u003e\u003cp\u003eHex: b1=55555555 b2=33333333 b3=0F0F0F0F b4=00FF00FF b5=0000FFFF\u003c/p\u003e\u003cp\u003eThe array num_ones is created for values 0-65535 (0:2^16-1).\r\nnum_ones(1)=0, num_ones(2)=1, num_ones(3)=1,num_ones(4)=2,...num_ones(65536)=15\u003c/p\u003e\u003cp\u003eDue to lack of zero indexing num_ones(value+1) is number of ones for value.\u003c/p\u003e\u003cp\u003eHint: Globals are not good for time performance.\u003c/p\u003e\u003cp\u003eHint: Segmentation appears to provide significant time optimization potential.\u003c/p\u003e","function_template":"function y = Ham(x)\r\n% Input uint32\r\nglobal num_ones b1 b2 b3 b4 b5\r\n  y = x;\r\nend","test_suite":"%%\r\nfeval(@assignin,'caller','score',2000);\r\n%%\r\nglobal num_ones b1 b2 b3 b4 b5 net_time\r\nnet_time=2000; % default in case of time out (not needed)\r\n\r\nb1=uint32(1431655765); \r\nb2=uint32(858993459);\r\nb3=uint32(252645135);\r\nb4=uint32(16711935);\r\nb5=uint32(65535);\r\n \r\nnum_ones=uint32(zeros(65536,1)); \r\nfor i=0:65535  num_ones(i+1)=length( find( bitget( i, 1:32 ) ) ) ; \r\nend % Cody 0.996 sec\r\n%%\r\nglobal num_ones b1 b2 b3 b4 b5 net_time\r\n\r\nw=uint32(randi(2^32,[65536*1,1])-1); \r\nfor i=1:4 % Clear timing\r\n  vw=Ham(w);\r\nend\r\n\r\nwexpect=num_ones(mod(w,65536)+1)+num_ones(floor(double(w)/65536)+1); %1.56\r\nt0=clock;\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\ndt=etime(clock,t0)*250*1000; % avg of 4 runs in us\r\nfprintf('Time to execute 65536 values %.0f usec\\n',dt);\r\nassert(isequal(wexpect,vw),sprintf('Time to execute 65536 values %.0f usec\\n',dt))\r\n%%\r\nglobal num_ones b1 b2 b3 b4 b5 net_time\r\n\r\nw=uint32(randi(2^32,[65536*1,1])-1); \r\n\r\n vw=Ham(w); % Three cycles of smaller vector\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n\r\nw=uint32(randi(2^32,[4096*4096,1])-1);\r\nwexpect=num_ones(mod(w,65536)+1)+num_ones(floor(double(w)/65536)+1); %1.56\r\n\r\n \r\n  vw=Ham(w); % Big Prep file\r\n  vw=Ham(w); % Big Prep file\r\n \r\nt0=clock;\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\nnet_time=etime(clock,t0)*250; % avg of 4 runs\r\nfprintf('Time to execute 4096*4096 values %.0f msec\\n',net_time);\r\n\r\nassert(isequal(wexpect,vw),sprintf('Time to execute 4096*4096 values %.0f msec\\n',net_time))\r\n%%\r\nglobal net_time\r\n% net_time in ms\r\n% Create graph data\r\nnet_time=min(2000,net_time); % Limit graph y-axis\r\n\r\nfeval(@assignin,'caller','score',floor(net_time));\r\n\r\n%fh=fopen('Ham.m','wt');\r\n%fprintf(fh,'%s\\n',repmat('1;',[1,round(net_time/2)]));\r\n%fclose(fh);","published":true,"deleted":false,"likes_count":0,"comments_count":1,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":18,"test_suite_updated_at":"2012-11-22T11:18:57.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2012-06-30T05:08:08.000Z","updated_at":"2026-02-09T12:51:11.000Z","published_at":"2012-07-01T05:19:12.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\u003eThe Hamming Weight,\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://en.wikipedia.org/wiki/Hamming_weight\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ewiki Hamming Weight\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e, in its most simple form is the number of ones in the binary representation of a value.\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 task here is to create a fast Hamming Weight function/method such that processing many 4K x 4K images of 32 bit integer can be evaluated rapidly. Saw this question posed on Stack Overflow.\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 Vector of length N of 32 bit integer values.\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 number of ones of the binary representation\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 in milliseconds to process a [4096*4096,1] 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\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eExamples:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Input [7 ; 3], output=[3;2]; [16 32], output [1;1]; [0 4294967295] output [0;32]\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\u003eTiming Test vector:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e uint32(randi(2^32,[4096*4096,1])-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\u003eMinimum vector length/increment:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e 65536\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\u003eHelpful, possibly, global variables.\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\u003eb1=uint32(1431655765); b2=uint32(858993459); b3=uint32(252645135) b4=uint32(16711935); b5=uint32(65535);\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\u003eHex: b1=55555555 b2=33333333 b3=0F0F0F0F b4=00FF00FF b5=0000FFFF\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 array num_ones is created for values 0-65535 (0:2^16-1). num_ones(1)=0, num_ones(2)=1, num_ones(3)=1,num_ones(4)=2,...num_ones(65536)=15\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\u003eDue to lack of zero indexing num_ones(value+1) is number of ones for value.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHint: Globals are not good for time performance.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHint: Segmentation appears to provide significant time optimization potential.\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":810,"title":"Hamming Weight - Size Scoring","description":"The Hamming Weight, \u003chttp://en.wikipedia.org/wiki/Hamming_weight wiki Hamming Weight\u003e, in its most simple form is the number of ones in the binary representation of a value.\r\n\r\nThe task here is to create a fast Hamming Weight function/method such that processing many 4K x 4K images of 32 bit integer can be evaluated rapidly. Saw this question posed on Stack Overflow.\r\n\r\n*Input:* Vector of length N of 32 bit integer values.\r\n\r\n*Output:* Total number of ones of the binary representation\r\n\r\n*Scoring:* Normal Cody Size, while solving multiple cases without timing out\r\n\r\nExamples: Input [7 ; 3], output=[3 ; 2];  [16 32], output [1 ; 1]; [0 4294967295]  output [ 0 ; 32] FFFFFFFF Hex = 2^32-1\r\n\r\nStressing Test vector : uint32(randi(2^32,[4096*4096,1])-1)\r\n\r\n\r\nHelpful, possibly, global variables.\r\n\r\nb1=uint32(1431655765); b2=uint32(858993459); b3=uint32(252645135) b4=uint32(16711935); b5=uint32(65535);\r\n\r\nHex: b1=55555555 b2=33333333 b3=0F0F0F0F b4=00FF00FF b5=0000FFFF\r\n\r\nThe array num_ones is created for values 0-65535 (0:2^16-1). num_ones(1)=0, num_ones(2)=1, num_ones(3)=1,num_ones(4)=2,...num_ones(65536)=15\r\n\r\nDue to lack of zero indexing num_ones(value+1) is number of ones for value.","description_html":"\u003cp\u003eThe Hamming Weight, \u003ca href=\"http://en.wikipedia.org/wiki/Hamming_weight\"\u003ewiki Hamming Weight\u003c/a\u003e, in its most simple form is the number of ones in the binary representation of a value.\u003c/p\u003e\u003cp\u003eThe task here is to create a fast Hamming Weight function/method such that processing many 4K x 4K images of 32 bit integer can be evaluated rapidly. Saw this question posed on Stack Overflow.\u003c/p\u003e\u003cp\u003e\u003cb\u003eInput:\u003c/b\u003e Vector of length N of 32 bit integer values.\u003c/p\u003e\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e Total number of ones of the binary representation\u003c/p\u003e\u003cp\u003e\u003cb\u003eScoring:\u003c/b\u003e Normal Cody Size, while solving multiple cases without timing out\u003c/p\u003e\u003cp\u003eExamples: Input [7 ; 3], output=[3 ; 2];  [16 32], output [1 ; 1]; [0 4294967295]  output [ 0 ; 32] FFFFFFFF Hex = 2^32-1\u003c/p\u003e\u003cp\u003eStressing Test vector : uint32(randi(2^32,[4096*4096,1])-1)\u003c/p\u003e\u003cp\u003eHelpful, possibly, global variables.\u003c/p\u003e\u003cp\u003eb1=uint32(1431655765); b2=uint32(858993459); b3=uint32(252645135) b4=uint32(16711935); b5=uint32(65535);\u003c/p\u003e\u003cp\u003eHex: b1=55555555 b2=33333333 b3=0F0F0F0F b4=00FF00FF b5=0000FFFF\u003c/p\u003e\u003cp\u003eThe array num_ones is created for values 0-65535 (0:2^16-1). num_ones(1)=0, num_ones(2)=1, num_ones(3)=1,num_ones(4)=2,...num_ones(65536)=15\u003c/p\u003e\u003cp\u003eDue to lack of zero indexing num_ones(value+1) is number of ones for value.\u003c/p\u003e","function_template":"function y = Ham(x)\r\n% Input uint32\r\nglobal num_ones b1 b2 b3 b4 b5\r\n  y = x;\r\nend","test_suite":"%%\r\nglobal num_ones b1 b2 b3 b4 b5 net_time\r\nnet_time=4000; % default in case of time out (not needed)\r\n\r\nb1=uint32(1431655765); \r\nb2=uint32(858993459);\r\nb3=uint32(252645135);\r\nb4=uint32(16711935);\r\nb5=uint32(65535);\r\n \r\nnum_ones=uint32(zeros(65536,1)); \r\nfor i=0:65535  num_ones(i+1)=length( find( bitget( i, 1:32 ) ) ) ; \r\nend % Cody 0.996 sec\r\n%%\r\nglobal num_ones b1 b2 b3 b4 b5 net_time\r\n\r\nw=uint32(randi(2^32,[4096*1,1])-1); \r\nfor i=1:4 % Clear timing\r\n  vw=Ham(w);\r\nend\r\n\r\nwexpect=num_ones(mod(w,65536)+1)+num_ones(floor(double(w)/65536)+1); %1.56\r\nt0=clock;\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\ndt=etime(clock,t0)*250*1000; % avg of 4 runs in us\r\nfprintf('Time to execute 4096 values %.0f usec\\n',dt);\r\nassert(isequal(wexpect,vw),sprintf('\\nTime to execute 4096 values %.0f usec\\n',dt))\r\n%%\r\nglobal num_ones b1 b2 b3 b4 b5 net_time\r\n\r\nw=uint32(randi(2^32,[4096*1,1])-1); \r\n\r\n vw=Ham(w); % Three cycles of smaller vector\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n\r\nw=uint32(randi(2^32,[4096*4096,1])-1);\r\nwexpect=num_ones(mod(w,65536)+1)+num_ones(floor(double(w)/65536)+1); %1.56\r\n\r\n \r\n  vw=Ham(w); % Big Prep file\r\n  vw=Ham(w); % Big Prep file\r\n \r\nt0=clock;\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\nnet_time=etime(clock,t0)*250; % avg of 4 runs\r\nfprintf('Time to execute 4096*4096 values %.0f msec\\n',net_time);\r\n\r\nassert(isequal(wexpect,vw),sprintf('\\nTime to execute 4096*4096 values %.0f msec\\n',net_time))\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":0,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":10,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2012-07-01T02:47:44.000Z","updated_at":"2012-07-01T05:25:20.000Z","published_at":"2012-07-01T05:25:20.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\u003eThe Hamming Weight,\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://en.wikipedia.org/wiki/Hamming_weight\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ewiki Hamming Weight\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e, in its most simple form is the number of ones in the binary representation of a value.\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 task here is to create a fast Hamming Weight function/method such that processing many 4K x 4K images of 32 bit integer can be evaluated rapidly. Saw this question posed on Stack Overflow.\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 Vector of length N of 32 bit integer values.\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 Total number of ones of the binary representation\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 Normal Cody Size, while solving multiple cases without timing out\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExamples: Input [7 ; 3], output=[3 ; 2]; [16 32], output [1 ; 1]; [0 4294967295] output [ 0 ; 32] FFFFFFFF Hex = 2^32-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:t\u003eStressing Test vector : uint32(randi(2^32,[4096*4096,1])-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:t\u003eHelpful, possibly, global variables.\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\u003eb1=uint32(1431655765); b2=uint32(858993459); b3=uint32(252645135) b4=uint32(16711935); b5=uint32(65535);\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\u003eHex: b1=55555555 b2=33333333 b3=0F0F0F0F b4=00FF00FF b5=0000FFFF\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 array num_ones is created for values 0-65535 (0:2^16-1). num_ones(1)=0, num_ones(2)=1, num_ones(3)=1,num_ones(4)=2,...num_ones(65536)=15\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\u003eDue to lack of zero indexing num_ones(value+1) is number of ones for value.\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":1900,"title":"GJam 2014 China Rd A: Rational Number Tree (Large Values)","description":"This Challenge is derived from \u003chttp://code.google.com/codejam/contest/2924486/dashboard#s=p1 GJam 2014 China Rational Number Tree\u003e.\r\n\r\nThe Goal is to determine the tree node if given [P,Q] or provide the [P,Q] if given a node. This is the Large Challenge with a Max of 64 Tree levels. Processing of uint64 size data requires extra care.\r\n\r\nConsider an infinite complete binary tree where the root node is 1/1 and the left and right children of node P/Q are P/(P+Q) and (P+Q)/Q, respectively. \r\n\r\nThe Tree looks like:\r\n\r\n         1/1\r\n    ______|______\r\n    |           |\r\n   1/2         2/1\r\n ___|___     ___|___\r\n |     |     |     |\r\n1/3   3/2   2/3   3/1\r\n\r\n\r\nThe nodes are 1/1, 1/2, 2/1, 1/3, 3/2, 2/3, 3/1,...\r\n\r\n\r\n*Input:* [N] or [P,Q] where N is a uint64 integer node or [P,Q] (double) are terms of a Node\r\n\r\n*Output:* [P,Q](double) or [N](uint64) depends on Input type\r\n\r\n*Examples:*\r\n\r\n  [Input]  [Output]\r\n    [2] [1 2]\r\n    [1 2] [2]\r\n    [5] [3 2]\r\n    [3 2] [5]\r\n\r\n*Contest Performance:* Best Delta Time of 14 minutes with only 368 able to process the large data set in less than 3 hours.\r\n\r\n*Notes:*\r\n\r\n1) Matlab has issues with uint64 for dec2bin and matrix multiplies.\r\n\r\n2) Example of uint64 read is provided in test suite comments\r\n\r\n3) Bitshift and bitget work on uint64","description_html":"\u003cp\u003eThis Challenge is derived from \u003ca href = \"http://code.google.com/codejam/contest/2924486/dashboard#s=p1\"\u003eGJam 2014 China Rational Number Tree\u003c/a\u003e.\u003c/p\u003e\u003cp\u003eThe Goal is to determine the tree node if given [P,Q] or provide the [P,Q] if given a node. This is the Large Challenge with a Max of 64 Tree levels. Processing of uint64 size data requires extra care.\u003c/p\u003e\u003cp\u003eConsider an infinite complete binary tree where the root node is 1/1 and the left and right children of node P/Q are P/(P+Q) and (P+Q)/Q, respectively.\u003c/p\u003e\u003cp\u003eThe Tree looks like:\u003c/p\u003e\u003cpre\u003e         1/1\r\n    ______|______\r\n    |           |\r\n   1/2         2/1\r\n ___|___     ___|___\r\n |     |     |     |\r\n1/3   3/2   2/3   3/1\u003c/pre\u003e\u003cp\u003eThe nodes are 1/1, 1/2, 2/1, 1/3, 3/2, 2/3, 3/1,...\u003c/p\u003e\u003cp\u003e\u003cb\u003eInput:\u003c/b\u003e [N] or [P,Q] where N is a uint64 integer node or [P,Q] (double) are terms of a Node\u003c/p\u003e\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e [P,Q](double) or [N](uint64) depends on Input type\u003c/p\u003e\u003cp\u003e\u003cb\u003eExamples:\u003c/b\u003e\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e[Input]  [Output]\r\n  [2] [1 2]\r\n  [1 2] [2]\r\n  [5] [3 2]\r\n  [3 2] [5]\r\n\u003c/pre\u003e\u003cp\u003e\u003cb\u003eContest Performance:\u003c/b\u003e Best Delta Time of 14 minutes with only 368 able to process the large data set in less than 3 hours.\u003c/p\u003e\u003cp\u003e\u003cb\u003eNotes:\u003c/b\u003e\u003c/p\u003e\u003cp\u003e1) Matlab has issues with uint64 for dec2bin and matrix multiplies.\u003c/p\u003e\u003cp\u003e2) Example of uint64 read is provided in test suite comments\u003c/p\u003e\u003cp\u003e3) Bitshift and bitget work on uint64\u003c/p\u003e","function_template":"function vout=Tree_CH(v);\r\n  vout=0;\r\nend","test_suite":"%%\r\ntic\r\nv=[          2081355757           4898583766 ];\r\nvexp=[uint64(11412103587704585708) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        385960903003         298051413714 ];\r\nvexp=[uint64(13079621846505187505) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(15477096172227810860) ];\r\nvexp=[        188445238409         450375998772 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 6473043965665514375) ];\r\nvexp=[        109230282567          33788952110 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(11270280438309969755) ];\r\nvexp=[         23328302733           8557676614 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[          6893018069           8719066441 ];\r\nvexp=[uint64( 1875942192845872366) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 6566840053865194126) ];\r\nvexp=[         65441249939          85425641391 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         21562167101          16520535073 ];\r\nvexp=[uint64(  956959952027690353) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[          1684200303           7336396097 ];\r\nvexp=[uint64(  871479491406563248) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 2524835851416755114) ];\r\nvexp=[         28589000023          46221104912 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        154908421282         199691474905 ];\r\nvexp=[uint64( 7885684695614904270) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 8176570117722182781) ];\r\nvexp=[         40721598494          22123450931 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 9379940828518631730) ];\r\nvexp=[         37341631466          63773070917 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        232141051889         328442531011 ];\r\nvexp=[uint64( 9972945692012784742) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 9539984397959825908) ];\r\nvexp=[         70384313011         178968141063 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        421484284721         599703187188 ];\r\nvexp=[uint64(17162693345262485798) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        127506314007          35528864794 ];\r\nvexp=[uint64( 4167185002280551319) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         11313093168          21049073135 ];\r\nvexp=[uint64(16014443480831614722) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 7450646608300783266) ];\r\nvexp=[         83429116694         148768825269 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(13305041571212833467) ];\r\nvexp=[         77301627056          27774083789 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(13635447149676152154) ];\r\nvexp=[         90797761304         143479563807 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(11002893257806672560) ];\r\nvexp=[         44637053272         195624712811 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(11051724027090761936) ];\r\nvexp=[         41361095819         189799709732 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(16687200783289968235) ];\r\nvexp=[        552258283575         209936061221 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         54164851566         156315474505 ];\r\nvexp=[uint64(13714337615447966724) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 7329431770178715643) ];\r\nvexp=[         13299036287           4535592331 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(16479631524350748877) ];\r\nvexp=[        165234451641          96812407705 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 6965988866158891263) ];\r\nvexp=[         21216614218           2585039947 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 2910455928437551420) ];\r\nvexp=[          6642417815          14808129701 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         13173276049          27570716815 ];\r\nvexp=[uint64( 4977980945016614908) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(16721022657493559975) ];\r\nvexp=[         65473107451          19359384006 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         16946040975          10725070912 ];\r\nvexp=[uint64( 6003309882203701925) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 2198342485367409266) ];\r\nvexp=[         23006488657          39032588924 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        120013217139          50302005308 ];\r\nvexp=[uint64(14344732783514005715) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         57191814347          77886936688 ];\r\nvexp=[uint64( 8056110860202858614) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(  971778736353519486) ];\r\nvexp=[         17760921544          20384041659 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        129560016431         165770554719 ];\r\nvexp=[uint64(15111464173338859822) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(17855284157674478998) ];\r\nvexp=[        215281025997         298483051015 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[          8822618162           5678585885 ];\r\nvexp=[uint64( 4934822377188433797) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(  632138406214928188) ];\r\nvexp=[          1835073727           4084131059 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(17330627945660580534) ];\r\nvexp=[         41195382388          56325602793 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[          6130953526          13549540271 ];\r\nvexp=[uint64( 1896894898836571068) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         25774822611          36694510870 ];\r\nvexp=[uint64(15996304402734859814) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 9847540446768144862) ];\r\nvexp=[         31041969169          37560295989 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(16612432143632526945) ];\r\nvexp=[         74531365158          60753898717 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(18413647273680019461) ];\r\nvexp=[         10651217970           6995653927 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         62279662838          47373225825 ];\r\nvexp=[uint64( 9640047776936252913) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(16583979163800903818) ];\r\nvexp=[        114249431453         187605944484 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[          9050571726           1557293089 ];\r\nvexp=[uint64( 9060257062169994207) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(12613321696385576431) ];\r\nvexp=[        125835757545          26079146381 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         35625689704          19896848117 ];\r\nvexp=[uint64( 4411487194398480861) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 3484689439967270015) ];\r\nvexp=[         38461243033           5285269728 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         16085645428          43823408873 ];\r\nvexp=[uint64(  894482490922679460) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(12060487473657709988) ];\r\nvexp=[        250877787515         682594856898 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        143931327293          76531069545 ];\r\nvexp=[uint64(12786697318005254653) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(14215963369683634045) ];\r\nvexp=[        100316121935          54160354223 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[          6557653153          12053301655 ];\r\nvexp=[uint64(  343376420813762434) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         47494216762         112901758075 ];\r\nvexp=[uint64( 3772080181899583404) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 6766488059228584288) ];\r\nvexp=[          8189923863          44106320581 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         41526542352          17027036567 ];\r\nvexp=[uint64(17583140790467836275) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[          7196361275           6496875429 ];\r\nvexp=[uint64( 6443574928762444801) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(17365843048376429107) ];\r\nvexp=[         18457341831           7658711707 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         78140576077          33767545674 ];\r\nvexp=[uint64( 6098897546038113251) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[          6734445697          12144787423 ];\r\nvexp=[uint64( 4770215408132554690) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        426726665699         126968827572 ];\r\nvexp=[uint64(12995540462042527271) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 5523416580603863552) ];\r\nvexp=[          4255700693          41585972826 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 3928457774057619184) ];\r\nvexp=[          3877602413          16306596859 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        136097916108          28801327007 ];\r\nvexp=[uint64( 5984770176263773551) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(16263054952801281548) ];\r\nvexp=[         14189368539          34874268638 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        662028875555         839442017139 ];\r\nvexp=[uint64(11859750004469197678) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         61393402621         332298132605 ];\r\nvexp=[uint64( 7879625004656522848) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[          1998630709           8715852076 ];\r\nvexp=[uint64(  429234479783941040) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         46021740257          21715449429 ];\r\nvexp=[uint64(14347105153381215235) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(  498971859648614985) ];\r\nvexp=[         30472124134          22307121041 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 6396162213707057064) ];\r\nvexp=[         26683188512          96283024611 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         46925294325          33937979441 ];\r\nvexp=[uint64( 1189627028589296041) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         76315837293          99424320902 ];\r\nvexp=[uint64(11656949174638158734) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         81880058662          38464981613 ];\r\nvexp=[uint64( 7071867154613101059) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(10763027175299443277) ];\r\nvexp=[        330973468653         194345162402 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         79087591419          23535674032 ];\r\nvexp=[uint64(15017024826981320231) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        241564818347         155909390759 ];\r\nvexp=[uint64(15575297637481852549) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         45079186819         295776092742 ];\r\nvexp=[uint64(16532545499676710720) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         22268771423          34477255167 ];\r\nvexp=[uint64(11007510539515659130) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 5666822869473456046) ];\r\nvexp=[         28640204779          36488487629 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(14041232599151835393) ];\r\nvexp=[         28442192536          25169506635 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 9611808239041397120) ];\r\nvexp=[          1055826287           7876555270 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        358405360794         498368744795 ];\r\nvexp=[uint64( 4955841772239017238) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 3979638765694296062) ];\r\nvexp=[          6903410593           7465748396 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        151761015812         211325030677 ];\r\nvexp=[uint64( 7987025462351843862) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        325266071703          87446899892 ];\r\nvexp=[uint64(16944641327505127607) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 3273057505586617135) ];\r\nvexp=[        124425395619          27124979723 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        142722642746          60293518411 ];\r\nvexp=[uint64(10857549622062058131) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        379488415355         462240655923 ];\r\nvexp=[uint64(16693497308621268574) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 5336487237226370963) ];\r\nvexp=[         76728175571          32349586492 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        366909173981         111494050923 ];\r\nvexp=[uint64(11044880014193584327) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 2767480983943234275) ];\r\nvexp=[        155407720950          67429662431 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[          4375152441          11923449250 ];\r\nvexp=[uint64(14458798758395123876) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        107946470399          33010545783 ];\r\nvexp=[uint64(13708745226178508359) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(  546205200272961984) ];\r\nvexp=[          1773793574          11180737745 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 6791165959744564855) ];\r\nvexp=[        125726750555          33307435529 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\ntoc\r\n%%\r\n% Read of GJam 64 bit data\r\n% function z = read_input64(fn)\r\n%  fid=fopen(fn);\r\n%  u=textscan(fid,'%u64');\r\n%  fclose(fid);\r\n%  vptr=2; % skip Test case count \r\n%  dptr=1;\r\n%  while vptr\u003csize(u{1},1)\r\n%   if u{1}(vptr)==1 % single N\r\n%    z{dptr}=u{1}(vptr+1);\r\n%    vptr=vptr+2;\r\n%   else % Pair for p/q\r\n%    z{dptr}=[u{1}(vptr+1) u{1}(vptr+2)];\r\n%    vptr=vptr+3;\r\n%   end\r\n%   dptr=dptr+1;\r\n%  end\r\n% end\r\n%Typical GJam 64 bit data\r\n% 100\r\n% 2 2081355757 4898583766\r\n% 2 385960903003 298051413714\r\n% 1 15477096172227810860\r\n% 1 6473043965665514375\r\n% 1 11270280438309969755\r\n% 2 6893018069 8719066441\r\n% 1 6566840053865194126\r\n% 2 21562167101 16520535073\r\n% 2 1684200303 7336396097\r\n% 1 2524835851416755114\r\n% 2 154908421282 199691474905\r\n","published":true,"deleted":false,"likes_count":0,"comments_count":0,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":5,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2013-09-29T03:53:20.000Z","updated_at":"2013-09-29T21:22:24.000Z","published_at":"2013-09-29T04:19:40.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/2924486/dashboard#s=p1\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eGJam 2014 China Rational Number Tree\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\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe Goal is to determine the tree node if given [P,Q] or provide the [P,Q] if given a node. This is the Large Challenge with a Max of 64 Tree levels. Processing of uint64 size data requires extra care.\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\u003eConsider an infinite complete binary tree where the root node is 1/1 and the left and right children of node P/Q are P/(P+Q) and (P+Q)/Q, respectively.\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 Tree looks like:\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/1\\n    ______|______\\n    |           |\\n   1/2         2/1\\n ___|___     ___|___\\n |     |     |     |\\n1/3   3/2   2/3   3/1]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe nodes are 1/1, 1/2, 2/1, 1/3, 3/2, 2/3, 3/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\u003eInput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e [N] or [P,Q] where N is a uint64 integer node or [P,Q] (double) are terms of a Node\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 [P,Q](double) or [N](uint64) depends on Input type\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eExamples:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[[Input]  [Output]\\n  [2] [1 2]\\n  [1 2] [2]\\n  [5] [3 2]\\n  [3 2] [5]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eContest Performance:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Best Delta Time of 14 minutes with only 368 able to process the large data set in less than 3 hours.\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\u003eNotes:\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\u003e1) Matlab has issues with uint64 for dec2bin and matrix multiplies.\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\u003e2) Example of uint64 read is provided in test suite comments\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\u003e3) Bitshift and bitget work on uint64\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":808,"title":"Hamming Weight - Fast","description":"The Hamming Weight, \u003chttp://en.wikipedia.org/wiki/Hamming_weight wiki Hamming Weight\u003e, in its most simple form is the number of ones in the binary representation of a value.\r\n\r\nThe task here is to create a fast Hamming Weight function/method such that processing many 4K x 4K images of 32 bit integer can be evaluated rapidly. Saw this question posed on Stack Overflow.\r\n\r\n*Input:* Vector of length N of 32 bit integer values.\r\n\r\n*Output:* Vector of number of ones of the binary representation\r\n\r\n*Scoring:* Time in milliseconds to process a [4096*4096,1] vector\r\n\r\n*Examples:* Input [7 ; 3], output=[3;2];  [16 32], output [1;1]; [0 4294967295] output [0;32]\r\n\r\n*Timing Test vector:* uint32(randi(2^32,[4096*4096,1])-1)\r\n\r\n*Minimum vector length/increment:* 65536\r\n\r\nHelpful, possibly, global variables.\r\n\r\nb1=uint32(1431655765); b2=uint32(858993459); b3=uint32(252645135) b4=uint32(16711935); b5=uint32(65535);\r\n\r\nHex: b1=55555555 b2=33333333 b3=0F0F0F0F b4=00FF00FF b5=0000FFFF \r\n\r\nThe array num_ones is created for values 0-65535 (0:2^16-1).\r\nnum_ones(1)=0, num_ones(2)=1, num_ones(3)=1,num_ones(4)=2,...num_ones(65536)=15\r\n\r\nDue to lack of zero indexing num_ones(value+1) is number of ones for value.\r\n\r\n\r\nHint: Globals are not good for time performance.\r\n\r\nHint: Segmentation appears to provide significant time optimization potential.\r\n\r\n\r\n\r\n","description_html":"\u003cp\u003eThe Hamming Weight, \u003ca href=\"http://en.wikipedia.org/wiki/Hamming_weight\"\u003ewiki Hamming Weight\u003c/a\u003e, in its most simple form is the number of ones in the binary representation of a value.\u003c/p\u003e\u003cp\u003eThe task here is to create a fast Hamming Weight function/method such that processing many 4K x 4K images of 32 bit integer can be evaluated rapidly. Saw this question posed on Stack Overflow.\u003c/p\u003e\u003cp\u003e\u003cb\u003eInput:\u003c/b\u003e Vector of length N of 32 bit integer values.\u003c/p\u003e\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e Vector of number of ones of the binary representation\u003c/p\u003e\u003cp\u003e\u003cb\u003eScoring:\u003c/b\u003e Time in milliseconds to process a [4096*4096,1] vector\u003c/p\u003e\u003cp\u003e\u003cb\u003eExamples:\u003c/b\u003e Input [7 ; 3], output=[3;2];  [16 32], output [1;1]; [0 4294967295] output [0;32]\u003c/p\u003e\u003cp\u003e\u003cb\u003eTiming Test vector:\u003c/b\u003e uint32(randi(2^32,[4096*4096,1])-1)\u003c/p\u003e\u003cp\u003e\u003cb\u003eMinimum vector length/increment:\u003c/b\u003e 65536\u003c/p\u003e\u003cp\u003eHelpful, possibly, global variables.\u003c/p\u003e\u003cp\u003eb1=uint32(1431655765); b2=uint32(858993459); b3=uint32(252645135) b4=uint32(16711935); b5=uint32(65535);\u003c/p\u003e\u003cp\u003eHex: b1=55555555 b2=33333333 b3=0F0F0F0F b4=00FF00FF b5=0000FFFF\u003c/p\u003e\u003cp\u003eThe array num_ones is created for values 0-65535 (0:2^16-1).\r\nnum_ones(1)=0, num_ones(2)=1, num_ones(3)=1,num_ones(4)=2,...num_ones(65536)=15\u003c/p\u003e\u003cp\u003eDue to lack of zero indexing num_ones(value+1) is number of ones for value.\u003c/p\u003e\u003cp\u003eHint: Globals are not good for time performance.\u003c/p\u003e\u003cp\u003eHint: Segmentation appears to provide significant time optimization potential.\u003c/p\u003e","function_template":"function y = Ham(x)\r\n% Input uint32\r\nglobal num_ones b1 b2 b3 b4 b5\r\n  y = x;\r\nend","test_suite":"%%\r\nfeval(@assignin,'caller','score',2000);\r\n%%\r\nglobal num_ones b1 b2 b3 b4 b5 net_time\r\nnet_time=2000; % default in case of time out (not needed)\r\n\r\nb1=uint32(1431655765); \r\nb2=uint32(858993459);\r\nb3=uint32(252645135);\r\nb4=uint32(16711935);\r\nb5=uint32(65535);\r\n \r\nnum_ones=uint32(zeros(65536,1)); \r\nfor i=0:65535  num_ones(i+1)=length( find( bitget( i, 1:32 ) ) ) ; \r\nend % Cody 0.996 sec\r\n%%\r\nglobal num_ones b1 b2 b3 b4 b5 net_time\r\n\r\nw=uint32(randi(2^32,[65536*1,1])-1); \r\nfor i=1:4 % Clear timing\r\n  vw=Ham(w);\r\nend\r\n\r\nwexpect=num_ones(mod(w,65536)+1)+num_ones(floor(double(w)/65536)+1); %1.56\r\nt0=clock;\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\ndt=etime(clock,t0)*250*1000; % avg of 4 runs in us\r\nfprintf('Time to execute 65536 values %.0f usec\\n',dt);\r\nassert(isequal(wexpect,vw),sprintf('Time to execute 65536 values %.0f usec\\n',dt))\r\n%%\r\nglobal num_ones b1 b2 b3 b4 b5 net_time\r\n\r\nw=uint32(randi(2^32,[65536*1,1])-1); \r\n\r\n vw=Ham(w); % Three cycles of smaller vector\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n\r\nw=uint32(randi(2^32,[4096*4096,1])-1);\r\nwexpect=num_ones(mod(w,65536)+1)+num_ones(floor(double(w)/65536)+1); %1.56\r\n\r\n \r\n  vw=Ham(w); % Big Prep file\r\n  vw=Ham(w); % Big Prep file\r\n \r\nt0=clock;\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\nnet_time=etime(clock,t0)*250; % avg of 4 runs\r\nfprintf('Time to execute 4096*4096 values %.0f msec\\n',net_time);\r\n\r\nassert(isequal(wexpect,vw),sprintf('Time to execute 4096*4096 values %.0f msec\\n',net_time))\r\n%%\r\nglobal net_time\r\n% net_time in ms\r\n% Create graph data\r\nnet_time=min(2000,net_time); % Limit graph y-axis\r\n\r\nfeval(@assignin,'caller','score',floor(net_time));\r\n\r\n%fh=fopen('Ham.m','wt');\r\n%fprintf(fh,'%s\\n',repmat('1;',[1,round(net_time/2)]));\r\n%fclose(fh);","published":true,"deleted":false,"likes_count":0,"comments_count":1,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":18,"test_suite_updated_at":"2012-11-22T11:18:57.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2012-06-30T05:08:08.000Z","updated_at":"2026-02-09T12:51:11.000Z","published_at":"2012-07-01T05:19:12.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\u003eThe Hamming Weight,\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://en.wikipedia.org/wiki/Hamming_weight\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ewiki Hamming Weight\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e, in its most simple form is the number of ones in the binary representation of a value.\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 task here is to create a fast Hamming Weight function/method such that processing many 4K x 4K images of 32 bit integer can be evaluated rapidly. Saw this question posed on Stack Overflow.\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 Vector of length N of 32 bit integer values.\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 number of ones of the binary representation\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 in milliseconds to process a [4096*4096,1] 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\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eExamples:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Input [7 ; 3], output=[3;2]; [16 32], output [1;1]; [0 4294967295] output [0;32]\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\u003eTiming Test vector:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e uint32(randi(2^32,[4096*4096,1])-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\u003eMinimum vector length/increment:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e 65536\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\u003eHelpful, possibly, global variables.\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\u003eb1=uint32(1431655765); b2=uint32(858993459); b3=uint32(252645135) b4=uint32(16711935); b5=uint32(65535);\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\u003eHex: b1=55555555 b2=33333333 b3=0F0F0F0F b4=00FF00FF b5=0000FFFF\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 array num_ones is created for values 0-65535 (0:2^16-1). num_ones(1)=0, num_ones(2)=1, num_ones(3)=1,num_ones(4)=2,...num_ones(65536)=15\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\u003eDue to lack of zero indexing num_ones(value+1) is number of ones for value.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHint: Globals are not good for time performance.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHint: Segmentation appears to provide significant time optimization potential.\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":810,"title":"Hamming Weight - Size Scoring","description":"The Hamming Weight, \u003chttp://en.wikipedia.org/wiki/Hamming_weight wiki Hamming Weight\u003e, in its most simple form is the number of ones in the binary representation of a value.\r\n\r\nThe task here is to create a fast Hamming Weight function/method such that processing many 4K x 4K images of 32 bit integer can be evaluated rapidly. Saw this question posed on Stack Overflow.\r\n\r\n*Input:* Vector of length N of 32 bit integer values.\r\n\r\n*Output:* Total number of ones of the binary representation\r\n\r\n*Scoring:* Normal Cody Size, while solving multiple cases without timing out\r\n\r\nExamples: Input [7 ; 3], output=[3 ; 2];  [16 32], output [1 ; 1]; [0 4294967295]  output [ 0 ; 32] FFFFFFFF Hex = 2^32-1\r\n\r\nStressing Test vector : uint32(randi(2^32,[4096*4096,1])-1)\r\n\r\n\r\nHelpful, possibly, global variables.\r\n\r\nb1=uint32(1431655765); b2=uint32(858993459); b3=uint32(252645135) b4=uint32(16711935); b5=uint32(65535);\r\n\r\nHex: b1=55555555 b2=33333333 b3=0F0F0F0F b4=00FF00FF b5=0000FFFF\r\n\r\nThe array num_ones is created for values 0-65535 (0:2^16-1). num_ones(1)=0, num_ones(2)=1, num_ones(3)=1,num_ones(4)=2,...num_ones(65536)=15\r\n\r\nDue to lack of zero indexing num_ones(value+1) is number of ones for value.","description_html":"\u003cp\u003eThe Hamming Weight, \u003ca href=\"http://en.wikipedia.org/wiki/Hamming_weight\"\u003ewiki Hamming Weight\u003c/a\u003e, in its most simple form is the number of ones in the binary representation of a value.\u003c/p\u003e\u003cp\u003eThe task here is to create a fast Hamming Weight function/method such that processing many 4K x 4K images of 32 bit integer can be evaluated rapidly. Saw this question posed on Stack Overflow.\u003c/p\u003e\u003cp\u003e\u003cb\u003eInput:\u003c/b\u003e Vector of length N of 32 bit integer values.\u003c/p\u003e\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e Total number of ones of the binary representation\u003c/p\u003e\u003cp\u003e\u003cb\u003eScoring:\u003c/b\u003e Normal Cody Size, while solving multiple cases without timing out\u003c/p\u003e\u003cp\u003eExamples: Input [7 ; 3], output=[3 ; 2];  [16 32], output [1 ; 1]; [0 4294967295]  output [ 0 ; 32] FFFFFFFF Hex = 2^32-1\u003c/p\u003e\u003cp\u003eStressing Test vector : uint32(randi(2^32,[4096*4096,1])-1)\u003c/p\u003e\u003cp\u003eHelpful, possibly, global variables.\u003c/p\u003e\u003cp\u003eb1=uint32(1431655765); b2=uint32(858993459); b3=uint32(252645135) b4=uint32(16711935); b5=uint32(65535);\u003c/p\u003e\u003cp\u003eHex: b1=55555555 b2=33333333 b3=0F0F0F0F b4=00FF00FF b5=0000FFFF\u003c/p\u003e\u003cp\u003eThe array num_ones is created for values 0-65535 (0:2^16-1). num_ones(1)=0, num_ones(2)=1, num_ones(3)=1,num_ones(4)=2,...num_ones(65536)=15\u003c/p\u003e\u003cp\u003eDue to lack of zero indexing num_ones(value+1) is number of ones for value.\u003c/p\u003e","function_template":"function y = Ham(x)\r\n% Input uint32\r\nglobal num_ones b1 b2 b3 b4 b5\r\n  y = x;\r\nend","test_suite":"%%\r\nglobal num_ones b1 b2 b3 b4 b5 net_time\r\nnet_time=4000; % default in case of time out (not needed)\r\n\r\nb1=uint32(1431655765); \r\nb2=uint32(858993459);\r\nb3=uint32(252645135);\r\nb4=uint32(16711935);\r\nb5=uint32(65535);\r\n \r\nnum_ones=uint32(zeros(65536,1)); \r\nfor i=0:65535  num_ones(i+1)=length( find( bitget( i, 1:32 ) ) ) ; \r\nend % Cody 0.996 sec\r\n%%\r\nglobal num_ones b1 b2 b3 b4 b5 net_time\r\n\r\nw=uint32(randi(2^32,[4096*1,1])-1); \r\nfor i=1:4 % Clear timing\r\n  vw=Ham(w);\r\nend\r\n\r\nwexpect=num_ones(mod(w,65536)+1)+num_ones(floor(double(w)/65536)+1); %1.56\r\nt0=clock;\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\ndt=etime(clock,t0)*250*1000; % avg of 4 runs in us\r\nfprintf('Time to execute 4096 values %.0f usec\\n',dt);\r\nassert(isequal(wexpect,vw),sprintf('\\nTime to execute 4096 values %.0f usec\\n',dt))\r\n%%\r\nglobal num_ones b1 b2 b3 b4 b5 net_time\r\n\r\nw=uint32(randi(2^32,[4096*1,1])-1); \r\n\r\n vw=Ham(w); % Three cycles of smaller vector\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n\r\nw=uint32(randi(2^32,[4096*4096,1])-1);\r\nwexpect=num_ones(mod(w,65536)+1)+num_ones(floor(double(w)/65536)+1); %1.56\r\n\r\n \r\n  vw=Ham(w); % Big Prep file\r\n  vw=Ham(w); % Big Prep file\r\n \r\nt0=clock;\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\nnet_time=etime(clock,t0)*250; % avg of 4 runs\r\nfprintf('Time to execute 4096*4096 values %.0f msec\\n',net_time);\r\n\r\nassert(isequal(wexpect,vw),sprintf('\\nTime to execute 4096*4096 values %.0f msec\\n',net_time))\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":0,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":10,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2012-07-01T02:47:44.000Z","updated_at":"2012-07-01T05:25:20.000Z","published_at":"2012-07-01T05:25:20.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\u003eThe Hamming Weight,\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://en.wikipedia.org/wiki/Hamming_weight\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ewiki Hamming Weight\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e, in its most simple form is the number of ones in the binary representation of a value.\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 task here is to create a fast Hamming Weight function/method such that processing many 4K x 4K images of 32 bit integer can be evaluated rapidly. Saw this question posed on Stack Overflow.\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 Vector of length N of 32 bit integer values.\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 Total number of ones of the binary representation\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 Normal Cody Size, while solving multiple cases without timing out\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExamples: Input [7 ; 3], output=[3 ; 2]; [16 32], output [1 ; 1]; [0 4294967295] output [ 0 ; 32] FFFFFFFF Hex = 2^32-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:t\u003eStressing Test vector : uint32(randi(2^32,[4096*4096,1])-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:t\u003eHelpful, possibly, global variables.\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\u003eb1=uint32(1431655765); b2=uint32(858993459); b3=uint32(252645135) b4=uint32(16711935); b5=uint32(65535);\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\u003eHex: b1=55555555 b2=33333333 b3=0F0F0F0F b4=00FF00FF b5=0000FFFF\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 array num_ones is created for values 0-65535 (0:2^16-1). num_ones(1)=0, num_ones(2)=1, num_ones(3)=1,num_ones(4)=2,...num_ones(65536)=15\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\u003eDue to lack of zero indexing num_ones(value+1) is number of ones for value.\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":1900,"title":"GJam 2014 China Rd A: Rational Number Tree (Large Values)","description":"This Challenge is derived from \u003chttp://code.google.com/codejam/contest/2924486/dashboard#s=p1 GJam 2014 China Rational Number Tree\u003e.\r\n\r\nThe Goal is to determine the tree node if given [P,Q] or provide the [P,Q] if given a node. This is the Large Challenge with a Max of 64 Tree levels. Processing of uint64 size data requires extra care.\r\n\r\nConsider an infinite complete binary tree where the root node is 1/1 and the left and right children of node P/Q are P/(P+Q) and (P+Q)/Q, respectively. \r\n\r\nThe Tree looks like:\r\n\r\n         1/1\r\n    ______|______\r\n    |           |\r\n   1/2         2/1\r\n ___|___     ___|___\r\n |     |     |     |\r\n1/3   3/2   2/3   3/1\r\n\r\n\r\nThe nodes are 1/1, 1/2, 2/1, 1/3, 3/2, 2/3, 3/1,...\r\n\r\n\r\n*Input:* [N] or [P,Q] where N is a uint64 integer node or [P,Q] (double) are terms of a Node\r\n\r\n*Output:* [P,Q](double) or [N](uint64) depends on Input type\r\n\r\n*Examples:*\r\n\r\n  [Input]  [Output]\r\n    [2] [1 2]\r\n    [1 2] [2]\r\n    [5] [3 2]\r\n    [3 2] [5]\r\n\r\n*Contest Performance:* Best Delta Time of 14 minutes with only 368 able to process the large data set in less than 3 hours.\r\n\r\n*Notes:*\r\n\r\n1) Matlab has issues with uint64 for dec2bin and matrix multiplies.\r\n\r\n2) Example of uint64 read is provided in test suite comments\r\n\r\n3) Bitshift and bitget work on uint64","description_html":"\u003cp\u003eThis Challenge is derived from \u003ca href = \"http://code.google.com/codejam/contest/2924486/dashboard#s=p1\"\u003eGJam 2014 China Rational Number Tree\u003c/a\u003e.\u003c/p\u003e\u003cp\u003eThe Goal is to determine the tree node if given [P,Q] or provide the [P,Q] if given a node. This is the Large Challenge with a Max of 64 Tree levels. Processing of uint64 size data requires extra care.\u003c/p\u003e\u003cp\u003eConsider an infinite complete binary tree where the root node is 1/1 and the left and right children of node P/Q are P/(P+Q) and (P+Q)/Q, respectively.\u003c/p\u003e\u003cp\u003eThe Tree looks like:\u003c/p\u003e\u003cpre\u003e         1/1\r\n    ______|______\r\n    |           |\r\n   1/2         2/1\r\n ___|___     ___|___\r\n |     |     |     |\r\n1/3   3/2   2/3   3/1\u003c/pre\u003e\u003cp\u003eThe nodes are 1/1, 1/2, 2/1, 1/3, 3/2, 2/3, 3/1,...\u003c/p\u003e\u003cp\u003e\u003cb\u003eInput:\u003c/b\u003e [N] or [P,Q] where N is a uint64 integer node or [P,Q] (double) are terms of a Node\u003c/p\u003e\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e [P,Q](double) or [N](uint64) depends on Input type\u003c/p\u003e\u003cp\u003e\u003cb\u003eExamples:\u003c/b\u003e\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e[Input]  [Output]\r\n  [2] [1 2]\r\n  [1 2] [2]\r\n  [5] [3 2]\r\n  [3 2] [5]\r\n\u003c/pre\u003e\u003cp\u003e\u003cb\u003eContest Performance:\u003c/b\u003e Best Delta Time of 14 minutes with only 368 able to process the large data set in less than 3 hours.\u003c/p\u003e\u003cp\u003e\u003cb\u003eNotes:\u003c/b\u003e\u003c/p\u003e\u003cp\u003e1) Matlab has issues with uint64 for dec2bin and matrix multiplies.\u003c/p\u003e\u003cp\u003e2) Example of uint64 read is provided in test suite comments\u003c/p\u003e\u003cp\u003e3) Bitshift and bitget work on uint64\u003c/p\u003e","function_template":"function vout=Tree_CH(v);\r\n  vout=0;\r\nend","test_suite":"%%\r\ntic\r\nv=[          2081355757           4898583766 ];\r\nvexp=[uint64(11412103587704585708) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        385960903003         298051413714 ];\r\nvexp=[uint64(13079621846505187505) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(15477096172227810860) ];\r\nvexp=[        188445238409         450375998772 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 6473043965665514375) ];\r\nvexp=[        109230282567          33788952110 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(11270280438309969755) ];\r\nvexp=[         23328302733           8557676614 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[          6893018069           8719066441 ];\r\nvexp=[uint64( 1875942192845872366) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 6566840053865194126) ];\r\nvexp=[         65441249939          85425641391 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         21562167101          16520535073 ];\r\nvexp=[uint64(  956959952027690353) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[          1684200303           7336396097 ];\r\nvexp=[uint64(  871479491406563248) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 2524835851416755114) ];\r\nvexp=[         28589000023          46221104912 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        154908421282         199691474905 ];\r\nvexp=[uint64( 7885684695614904270) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 8176570117722182781) ];\r\nvexp=[         40721598494          22123450931 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 9379940828518631730) ];\r\nvexp=[         37341631466          63773070917 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        232141051889         328442531011 ];\r\nvexp=[uint64( 9972945692012784742) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 9539984397959825908) ];\r\nvexp=[         70384313011         178968141063 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        421484284721         599703187188 ];\r\nvexp=[uint64(17162693345262485798) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        127506314007          35528864794 ];\r\nvexp=[uint64( 4167185002280551319) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         11313093168          21049073135 ];\r\nvexp=[uint64(16014443480831614722) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 7450646608300783266) ];\r\nvexp=[         83429116694         148768825269 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(13305041571212833467) ];\r\nvexp=[         77301627056          27774083789 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(13635447149676152154) ];\r\nvexp=[         90797761304         143479563807 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(11002893257806672560) ];\r\nvexp=[         44637053272         195624712811 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(11051724027090761936) ];\r\nvexp=[         41361095819         189799709732 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(16687200783289968235) ];\r\nvexp=[        552258283575         209936061221 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         54164851566         156315474505 ];\r\nvexp=[uint64(13714337615447966724) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 7329431770178715643) ];\r\nvexp=[         13299036287           4535592331 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(16479631524350748877) ];\r\nvexp=[        165234451641          96812407705 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 6965988866158891263) ];\r\nvexp=[         21216614218           2585039947 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 2910455928437551420) ];\r\nvexp=[          6642417815          14808129701 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         13173276049          27570716815 ];\r\nvexp=[uint64( 4977980945016614908) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(16721022657493559975) ];\r\nvexp=[         65473107451          19359384006 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         16946040975          10725070912 ];\r\nvexp=[uint64( 6003309882203701925) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 2198342485367409266) ];\r\nvexp=[         23006488657          39032588924 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        120013217139          50302005308 ];\r\nvexp=[uint64(14344732783514005715) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         57191814347          77886936688 ];\r\nvexp=[uint64( 8056110860202858614) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(  971778736353519486) ];\r\nvexp=[         17760921544          20384041659 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        129560016431         165770554719 ];\r\nvexp=[uint64(15111464173338859822) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(17855284157674478998) ];\r\nvexp=[        215281025997         298483051015 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[          8822618162           5678585885 ];\r\nvexp=[uint64( 4934822377188433797) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(  632138406214928188) ];\r\nvexp=[          1835073727           4084131059 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(17330627945660580534) ];\r\nvexp=[         41195382388          56325602793 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[          6130953526          13549540271 ];\r\nvexp=[uint64( 1896894898836571068) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         25774822611          36694510870 ];\r\nvexp=[uint64(15996304402734859814) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 9847540446768144862) ];\r\nvexp=[         31041969169          37560295989 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(16612432143632526945) ];\r\nvexp=[         74531365158          60753898717 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(18413647273680019461) ];\r\nvexp=[         10651217970           6995653927 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         62279662838          47373225825 ];\r\nvexp=[uint64( 9640047776936252913) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(16583979163800903818) ];\r\nvexp=[        114249431453         187605944484 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[          9050571726           1557293089 ];\r\nvexp=[uint64( 9060257062169994207) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(12613321696385576431) ];\r\nvexp=[        125835757545          26079146381 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         35625689704          19896848117 ];\r\nvexp=[uint64( 4411487194398480861) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 3484689439967270015) ];\r\nvexp=[         38461243033           5285269728 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         16085645428          43823408873 ];\r\nvexp=[uint64(  894482490922679460) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(12060487473657709988) ];\r\nvexp=[        250877787515         682594856898 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        143931327293          76531069545 ];\r\nvexp=[uint64(12786697318005254653) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(14215963369683634045) ];\r\nvexp=[        100316121935          54160354223 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[          6557653153          12053301655 ];\r\nvexp=[uint64(  343376420813762434) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         47494216762         112901758075 ];\r\nvexp=[uint64( 3772080181899583404) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 6766488059228584288) ];\r\nvexp=[          8189923863          44106320581 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         41526542352          17027036567 ];\r\nvexp=[uint64(17583140790467836275) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[          7196361275           6496875429 ];\r\nvexp=[uint64( 6443574928762444801) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(17365843048376429107) ];\r\nvexp=[         18457341831           7658711707 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         78140576077          33767545674 ];\r\nvexp=[uint64( 6098897546038113251) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[          6734445697          12144787423 ];\r\nvexp=[uint64( 4770215408132554690) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        426726665699         126968827572 ];\r\nvexp=[uint64(12995540462042527271) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 5523416580603863552) ];\r\nvexp=[          4255700693          41585972826 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 3928457774057619184) ];\r\nvexp=[          3877602413          16306596859 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        136097916108          28801327007 ];\r\nvexp=[uint64( 5984770176263773551) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(16263054952801281548) ];\r\nvexp=[         14189368539          34874268638 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        662028875555         839442017139 ];\r\nvexp=[uint64(11859750004469197678) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         61393402621         332298132605 ];\r\nvexp=[uint64( 7879625004656522848) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[          1998630709           8715852076 ];\r\nvexp=[uint64(  429234479783941040) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         46021740257          21715449429 ];\r\nvexp=[uint64(14347105153381215235) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(  498971859648614985) ];\r\nvexp=[         30472124134          22307121041 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 6396162213707057064) ];\r\nvexp=[         26683188512          96283024611 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         46925294325          33937979441 ];\r\nvexp=[uint64( 1189627028589296041) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         76315837293          99424320902 ];\r\nvexp=[uint64(11656949174638158734) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         81880058662          38464981613 ];\r\nvexp=[uint64( 7071867154613101059) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(10763027175299443277) ];\r\nvexp=[        330973468653         194345162402 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         79087591419          23535674032 ];\r\nvexp=[uint64(15017024826981320231) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        241564818347         155909390759 ];\r\nvexp=[uint64(15575297637481852549) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         45079186819         295776092742 ];\r\nvexp=[uint64(16532545499676710720) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[         22268771423          34477255167 ];\r\nvexp=[uint64(11007510539515659130) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 5666822869473456046) ];\r\nvexp=[         28640204779          36488487629 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(14041232599151835393) ];\r\nvexp=[         28442192536          25169506635 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 9611808239041397120) ];\r\nvexp=[          1055826287           7876555270 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        358405360794         498368744795 ];\r\nvexp=[uint64( 4955841772239017238) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 3979638765694296062) ];\r\nvexp=[          6903410593           7465748396 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        151761015812         211325030677 ];\r\nvexp=[uint64( 7987025462351843862) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        325266071703          87446899892 ];\r\nvexp=[uint64(16944641327505127607) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 3273057505586617135) ];\r\nvexp=[        124425395619          27124979723 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        142722642746          60293518411 ];\r\nvexp=[uint64(10857549622062058131) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        379488415355         462240655923 ];\r\nvexp=[uint64(16693497308621268574) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 5336487237226370963) ];\r\nvexp=[         76728175571          32349586492 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        366909173981         111494050923 ];\r\nvexp=[uint64(11044880014193584327) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 2767480983943234275) ];\r\nvexp=[        155407720950          67429662431 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[          4375152441          11923449250 ];\r\nvexp=[uint64(14458798758395123876) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[        107946470399          33010545783 ];\r\nvexp=[uint64(13708745226178508359) ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64(  546205200272961984) ];\r\nvexp=[          1773793574          11180737745 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\n%%\r\nv=[uint64( 6791165959744564855) ];\r\nvexp=[        125726750555          33307435529 ];\r\nvout=Tree_CH(v);\r\nassert(isequal(vout,vexp))\r\ntoc\r\n%%\r\n% Read of GJam 64 bit data\r\n% function z = read_input64(fn)\r\n%  fid=fopen(fn);\r\n%  u=textscan(fid,'%u64');\r\n%  fclose(fid);\r\n%  vptr=2; % skip Test case count \r\n%  dptr=1;\r\n%  while vptr\u003csize(u{1},1)\r\n%   if u{1}(vptr)==1 % single N\r\n%    z{dptr}=u{1}(vptr+1);\r\n%    vptr=vptr+2;\r\n%   else % Pair for p/q\r\n%    z{dptr}=[u{1}(vptr+1) u{1}(vptr+2)];\r\n%    vptr=vptr+3;\r\n%   end\r\n%   dptr=dptr+1;\r\n%  end\r\n% end\r\n%Typical GJam 64 bit data\r\n% 100\r\n% 2 2081355757 4898583766\r\n% 2 385960903003 298051413714\r\n% 1 15477096172227810860\r\n% 1 6473043965665514375\r\n% 1 11270280438309969755\r\n% 2 6893018069 8719066441\r\n% 1 6566840053865194126\r\n% 2 21562167101 16520535073\r\n% 2 1684200303 7336396097\r\n% 1 2524835851416755114\r\n% 2 154908421282 199691474905\r\n","published":true,"deleted":false,"likes_count":0,"comments_count":0,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":5,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2013-09-29T03:53:20.000Z","updated_at":"2013-09-29T21:22:24.000Z","published_at":"2013-09-29T04:19:40.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/2924486/dashboard#s=p1\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eGJam 2014 China Rational Number Tree\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\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe Goal is to determine the tree node if given [P,Q] or provide the [P,Q] if given a node. This is the Large Challenge with a Max of 64 Tree levels. Processing of uint64 size data requires extra care.\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\u003eConsider an infinite complete binary tree where the root node is 1/1 and the left and right children of node P/Q are P/(P+Q) and (P+Q)/Q, respectively.\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 Tree looks like:\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/1\\n    ______|______\\n    |           |\\n   1/2         2/1\\n ___|___     ___|___\\n |     |     |     |\\n1/3   3/2   2/3   3/1]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe nodes are 1/1, 1/2, 2/1, 1/3, 3/2, 2/3, 3/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\u003eInput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e [N] or [P,Q] where N is a uint64 integer node or [P,Q] (double) are terms of a Node\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 [P,Q](double) or [N](uint64) depends on Input type\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eExamples:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[[Input]  [Output]\\n  [2] [1 2]\\n  [1 2] [2]\\n  [5] [3 2]\\n  [3 2] [5]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eContest Performance:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Best Delta Time of 14 minutes with only 368 able to process the large data set in less than 3 hours.\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\u003eNotes:\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\u003e1) Matlab has issues with uint64 for dec2bin and matrix multiplies.\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\u003e2) Example of uint64 read is provided in test suite comments\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\u003e3) Bitshift and bitget work on uint64\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:\"bitshift\"","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:\"bitshift\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"bitshift\"","","\"","bitshift","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007f4604a64608\u003e":null,"#\u003cMathWorks::Search::Field:0x00007f4604a64568\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007f4604a63ca8\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007f4604a64928\u003e":1,"#\u003cMathWorks::Search::Field:0x00007f4604a647e8\u003e":50,"#\u003cMathWorks::Search::Field:0x00007f4604a64748\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007f4604a646a8\u003e":"tag:\"bitshift\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007f4604a646a8\u003e":"tag:\"bitshift\""},"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":"search","password":"J3bGPZzQ7asjJcCk","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:\"bitshift\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"bitshift\"","","\"","bitshift","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007f4604a64608\u003e":null,"#\u003cMathWorks::Search::Field:0x00007f4604a64568\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007f4604a63ca8\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007f4604a64928\u003e":1,"#\u003cMathWorks::Search::Field:0x00007f4604a647e8\u003e":50,"#\u003cMathWorks::Search::Field:0x00007f4604a64748\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007f4604a646a8\u003e":"tag:\"bitshift\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007f4604a646a8\u003e":"tag:\"bitshift\""},"queried_facets":{}},"options":{"fields":["id","difficulty_rating"]},"join":" "},"results":[{"id":808,"difficulty_rating":"easy"},{"id":810,"difficulty_rating":"medium"},{"id":1900,"difficulty_rating":"unrated"}]}}