<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-6085626273415496889</id><updated>2025-12-29T11:57:11.866-08:00</updated><category term="Algorithms"/><category term="Analytical Mathematics"/><category term="Puzzles"/><category term="Technology"/><category term="Data Structures"/><category term="Binary System"/><category term="Bit Math Fun"/><category term="Operating Systems"/><category term="Advanced C++"/><category term="Behavioural"/><category term="Books"/><category term="C and C++"/><category term="Interview Questions"/><category term="Knowledge sharing"/><category term="Programming"/><category term="Webkit"/><title type='text'>Lateral Thinking, Analytical Mathematics and Computing</title><subtitle type='html'>Once, Carl Friedrich Gauss said, &quot;Mathematics is the queen of sciences and number theory is the queen of mathematics.&quot; Mathematics is a tool to prove our analogy. Here I post some of my collections that emphasis significance of mathematics in real life. Posts also include questions on computing, mathematics, Free-Open-Source-Software, Algorithms, Data Structures and computers in general. All thoughts/opinions are mine, not related to my employer.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://math-puzzles-computing.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://math-puzzles-computing.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/09855100606552890649</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>59</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-6085626273415496889.post-4551619833205846751</id><published>2012-11-19T21:46:00.003-08:00</published><updated>2012-11-19T21:46:29.373-08:00</updated><title type='text'>Branch-free detection of sign</title><content type='html'>I found something wrong in the following video,&lt;br /&gt;
&lt;br /&gt;
https://www.youtube.com/watch?v=Z59REm2YKX0&lt;br /&gt;
&lt;br /&gt;
The branch free code by author is,&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 11.0pt; mso-no-proof: yes;&quot;&gt;bool&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 11.0pt; mso-no-proof: yes;&quot;&gt;
OppositeSign(&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; a, &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;
b) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 11.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;return&lt;/span&gt; (a ^
b) &amp;lt; &lt;span style=&quot;color: red;&quot;&gt;0&lt;/span&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 11.0pt; mso-ansi-language: EN-US; mso-bidi-language: AR-SA; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;; mso-fareast-language: EN-US; mso-no-proof: yes;&quot;&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
But the compiler can internally convert the statement &quot;return (a ^ b) &amp;lt; 0;&quot; into conditional code.&lt;br /&gt;
&lt;br /&gt;
Here is a branch free code, if the processor provide bit manipulation instructions (most of the processors provide)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: green; font-family: &amp;quot;Courier New&amp;quot;; font-size: 11.0pt; mso-no-proof: yes;&quot;&gt;// except inputs every thing is compile time constant&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 11.0pt; mso-no-proof: yes;&quot;&gt;bool&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 11.0pt; mso-no-proof: yes;&quot;&gt;
OppositeSign(&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt; a, &lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;
b) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 11.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;unsigned&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt; shift = &lt;span style=&quot;color: blue;&quot;&gt;sizeof&lt;/span&gt;(&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;)*&lt;span style=&quot;color: red;&quot;&gt;8&lt;/span&gt;-&lt;span style=&quot;color: red;&quot;&gt;1&lt;/span&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 11.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;unsigned&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;const&lt;/span&gt; mask&amp;nbsp; = &lt;span style=&quot;color: red;&quot;&gt;1&lt;/span&gt; &amp;lt;&amp;lt; shift;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 11.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 11.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;unsigned&lt;/span&gt;((a&amp;amp;mask)^(b&amp;amp;mask)) &amp;gt;&amp;gt; shift;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 11.0pt; mso-no-proof: yes;&quot;&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
</content><link rel='replies' type='application/atom+xml' href='http://math-puzzles-computing.blogspot.com/feeds/4551619833205846751/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6085626273415496889/4551619833205846751' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/4551619833205846751'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/4551619833205846751'/><link rel='alternate' type='text/html' href='http://math-puzzles-computing.blogspot.com/2012/11/branch-free-detection-of-sign.html' title='Branch-free detection of sign'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/09855100606552890649</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6085626273415496889.post-5736678357504822701</id><published>2011-10-29T03:42:00.000-07:00</published><updated>2011-10-29T03:42:26.215-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithms"/><title type='text'>Online Median Tracing Algorithm</title><content type='html'>&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Given a stream of integers (flow read from disk or some other resource, like a sensor counting some events). How can we find the median of elements read so far?&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;It is an interesting &lt;i&gt;online&lt;/i&gt; algorithm. Online, we mean the algorithm that can output result after any instance of input processing. For example, after reading i-elements we should be able to tell the median of i-elements read till now.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;I read the question on &lt;a href=&quot;http://codingplayground.blogspot.com/2011/10/compute-running-median-and-running.html&quot;&gt;Gulli blog&lt;/a&gt;, and came up with some solution. I have posted the algorithm on Geeksforgeeks.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;a href=&quot;http://www.geeksforgeeks.org/archives/14873&quot;&gt;http://www.geeksforgeeks.org/archives/14873&lt;/a&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Again, it is just for my reference of link. Any comments, please post on Geeksforgeeks.&lt;/span&gt;</content><link rel='replies' type='application/atom+xml' href='http://math-puzzles-computing.blogspot.com/feeds/5736678357504822701/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6085626273415496889/5736678357504822701' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/5736678357504822701'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/5736678357504822701'/><link rel='alternate' type='text/html' href='http://math-puzzles-computing.blogspot.com/2011/10/online-median-tracing-algorithm.html' title='Online Median Tracing Algorithm'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/09855100606552890649</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6085626273415496889.post-7492081677078512824</id><published>2011-08-13T05:08:00.000-07:00</published><updated>2011-08-14T07:39:41.265-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Operating Systems"/><title type='text'>Operating Systems - A Fun</title><content type='html'>&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt;&quot;&gt;August 13, 2011&lt;br /&gt;
I read “operating systems” during my&amp;nbsp;academics, and fortunately got an opportunity to work on a commercial OS development for embedded systems. The following list of resources will help to understand the OS internals,&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt; text-align: justify;&quot;&gt;1. Minix – the foundation behind Linux implementation. The source code is available from the book Operating Systems Design and Implementation, 3e, by Tanenbaum and Woodhull.&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt; text-align: justify;&quot;&gt;2. &lt;a href=&quot;http://en.wikipedia.org/wiki/MicroC/OS-II&quot;&gt;Micro C OS - II&lt;/a&gt; and Micro C OS – III. If you want simple OS implementation for embedded system, it is the book for you. The recent updated kernel (µCOS - III) supports virtual memory management. Code is free for educational purpose, get from “&lt;a href=&quot;http://micrium.com/page/home&quot;&gt;http://micrium.com/page/home&lt;/a&gt;”. The story behind Micro C OS is interesting, an example of what passion and need can do, read the story in the &lt;a href=&quot;http://www.flipkart.com/books/1578201039?_l=DwV5aypZeHJJWAw4l9Jl7w--&amp;amp;_r=_gvfTFZZJIgtaVBXz_lt6g--&amp;amp;pid=ssx3fv815e&quot;&gt;book&lt;/a&gt;, the same was available on their website.&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt; text-align: justify;&quot;&gt;3. eCos is another RTOS written in C++. However, it seems it is now defunct.&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt; text-align: justify;&quot;&gt;4. &lt;a href=&quot;http://www.rtems.com/&quot;&gt;RTEMS&lt;/a&gt; – The first (IMHO) open source kernel implementing rate monotonic scheduling. Written in C++.&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt; text-align: justify;&quot;&gt;5. pintos – created by a Stanford student, available for free. Check their website.&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt; text-align: justify;&quot;&gt;The above links help to navigate real code. But for the concepts behind the code, we need to consult relevant books or papers.&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt; text-align: justify;&quot;&gt;For example, read &lt;a href=&quot;http://joshaas.net/linux/linux_cpu_scheduler.pdf&quot;&gt;this&lt;/a&gt; paper for Linux O(1) scheduler implementation, you need to be aware of priority arrays, complexity basics, etc. Similarly, for practical use of linked lists in Linux read Robert Love book.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://math-puzzles-computing.blogspot.com/feeds/7492081677078512824/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6085626273415496889/7492081677078512824' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/7492081677078512824'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/7492081677078512824'/><link rel='alternate' type='text/html' href='http://math-puzzles-computing.blogspot.com/2011/08/operating-systems-fun.html' title='Operating Systems - A Fun'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/09855100606552890649</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6085626273415496889.post-1152884470130392709</id><published>2011-07-30T00:09:00.000-07:00</published><updated>2011-08-18T00:03:19.578-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithms"/><category scheme="http://www.blogger.com/atom/ns#" term="Puzzles"/><title type='text'>Decision Trees - Fake Coin</title><content type='html'>&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px; line-height: 19px;&quot;&gt;30-July-2011&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
I have pending post from few months. It is on application of decision trees to analyze algorithms.&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 12pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: 19px;&quot;&gt;The puzzle is given below,&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: 19px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: 19px;&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;background-color: white; font-family: Helvetica, Arial, Verdana, sans-serif; font-size: 13px; line-height: 19px;&quot;&gt;&lt;em&gt;We are provided a two pan fair balance and N identically looking coins, out of which only one coin&amp;nbsp;&lt;strong&gt;m&lt;/strong&gt;&lt;strong&gt;ay be&lt;/strong&gt;&amp;nbsp;defective. How can we trace which coin, if any, is odd one, and also determine whether it is lighter or heavier in minimum number of trials in the worst case?&lt;/em&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: x-small; line-height: 19px;&quot;&gt;&quot;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: 19px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: 19px;&quot;&gt;Try to solve the puzzle on your own, and then read my systematic solution on the following link&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: 19px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;line-height: 24px;&quot;&gt;&lt;span style=&quot;font-size: 10pt; line-height: 19px;&quot;&gt;&lt;a href=&quot;http://geeksforgeeks.org/?p=9411&quot;&gt;http://geeksforgeeks.org/?p=9411&lt;/a&gt;&quot;.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 24px; margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 12pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: 10pt; line-height: 19px;&quot;&gt;There are other applications of decision trees, like analyzing comparison based sorting routines. We may think that comparison is cheap operation, however, in reality we deal with large objects than simple integers, where comparison is costly &amp;nbsp;operation. We need decision trees to analyze lower bounds of comparison sorting.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 24px; margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 12pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: 10pt; line-height: 19px;&quot;&gt;There are other advantages of trees like solving game of Nim, probability problems, etc...&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 24px; margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 12pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: 10pt; line-height: 19px;&quot;&gt;Again the link is for my reference. Any comments post on Geeksforgeeks.org site.&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://math-puzzles-computing.blogspot.com/feeds/1152884470130392709/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6085626273415496889/1152884470130392709' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/1152884470130392709'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/1152884470130392709'/><link rel='alternate' type='text/html' href='http://math-puzzles-computing.blogspot.com/2011/07/decision-trees-fake-coin.html' title='Decision Trees - Fake Coin'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/09855100606552890649</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6085626273415496889.post-949184961488541376</id><published>2011-06-06T04:35:00.000-07:00</published><updated>2011-06-06T04:36:11.400-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Technology"/><title type='text'>Text Search Engine</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;6-June-2011&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;Given huge text file or collection of documents&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;. How will you search for a word in that collection?&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Lucene&quot;&gt;Lucene&lt;/a&gt; is an open source library that indexes text for efficient information retrieval. &lt;a href=&quot;http://lucene.apache.org/java/docs/index.html&quot;&gt;Here&lt;/a&gt; is the project home page. Lucene can be effectively used to index words in any document format, that makes it flexible to adopt web applications. The story behind Lucene is interesting to read. The name Lucene is the name of better-half of the creator of Lucene software, named Dogg Cutting.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;Lucene was originally implemented in Java, and there are port to other languages like CLucene in C++. Lucene can be used as information retrieval on small sites.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;The Qt library includes Lucene port to Qt C++ under 3party libraries. We can experiment with the Qt code for information retrieval on small documents.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: x-small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://math-puzzles-computing.blogspot.com/feeds/949184961488541376/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6085626273415496889/949184961488541376' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/949184961488541376'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/949184961488541376'/><link rel='alternate' type='text/html' href='http://math-puzzles-computing.blogspot.com/2011/06/text-search-engine.html' title='Text Search Engine'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/09855100606552890649</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6085626273415496889.post-5871671827269733001</id><published>2011-05-07T21:54:00.000-07:00</published><updated>2011-05-07T23:30:18.015-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithms"/><category scheme="http://www.blogger.com/atom/ns#" term="Analytical Mathematics"/><title type='text'>Connecting Capacitances - Project Euler Problem#155</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;8 - May - 2011&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;I came across an interesting problem. Given N capacitors (or inductors, resistors) of same value say C (unit is Farad). How many difference capacitances can be derived from these N capacitors connected in series, parallel and series-parallel combination.&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;For example if N = 1, only one way it can be connected. If N = 2, they can be connected in two different ways of series and parallel. If N = 3, they can be connected in 7 different ways. That means we can derive 7 different capacitance values from given 3 capacitors of same value. These 7 combinations can be found pictorially on the &lt;a href=&quot;http://projecteuler.net/index.php?section=problems&amp;amp;id=155&quot;&gt;Project Euler page&lt;/a&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif;&quot;&gt;Can we generalize it? Yes. Say the total possible capacitances are represented as S(n). If we observe, only the difference between S(n-1) and S(n-2) will participate using next capacitor. For example to calculate S(n) we need the difference DELTA = S(n-1) - S(n-2), and all&amp;nbsp;combinations&amp;nbsp;upto S(n-1). The new capacitor C(n) can be connected in series and parallel with every combination of DELTA..&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;With an example it will be clear. We know S(1) = 1, S(2) = 3 and S(3) = 7. To calculate S(4) we need DELTA = S(3) - S(2) = 4. We can connect the 4-th capacitor in series and parallel with these 4 combinations, overall it adds 4 * 2 = 8 more combination to the existing 7 combination upto S(3). So, clearly S(4) = 2 x DELTA + S(3) = 15. There is a recursive formula. It is evident from the recursion tree given below for S(5).&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhauiWhRXtJqvzd3I6ERnx2QpN7rNFSkzNjLof9myTNVUDdFDZg_VpPN2RTYtYDbunQVrmsTumvO8Xub0T_45dkMRUhZXI4Frg0b_zqbHhyCNspNWOEHUsZpWHVoAolqx-0t0ne4SzWiTcM/s1600/Capacitors.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;225&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhauiWhRXtJqvzd3I6ERnx2QpN7rNFSkzNjLof9myTNVUDdFDZg_VpPN2RTYtYDbunQVrmsTumvO8Xub0T_45dkMRUhZXI4Frg0b_zqbHhyCNspNWOEHUsZpWHVoAolqx-0t0ne4SzWiTcM/s320/Capacitors.jpg&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;Solution can be generalized as&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;S(n) = 2 x (S(n-1) - S(n-2)) + S(n-1)&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; = 3 x S(n-1) - 2 x S(n-2)&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;Base case S(1) = 1, S(2) = 3 and S(3) = 7.&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: x-small;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;We can code above formula using recursion easily.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;int totalPossibleCapacitances(int n)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; static int S[] = {1, 3, 7};&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; if(n &amp;gt; 3)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; // Non trival case&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;return 3*totalPossibleCapacitances(n-1) - 2*totalPossibleCapacitances(n-2)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; else&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; // Trivial case&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return S[n - 1];&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif;&quot;&gt;&lt;b&gt;Complexity&lt;/b&gt;: What will be the complexity? If we observe the recursion tree, the problems are overlapped. Similar to the Fibonacci series. The&amp;nbsp;characteristic&amp;nbsp;equation of generalized solution can be written as&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif;&quot;&gt;x ^ 2 - 3*x + 2 = 0 whose roots are x1 = 1 and x2 = 2. The solution is in the form&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif;&quot;&gt;S(n) = A(x1)^n + B(x2)^n where A and B are constants that depends on initial conditions. After&amp;nbsp;substitution&amp;nbsp;of S(2) and S(3) we get,&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif;&quot;&gt;S(n) = 1.2 * (2)^n - 3 which is exponential.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;So, the above function runs with exponential complexity. Can we do better? Yes, the well known technique &quot;&lt;b&gt;Dynamic Programming&lt;/b&gt;&quot;.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Arial, sans-serif; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;If we observe from S(4), every number is depending on past two numbers only. S(n) = 3*S(n-1) - 2*S(n-2), i.e. we need to store only the past two values to find next number. A three element array will be sufficient. Given below pseudo code,&lt;/div&gt;&lt;div style=&quot;font-family: Arial, sans-serif; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;int totalPossibleCapacitances(int n)&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; // For efficiency purpose&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; // static&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; int S[] = {1, 3, 7};&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; if(n &amp;gt; 3)&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; {&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; // Non trivial&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; int activeIndex = 0;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; for(int i = 4; i &amp;lt;= n; i++)&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; S[activeIndex] = 3*S[ (activeIndex + 2) % 3 ] - 2*S[ (activeIndex + 1) % 3 ];&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; activeIndex++;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; activeIndex %= 3;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if(activeIndex)&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; activeIndex--;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return S[activeIndex];&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; else&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; {&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; // Trivial&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return (n &amp;gt; 0 ? S[n - 1] : -1);&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;Fun to code&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;. The same applies to Inductors and Resistors. The series appears as 1, 3, 7, 15, 31, 63, 127, 255, ... which is one less than powers of 2. Ofcourse generating this series is quite easy. The emphasis is on the recursion tree, and dynamic programming.&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://math-puzzles-computing.blogspot.com/feeds/5871671827269733001/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6085626273415496889/5871671827269733001' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/5871671827269733001'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/5871671827269733001'/><link rel='alternate' type='text/html' href='http://math-puzzles-computing.blogspot.com/2011/05/connecting-capacitances-project-euler.html' title='Connecting Capacitances - Project Euler Problem#155'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/09855100606552890649</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhauiWhRXtJqvzd3I6ERnx2QpN7rNFSkzNjLof9myTNVUDdFDZg_VpPN2RTYtYDbunQVrmsTumvO8Xub0T_45dkMRUhZXI4Frg0b_zqbHhyCNspNWOEHUsZpWHVoAolqx-0t0ne4SzWiTcM/s72-c/Capacitors.jpg" height="72" width="72"/><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6085626273415496889.post-4934315130076500477</id><published>2011-05-02T18:57:00.000-07:00</published><updated>2011-05-02T19:01:29.625-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Data Structures"/><title type='text'>Facebook - Friends Search</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15px; line-height: 20px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;3 - May - 2011&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;When you type a name in friends list, the list automatically displays&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;those&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;filtered names containing typed-in characters. What data structure suites well in such situation? I will provide few links for the same in JavaScript.&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;Yet to update.&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://math-puzzles-computing.blogspot.com/feeds/4934315130076500477/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6085626273415496889/4934315130076500477' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/4934315130076500477'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/4934315130076500477'/><link rel='alternate' type='text/html' href='http://math-puzzles-computing.blogspot.com/2011/05/facebook-friends-search.html' title='Facebook - Friends Search'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/09855100606552890649</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6085626273415496889.post-2351805428477363153</id><published>2011-05-01T11:08:00.000-07:00</published><updated>2011-05-01T20:49:44.000-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Bit Math Fun"/><title type='text'>Next Higher Number with Same Number of Set Bits</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: 20px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 12px; line-height: 14px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;1 - May - 2011&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;Given a number say, N - an integer. Find the next larger number with same set (logic 1) number of bits.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;For example consider N = 156 which is&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: monospace; font-size: 14px; line-height: 19px; white-space: pre;&quot;&gt;10011100 &lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;in binary. It has 4 bits set to logic 1. The next immediate number with 4 bits set to logic 1 is 163.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;Try out the solution and compare it with my post on&amp;nbsp;&lt;a href=&quot;http://geeksforgeeks.org/?p=10375&quot; style=&quot;color: blue; text-decoration: none;&quot;&gt;Geeksforgeeks&lt;/a&gt;. Again I am posting it as a reference to Geeksforgeeks link.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;If any clarification needed, post your comments there. They will be helpful to many engineers visiting Geeksforgeeks.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://math-puzzles-computing.blogspot.com/feeds/2351805428477363153/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6085626273415496889/2351805428477363153' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/2351805428477363153'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/2351805428477363153'/><link rel='alternate' type='text/html' href='http://math-puzzles-computing.blogspot.com/2011/05/next-higher-number-with-same-number-of.html' title='Next Higher Number with Same Number of Set Bits'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/09855100606552890649</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6085626273415496889.post-7122627923762673685</id><published>2011-04-22T22:45:00.000-07:00</published><updated>2011-04-22T22:46:50.031-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Advanced C++"/><title type='text'>Opaque Pointer and Binary Compatibility</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif; line-height: 24px;&quot;&gt;23, April 2011&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;line-height: 150%; margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 12pt; text-align: justify;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: black; line-height: 150%;&quot;&gt;Binary Compatibility:&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: black; line-height: 150%;&quot;&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;When you compile a small application that prints &quot;hello, world&quot;, we are implicitly depending on third party libraries usually developed by compiler vendor. If there is bug, or upgraded software from the vendor, can we run our application without recompiling? If yes, we say that the library is binary compatible.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Or in-other words, let us say we supplied a library, and later we come up with&amp;nbsp;great&amp;nbsp;ideas that requires changes in many classes layouts, how can you ship the upgraded libraries without recompiling client code? The solution is Handle/Body idiom (refer Advanced C++ book by Jim Copelin).&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Binary compatibility depends on various factors&lt;span class=&quot;apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Application_binary_interface&quot;&gt;Application Binary Interface&lt;/a&gt;&lt;span class=&quot;apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;definition, source compatibility&amp;nbsp;&lt;a href=&quot;http://en.wikipedia.org/wiki/Opaque_pointer&quot;&gt;opaque pointer&lt;/a&gt;&lt;span class=&quot;apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;in code.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: black; line-height: 150%;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: black; line-height: 150%;&quot;&gt;Opaque Pointer (Also referred as Handle/Body Idiom, Pimpl Idiom, Cheshire Cat etc.):&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: black; line-height: 150%;&quot;&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;To achieve binary compatibility we separate implementation from interface, so easy to say? The interface will have an indirection (pointer or Handle) to implementation (Body). An excellent article on Qt d-pointer (a.k.a. data pointer) is available on&lt;span class=&quot;apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://developer.qt.nokia.com/wiki/Dpointer&quot;&gt;Qt developer page&lt;/a&gt;&lt;span class=&quot;apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;and&lt;span class=&quot;apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://techbase.kde.org/Policies/Library_Code_Policy&quot;&gt;KDE developer page&lt;/a&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Note that the concept of Opaque Pointer or Pointer to Implementation (PIMPL) is used extensively in many software libraries. I came across PIMPL in mobile software, Qt, Boost, many more. I am preparing an article for GeeksforGeeks, soon it will be published.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Also note the concept of q-pointer (pointer back to Handle class) that references the API class for any functionality defined in API class&amp;nbsp;hierarchy.&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://math-puzzles-computing.blogspot.com/feeds/7122627923762673685/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6085626273415496889/7122627923762673685' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/7122627923762673685'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/7122627923762673685'/><link rel='alternate' type='text/html' href='http://math-puzzles-computing.blogspot.com/2011/04/opaque-pointer-and-binary-compatibility.html' title='Opaque Pointer and Binary Compatibility'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/09855100606552890649</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6085626273415496889.post-4985058224413953254</id><published>2011-04-22T02:02:00.000-07:00</published><updated>2011-04-22T21:51:27.415-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Technology"/><title type='text'>Layout Engines - WebKit, Gecko, ...</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: x-small;&quot;&gt;22, April 2011&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;line-height: 150%; margin-bottom: 12.0pt; margin-left: 0in; margin-right: 0in; margin-top: 12.0pt; text-align: justify;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: x-small;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt; line-height: 150%;&quot;&gt;Layout Engine:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: x-small;&quot;&gt;  &lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;line-height: 150%; margin-bottom: 12.0pt; margin-left: 0in; margin-right: 0in; margin-top: 12.0pt; text-align: justify;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt; line-height: 150%;&quot;&gt;If we want to display an image on screen, how can we do it? We need an entity that paints the screen with the image content. These links helps as digital diary.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;line-height: 150%; margin-bottom: 12.0pt; margin-left: 0in; margin-right: 0in; margin-top: 12.0pt; text-align: justify;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt; line-height: 150%;&quot;&gt;The traditional software frameworks like Windows Form, Qt widget, Java Swings etc. provides functionality to render the screen using static information called objects. There are more&amp;nbsp;sophisticated&amp;nbsp;&lt;b&gt;&lt;i&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Web_browser_engine&quot;&gt;rendering engines&lt;/a&gt;&lt;/i&gt;&lt;/b&gt;&lt;span class=&quot;apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;that are part of web browsers. The concept of layout engine came from early development of web (Netscape navigator).&lt;/span&gt;&lt;span style=&quot;color: black; font-size: 13.5pt; line-height: 150%;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 12pt; text-align: justify;&quot;&gt;&lt;div style=&quot;line-height: 150%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt; line-height: 150%;&quot;&gt;Layout engine is the heart of any web browser; examples are WebKit (Chrome, Safari, Konqueror browsers) Gecko (Firefox, Epic&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px; line-height: 19px;&quot;&gt;browsers&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px; line-height: 19px;&quot;&gt;), Presto (Opera&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px; line-height: 19px;&quot;&gt;browser&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px; line-height: 19px;&quot;&gt;), Trident (Internet Explorer&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px; line-height: 19px;&quot;&gt;browser&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px; line-height: 19px;&quot;&gt;) etc. are few examples of well-known layout engines. Almost all of these layout engines are written in advanced C++.&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style=&quot;line-height: 150%; margin-bottom: 12.0pt; margin-left: 0in; margin-right: 0in; margin-top: 12.0pt; text-align: justify;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: x-small;&quot;&gt;&lt;span class=&quot;apple-style-span&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt; line-height: 150%;&quot;&gt;The concept of layout engines is not limited to browsers. One can use it to design applications that needs screen rendering. For example, the outlook mail client, Winamp player display, RealPlayer displays are based on Microsoft propriety Trident engine. Gecko is also used in Thinderbird email client, Google Picasa, and other Internet application suites.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black; font-size: 13.5pt; line-height: 150%;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;line-height: 150%; margin-bottom: 12.0pt; margin-left: 0in; margin-right: 0in; margin-top: 12.0pt; text-align: justify;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt; line-height: 150%;&quot;&gt;Examples of Webkit layout engine other than browser application can be found in &lt;a href=&quot;http://doc.qt.nokia.com/latest/examples-webkit.html&quot;&gt;Qt example code&lt;/a&gt;. The Qt documentation application is based on Webkit layout engine. It is interesting to imagine the applications of these layout engines. If we are handy with the engine design and few web standards, we can adopt the engine alone for&amp;nbsp;variety&amp;nbsp;of applications.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;line-height: 150%; margin-bottom: 12.0pt; margin-left: 0in; margin-right: 0in; margin-top: 12.0pt; text-align: justify;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt; line-height: 150%;&quot;&gt;With the advancement of Cloud Computing Technology, the computer geeks need a thin client application that connects to a server in cloud. Brower based thin clients are going to play major role in future. Chrome design (see Webkit2 also) and development is one example. IE9 also very interesting. Time will answer well.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;line-height: 150%; margin-bottom: 12.0pt; margin-left: 0in; margin-right: 0in; margin-top: 12.0pt; text-align: justify;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: x-small;&quot;&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt; line-height: 150%;&quot;&gt;Script Engine:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;line-height: 150%; margin-bottom: 12.0pt; margin-left: 0in; margin-right: 0in; margin-top: 12.0pt; text-align: justify;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt; line-height: 150%;&quot;&gt;Well, the layout engine itself can’t help much. It should be flexible to program for dynamism. The client side scripting languages like JavaScript play a critical role here. But one needs to parse these scripts in the browser at runtime. The parsing entity is called Script Engine. For more details read about &lt;a href=&quot;http://en.wikipedia.org/wiki/Chakra_(JScript_engine)&quot;&gt;Chakra&lt;/a&gt; and &lt;a href=&quot;http://en.wikipedia.org/wiki/V8_(JavaScript_engine)&quot;&gt;V8&lt;/a&gt; script engines. Firefox uses optimized &lt;a href=&quot;http://en.wikipedia.org/wiki/TraceMonkey&quot;&gt;SpiderMonkey&lt;/a&gt;&amp;nbsp;as script engine. The performance of script engine contributes to overall performance of browser considerably.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt; line-height: 115%;&quot;&gt;For details on Gecko see &lt;a href=&quot;https://developer.mozilla.org/en/Gecko&quot;&gt;this link&lt;/a&gt;. For details of WebKit see &lt;a href=&quot;http://www.webkit.org/&quot;&gt;this link&lt;/a&gt;. &lt;a href=&quot;http://trac.webkit.org/wiki&quot;&gt;WebKit wiki&lt;/a&gt;. The code is free, start exploring their design and implementation. We can see the practical use of C++, design patterns, advanced C++ idioms, etc.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://math-puzzles-computing.blogspot.com/feeds/4985058224413953254/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6085626273415496889/4985058224413953254' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/4985058224413953254'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/4985058224413953254'/><link rel='alternate' type='text/html' href='http://math-puzzles-computing.blogspot.com/2011/04/layout-engines-webkit-gecko.html' title='Layout Engines - WebKit, Gecko, ...'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/09855100606552890649</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6085626273415496889.post-4737607484159889262</id><published>2011-04-15T09:29:00.000-07:00</published><updated>2011-04-15T09:53:20.114-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Data Structures"/><title type='text'>Trie – An efficient way to search data</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px; line-height: 19px;&quot;&gt;15 April, 2011&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial; font-size: 10pt;&quot;&gt;  &lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt;&quot;&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Trie&quot;&gt;Trie&lt;/a&gt; – An efficient way to search data&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt;&quot;&gt;Trie is an information retrieval data structure efficiently extracts required data. For example, see the following,&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt;&quot;&gt;&lt;/div&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;When we type a name on smart phones it displays all the contacts containing the typed-in string. What data structure will fill such needs?&lt;/li&gt;
&lt;li&gt;In advanced editors like &lt;a href=&quot;http://msdn.microsoft.com/hi-in/vstudio/aa718325&quot;&gt;Visual Studio&lt;/a&gt;, &lt;a href=&quot;http://qt.nokia.com/&quot;&gt;Qt Creator,&lt;/a&gt; etc… when we type the identifier name, it automatically displays all the members part of that object. How is it possible?&lt;/li&gt;
&lt;li&gt;On Linux (even on emulated DOS in Windows), if we type characters “gcc” + TAB key, it displays all the commands with “gcc” as prefix. What data structures is appropriate here?&lt;/li&gt;
&lt;/ol&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: 24px;&quot;&gt;All the above cases use a kind of trie data structure. Perhaps, the practical implementation may vary based on kind of application.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial; font-size: 10pt;&quot;&gt;There is a demand for tries on &lt;a href=&quot;http://geeksforgeeks.org/&quot;&gt;Geeksforgeeks&lt;/a&gt;. We will be posting few articles on these in coming weeks.&lt;/span&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://math-puzzles-computing.blogspot.com/feeds/4737607484159889262/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6085626273415496889/4737607484159889262' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/4737607484159889262'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/4737607484159889262'/><link rel='alternate' type='text/html' href='http://math-puzzles-computing.blogspot.com/2011/04/trie-efficient-way-to-search-data.html' title='Trie – An efficient way to search data'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/09855100606552890649</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6085626273415496889.post-8619234366973416542</id><published>2011-04-11T10:34:00.000-07:00</published><updated>2011-04-11T11:14:30.960-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Operating Systems"/><title type='text'>Differences between Semaphores and Mutexes</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;11, Apr 2011&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;What are the differences between semaphores and mutexes?. Are binary semaphore and mutex same or just a notational convenience?&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;It is typical interview question. Surprisingly, we get correct answers rare.&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;From my learnings during RTOS design, I am preparing an article for Geeksforgeeks (&lt;a href=&quot;http://geeksforgeeks.org/?p=9102&quot;&gt;link&lt;/a&gt;), it will be published soon.&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;An interesting question, if you observe Windows word 2003, Adobe PDF reader, only one instance will be running in the task manager. Where as Windows Excel 2003 will create as many processes as number of excel documents opened.&amp;nbsp;&lt;i&gt;How to implement, only one instance of process must be running, invoking another instance should prompt &quot;An instance is already running&quot; message&lt;/i&gt;?&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://math-puzzles-computing.blogspot.com/feeds/8619234366973416542/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6085626273415496889/8619234366973416542' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/8619234366973416542'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/8619234366973416542'/><link rel='alternate' type='text/html' href='http://math-puzzles-computing.blogspot.com/2011/04/differences-between-semaphores-and.html' title='Differences between Semaphores and Mutexes'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/09855100606552890649</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6085626273415496889.post-8074782791787055239</id><published>2011-04-10T05:26:00.000-07:00</published><updated>2011-04-10T05:27:30.677-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Technology"/><title type='text'>Beautiful Architecture - Worth Reading (Productive Weekend)</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;10, Apr 2011.&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: x-small;&quot;&gt;Got new book &quot;&lt;a href=&quot;http://www.flipkart.com/beautiful-architecture-diomidis-spinellis-book-8184046901?ref=9b64c773-91ff-44c7-8a58-04fd8ba2d0f3&quot;&gt;Beautiful Architecture&lt;/a&gt;&quot;. I got the book from local book shop. Good to know that the&amp;nbsp;royalty&amp;nbsp;on the book will be donated to &quot;&lt;a href=&quot;http://www.doctorswithoutborders.org/&quot;&gt;Doctors Without Boarders&lt;/a&gt;&quot;.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;font-family: Arial; font-size: medium; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;The book is collection of essays from experts that designed high end, and high availability systems. Yet to finish few chapters, left good learning experience.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;Few topics to name,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;1. Scalable Architectures&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;2. Resource Oriented Architectures&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;3. Data Centric Architectures (Facebook)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;4. Virtualization (Xen Hypervisor)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;5. Fault Tolerant Operating Systems&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;6. GNU Emacs and KDE (Qt)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;7. Metacircular Virtual Machines&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: black; font-family: Arial, sans-serif; font-size: 10pt;&quot;&gt;Happy and worthy weekend.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Arial; font-size: medium; margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Arial; font-size: medium;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://math-puzzles-computing.blogspot.com/feeds/8074782791787055239/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6085626273415496889/8074782791787055239' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/8074782791787055239'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/8074782791787055239'/><link rel='alternate' type='text/html' href='http://math-puzzles-computing.blogspot.com/2011/04/beautiful-architecture-worth-reading.html' title='Beautiful Architecture - Worth Reading (Productive Weekend)'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/09855100606552890649</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6085626273415496889.post-3295883323177785311</id><published>2011-04-08T09:15:00.000-07:00</published><updated>2011-04-10T05:28:50.408-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithms"/><category scheme="http://www.blogger.com/atom/ns#" term="Analytical Mathematics"/><title type='text'>An Interview Experience</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: 24px;&quot;&gt;8 April, 2011&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 6pt; margin-left: 0in; margin-right: 0in; margin-top: 12pt; text-align: justify;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 150%; margin-bottom: 6pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt;&quot;&gt;I don&#39;t like to present in interview panels unless really required. Today I had to be in an interview panel. Good puzzle I recapped after long time.&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 150%; margin-bottom: 6pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt;&quot;&gt;Two players A and B playing a game (similar to game of&amp;nbsp;&lt;a href=&quot;http://en.wikipedia.org/wiki/Nim&quot;&gt;Nim&lt;/a&gt;). Given a pile of &lt;b&gt;&lt;i&gt;N&lt;/i&gt;&lt;/b&gt; coins, the players need to pick coins from the pile. In each move a player can pick atleast&amp;nbsp;&lt;b&gt;&lt;i&gt;1&lt;/i&gt;&lt;/b&gt;&amp;nbsp;and utmost&amp;nbsp;&lt;b&gt;&lt;i&gt;m&lt;/i&gt;&lt;/b&gt;&amp;nbsp;coins. The player who makes the last move is the winner.&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 150%; margin-bottom: 6pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt;&quot;&gt;For example if A starts the game, he can pick &lt;i&gt;1, 2, 3, ... m&lt;/i&gt; coins from the pile. Then B can pick &lt;i&gt;1, 2, 3, .... m&lt;/i&gt; coins from the remaining pile. Next A, followed by B, so on...&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 150%; margin-bottom: 6pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt;&quot;&gt;In the process, the player who makes last move (picks all remaining coins &amp;lt;= m from the pile) is the winner.&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 150%; margin-bottom: 6pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt;&quot;&gt;Now, given N = 15, m = 3, and A starts the game with some &lt;i&gt;1 &amp;lt;= x &amp;lt;= m&lt;/i&gt; coins. How many different ways are there that A can win the game?&lt;/div&gt;&lt;b&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;Hint&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;: Use state space analysis method (i.e. trees).&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://math-puzzles-computing.blogspot.com/feeds/3295883323177785311/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6085626273415496889/3295883323177785311' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/3295883323177785311'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/3295883323177785311'/><link rel='alternate' type='text/html' href='http://math-puzzles-computing.blogspot.com/2011/04/interview-experience.html' title='An Interview Experience'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/09855100606552890649</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6085626273415496889.post-6877018267958966142</id><published>2011-04-08T07:46:00.000-07:00</published><updated>2011-04-15T10:09:38.341-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Analytical Mathematics"/><title type='text'>Pascal’s Identity - Theoretical Significance</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif; line-height: 24px;&quot;&gt;April 8, 2011&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;  &lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt; text-align: justify;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;We know that C (n, r) = C (n – 1, r – 1) + C (n – 1, r)&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt; text-align: justify;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;It is quite easy to prove it mathematically. &lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;But what is its significance in real life examples?&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt; text-align: justify;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;C (n, r) is selecting r elements from n elements. Pick a special element &lt;i style=&quot;mso-bidi-font-style: normal;&quot;&gt;k&lt;/i&gt; from these n elements, we left with (n – 1) elements.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt; text-align: justify;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;Now this r elements selection C (n, r) can be grouped into two categories,&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;line-height: 150%; margin-bottom: 6.0pt; margin-left: .5in; margin-right: 0in; margin-top: 6.0pt; mso-list: l0 level1 lfo1; tab-stops: list .5in; text-align: justify; text-indent: -.25in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;A)&lt;span style=&quot;font: normal normal normal 7pt/normal &#39;Times New Roman&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;r elements that contain the element k.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;line-height: 150%; margin-bottom: 6.0pt; margin-left: .5in; margin-right: 0in; margin-top: 6.0pt; mso-list: l0 level1 lfo1; tab-stops: list .5in; text-align: justify; text-indent: -.25in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;B)&lt;span style=&quot;font: normal normal normal 7pt/normal &#39;Times New Roman&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;r elements that &lt;i style=&quot;mso-bidi-font-style: normal;&quot;&gt;do not&lt;/i&gt; contain the element k.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt; text-align: justify;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;From A) the special element k is in all r selections. Since k is part of r elements, we need to choose remaining (r – 1) elements from remaining (n – 1) elements, this can be done in C (n – 1, r – 1) ways.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt; text-align: justify;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;From B) the special element k is not there in all r selections. It means, we will have to select all the r elements from available (n – 1) elements (we must exclude element k from n). This can be done in C (n – 1, r) ways.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt; text-align: justify;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;Now it is evident that, the first term of RHS is combination containing special element k, and the second term is combination without the special element k. Sum of these two is selecting r elements from n elements.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 6pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;line-height: 150%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;Interesting proof, little confusing to understand, but useful in practice.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;margin-bottom: 6pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;font-family: Arial; line-height: 24px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;b&gt;Pascal&#39;s Identity and Dynamic Programming:&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Arial; line-height: 24px; margin-bottom: 6pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;If we note the identity, it is the base for recursive calculation of binomial coefficients. However, when plain recursion is used, it will lead to an exponential algorithm.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 6pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 6pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: 24px;&quot;&gt;Recursion with memoization can result in an efficient algorithm. The idea behind this dynamic programming (programming we mean a method of tabulation) is to tabulate the first&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: 24px;&quot;&gt;occurrence&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: 24px;&quot;&gt;&amp;nbsp;of binomial co-efficients.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style=&quot;line-height: 24px; margin-bottom: 6pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;line-height: 24px; margin-bottom: 6pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;Pascal&#39;s triangle can serve as the tabulation in the dynamic method. Each co-efficient is sum of coefficients in the previous row. The table can grow till min(k, i) where 0 &amp;lt;= i &amp;lt;= n.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style=&quot;line-height: 24px; margin-bottom: 6pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Arial; line-height: 24px; margin-bottom: 6pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Try the dynamic algorithm.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;i&gt;&lt;b&gt;Note that there is no need of two dimensional array to memoization of partial results. This fact is evident from Pascal&#39;s triangle with an extra variable to store nCk.&lt;/b&gt;&lt;/i&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://math-puzzles-computing.blogspot.com/feeds/6877018267958966142/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6085626273415496889/6877018267958966142' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/6877018267958966142'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/6877018267958966142'/><link rel='alternate' type='text/html' href='http://math-puzzles-computing.blogspot.com/2011/04/pascals-identity-theoretical.html' title='Pascal’s Identity - Theoretical Significance'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/09855100606552890649</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6085626273415496889.post-4163753495049425023</id><published>2011-03-29T06:32:00.000-07:00</published><updated>2011-03-29T06:32:42.144-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Programming"/><title type='text'>Programming Style - A discipline of writing code</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;29, March 2011&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Discipline is simple, yet powerful word. However, the meaning can vary from person to person (Remember that practice is always different from law :)). The same applies to programming.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;When writing code we never exercise importance of &lt;i&gt;meaningful and precise&lt;/i&gt; comments. I have seen many code snippets on &lt;a href=&quot;http://geeksforgeeks.org/&quot;&gt;Geeksforgeeks&lt;/a&gt; and other&amp;nbsp;forums. Most of the code snippets posted by readers are missing comments. &lt;i&gt;&lt;b&gt;Readability,&amp;nbsp;maintainability (simplicity), clarity&lt;/b&gt;&lt;/i&gt; and &lt;b&gt;&lt;i&gt;generality&lt;/i&gt;&lt;/b&gt; are key to write elegant code.&amp;nbsp;Brevity&amp;nbsp;also key principle of&amp;nbsp;writing&amp;nbsp;code.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Note that geting it right (or code is fine since it is working.... bla bla) are poor reasons to convince ourself. Accept comments if they are adding value.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;About 2 years ago, I had written a program in Perl script to extract symbol table from binary executable. It was about 5000 lines. Yesterday I reviewed it again, based on a call from previous teammate. I am proud that the code is well commented in such a way any engineer in future can take it forward. I could able to recap all the data structures, algorithms easily from the comments.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;I would recommend to read The Practice of Programming by Kernighan.&lt;/span&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://math-puzzles-computing.blogspot.com/feeds/4163753495049425023/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6085626273415496889/4163753495049425023' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/4163753495049425023'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/4163753495049425023'/><link rel='alternate' type='text/html' href='http://math-puzzles-computing.blogspot.com/2011/03/programming-style-discipline-of-writing.html' title='Programming Style - A discipline of writing code'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/09855100606552890649</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6085626273415496889.post-1190270089436529643</id><published>2011-03-19T09:38:00.000-07:00</published><updated>2011-04-08T08:24:12.983-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Data Structures"/><title type='text'>Tournament Tree and Winner Tree</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: 18px;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 12pt;&quot;&gt;&lt;div style=&quot;line-height: 150%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;apple-style-span&quot;&gt;&lt;span style=&quot;font-family: Arial; font-size: 10pt; line-height: 150%;&quot;&gt;March 19, 2011.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;line-height: 150%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;apple-style-span&quot;&gt;&lt;span style=&quot;font-family: Arial; font-size: 10pt; line-height: 150%;&quot;&gt;How many minimum comparisons are needed to trace second smallest element in an unsorted array?&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;line-height: 150%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;apple-style-span&quot;&gt;&lt;span style=&quot;font-family: Arial; font-size: 10pt; line-height: 150%;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;line-height: 150%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333; font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;apple-style-span&quot;&gt;&lt;span style=&quot;font-family: Arial; font-size: 10pt; line-height: 150%;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333; font-size: 13px; line-height: 19px;&quot;&gt;We can use a tournament tree approach based on adversary&amp;nbsp;argument to prove minimum number of comparisons.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;line-height: 150%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333; font-size: 13px; line-height: 19px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;line-height: 150%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333; font-size: 13px; line-height: 19px;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333; font-size: 13px; line-height: 19px;&quot;&gt;In tournament method, every number (element) is paired with other number successively to find the maximum number. This forms a tree like structure. We must have N - 1 comparisons to find maximum number.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;line-height: 150%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333; font-size: 13px; line-height: 19px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;line-height: 150%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333; font-size: 13px; line-height: 19px;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333; font-size: 13px; line-height: 19px;&quot;&gt;The information gained while finding max number will be used in reducing the comparisons for second largest. It meas, we would need to consider only those which lost against max number to be considered for second largest.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;line-height: 150%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333; font-size: 13px; line-height: 19px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;line-height: 150%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333; font-size: 13px; line-height: 19px;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333; font-size: 13px; line-height: 19px;&quot;&gt;From the tree structure, we can conclude such lost numbers can&#39;t be more than [log(N) - 1] (consider tree height).&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;line-height: 150%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333; font-size: 13px; line-height: 19px;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333; font-size: 13px; line-height: 19px;&quot;&gt;Overall, at minimum we need ( N - 1 + log(N) - 1 = N + log(N) - 2 ) comparisons.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;line-height: 150%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333; font-size: 13px; line-height: 19px;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333; font-size: 13px; line-height: normal;&quot;&gt;Read my post on&lt;span class=&quot;apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://geeksforgeeks.org/?p=11556&quot;&gt;Geeksforgeeks&lt;/a&gt;&amp;nbsp;for details on Winner tree approach.&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://math-puzzles-computing.blogspot.com/feeds/1190270089436529643/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6085626273415496889/1190270089436529643' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/1190270089436529643'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/1190270089436529643'/><link rel='alternate' type='text/html' href='http://math-puzzles-computing.blogspot.com/2011/03/tournament-tree-and-winner-tree.html' title='Tournament Tree and Winner Tree'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/09855100606552890649</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6085626273415496889.post-3641008771426451296</id><published>2011-03-16T07:32:00.000-07:00</published><updated>2011-04-10T04:25:40.856-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Bit Math Fun"/><title type='text'>Count number of set bits in an array</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;There are various techniques to count bits in an integer. These methods can be integrated to count bits in an array. I have published a simple and efficient method on &lt;a href=&quot;http://geeksforgeeks.org/?p=11263&quot;&gt;Geeksforgeeks&lt;/a&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;In the post, observe the meta program using C/C++ preprocessor that generates code for main program.&lt;/span&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://math-puzzles-computing.blogspot.com/feeds/3641008771426451296/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6085626273415496889/3641008771426451296' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/3641008771426451296'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/3641008771426451296'/><link rel='alternate' type='text/html' href='http://math-puzzles-computing.blogspot.com/2011/03/count-number-of-set-bits-in-array.html' title='Count number of set bits in an array'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/09855100606552890649</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6085626273415496889.post-7118402915684168326</id><published>2011-03-16T07:29:00.000-07:00</published><updated>2011-03-16T07:29:19.801-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Technology"/><title type='text'>Minesweeper program</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;A nice program written in Qt and Python.&lt;br /&gt;
&lt;br /&gt;
For details see the following link,&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://lionel.textmalaysia.com/minesweeper-in-python-gui.html&quot;&gt;http://lionel.textmalaysia.com/minesweeper-in-python-gui.html&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Last week KDE India meeting was organised. The above program was explained by&amp;nbsp;KDE geek Dinesh Sai. Thanks to him.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://math-puzzles-computing.blogspot.com/feeds/7118402915684168326/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6085626273415496889/7118402915684168326' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/7118402915684168326'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/7118402915684168326'/><link rel='alternate' type='text/html' href='http://math-puzzles-computing.blogspot.com/2011/03/minesweeper-program.html' title='Minesweeper program'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/09855100606552890649</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6085626273415496889.post-6601987328647106573</id><published>2011-03-04T20:59:00.000-08:00</published><updated>2011-04-13T07:58:10.408-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Analytical Mathematics"/><title type='text'>A puzzle (mathematics) - properties of decimal number system</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: 20px;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;5, March 2011.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;I come across one geek blog, and there is one interesting question... &quot;Given a number containing all numbers from 1 through 9, find next higher number with same digits!&quot;.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;The question should trigger a hint. &quot;Same digits&quot; mean the next higher number will have all same digits (In other words, the difference between N and N&lt;sub&gt;next&lt;/sub&gt; should be multiple of 9). Since all the digits are same in both the numbers, difference between the two numbers will cancel out, and makes the next higher number increase by a factor which is multiple of 9. Also, since the number is higher one, we should look for increasing patterns from right to&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 12pt; margin-left: 0in; margin-right: 0in; margin-top: 6pt; text-align: justify;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;For example consider&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;N = 125786493&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: x-small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;We can see&lt;/span&gt;&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;1257864&lt;b&gt;39&lt;/b&gt; can&#39;t be next higher number&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;. We must look for first three digits.&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;125786&lt;b&gt;493&lt;/b&gt;&amp;nbsp;should be transformed to&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;125786&lt;b&gt;934&lt;/b&gt;&amp;nbsp;with a difference of 441 which is multiple of 9. Or simply 49 x 9 (observe the original number).&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;Another number - 157869324 transformed to&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;157869342 with difference of 18 = 2 x 9. Obviously, there is a pattern.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;Logic is clear, can we write the program easily, ....? Not really! Try out.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, sans-serif; font-size: 13px;&quot;&gt;Another variant &quot;&lt;b&gt;Find the next higher number with same number of set bits&lt;/b&gt;&quot;, I had published an article &lt;a href=&quot;http://geeksforgeeks.org/?p=10375&quot;&gt;here&lt;/a&gt;.&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://math-puzzles-computing.blogspot.com/feeds/6601987328647106573/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6085626273415496889/6601987328647106573' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/6601987328647106573'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/6601987328647106573'/><link rel='alternate' type='text/html' href='http://math-puzzles-computing.blogspot.com/2011/03/puzzle-on-mathematics-properties-of.html' title='A puzzle (mathematics) - properties of decimal number system'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/09855100606552890649</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6085626273415496889.post-3060090522499708653</id><published>2011-03-04T07:46:00.000-08:00</published><updated>2013-02-27T17:46:40.074-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Data Structures"/><title type='text'>How many levels?</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
4th, March, 2011&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;a href=&quot;http://en.wikipedia.org/wiki/Tree_(data_structure)&quot;&gt;Tree&lt;/a&gt;&amp;nbsp;is one interesting data structure in computer science. I like the properties of trees and graphs.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Sometimes we receive some junk mails, containing junk information something like if you don&#39;t forward the mail to next 3 people something bad will happen... bla... bla... bla... and some more BS...&amp;nbsp;(&lt;i&gt;these mails&amp;nbsp;usually related to God, even if-at-all God exists, he might be laughing at such mails. Recently I have seen a funny mail, I will post on&amp;nbsp;&lt;a href=&quot;http://wemeanourthoughts.blogspot.com/&quot;&gt;http://wemeanourthoughts.blogspot.com/&lt;/a&gt;&amp;nbsp;fun to read&lt;/i&gt;).&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
But as an Electronics and Computer Science Engineer, it fascinates me to find how many pages would go for waste because of sense-less people.&lt;br /&gt;
&lt;br /&gt;
We can represent the above mail chain as an m-ary tree. Where m is the branching factor, such as 3 in the above case.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
As a simple math, if the mail chain ends at L-th level, how much space would they all occupy in mail servers (ofcourse this is apart from the space on user machines, assume each mail takes few MBs since those mails contain pictures as well)?. It would be in few Giga Bytes of space.&lt;br /&gt;
&lt;br /&gt;
If all the persons at X-level changes the number 3 to 6, how the output will be affected?&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;Reverse (Synthesis) Question:&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Given there are N leaves in a binary tree, how many levels an optimally balanced binary tree can have?&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;Answer:&lt;/b&gt; log&lt;sub&gt;2&lt;/sub&gt;N + 1&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
In general, an m-ary tree with N leaves can have&amp;nbsp;&lt;b&gt;log&lt;sub&gt;m&lt;/sub&gt;N + 1&lt;/b&gt; levels.&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://math-puzzles-computing.blogspot.com/feeds/3060090522499708653/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6085626273415496889/3060090522499708653' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/3060090522499708653'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/3060090522499708653'/><link rel='alternate' type='text/html' href='http://math-puzzles-computing.blogspot.com/2011/03/how-many-levels.html' title='How many levels?'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/09855100606552890649</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6085626273415496889.post-7580613066427557994</id><published>2011-02-25T18:25:00.000-08:00</published><updated>2011-04-11T09:50:58.816-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Analytical Mathematics"/><category scheme="http://www.blogger.com/atom/ns#" term="Puzzles"/><title type='text'>Shortest path in connecting all vertices</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Given below 4 vertices on a two dimensional plane. Connect them in shortest possible way. In other words, there are 4 cities A, B, C and D at the corner of square of side (a) units. What will be the minimum cost of connecting these cities?&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhOkrwdysIuP9TJfpQksnjpXMPS88T6QlbbVYGmennzcSUeBDKHAzpusuV7Jb7k-NDymYl8OR177P4xlgXQHXicEz7WBJk0GMXHpfHM9nQMn7X75xgaZ1L4DnwmdKP1tBRnGkt-mmcZGfrJ/s1600/ShortestPPath.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhOkrwdysIuP9TJfpQksnjpXMPS88T6QlbbVYGmennzcSUeBDKHAzpusuV7Jb7k-NDymYl8OR177P4xlgXQHXicEz7WBJk0GMXHpfHM9nQMn7X75xgaZ1L4DnwmdKP1tBRnGkt-mmcZGfrJ/s1600/ShortestPPath.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
Those making circuit layouts of VLSI chips need to solve the puzzle&amp;nbsp;smartly. Even 0.1% gain in space is huge on&amp;nbsp;miniaturized&amp;nbsp;chips.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Answer&lt;/b&gt;: Assuming the side of square as &lt;b&gt;&lt;i&gt;s&lt;/i&gt;&lt;/b&gt;, the minimum cost (road connecting all four cities) is &lt;b&gt;&lt;i&gt;s x (SQRT(3) + 1)&lt;/i&gt;&lt;/b&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://math-puzzles-computing.blogspot.com/feeds/7580613066427557994/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6085626273415496889/7580613066427557994' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/7580613066427557994'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/7580613066427557994'/><link rel='alternate' type='text/html' href='http://math-puzzles-computing.blogspot.com/2011/02/shortest-path-in-connecting-all.html' title='Shortest path in connecting all vertices'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/09855100606552890649</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhOkrwdysIuP9TJfpQksnjpXMPS88T6QlbbVYGmennzcSUeBDKHAzpusuV7Jb7k-NDymYl8OR177P4xlgXQHXicEz7WBJk0GMXHpfHM9nQMn7X75xgaZ1L4DnwmdKP1tBRnGkt-mmcZGfrJ/s72-c/ShortestPPath.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6085626273415496889.post-3664231038726541193</id><published>2011-02-22T05:46:00.000-08:00</published><updated>2011-02-22T05:46:28.940-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Data Structures"/><title type='text'>Splitting a binary search tree at a given node</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;22, Feb 2011&lt;br /&gt;
Given a node and root of tree, how to split the binary search tree into two different trees.&lt;br /&gt;
&lt;br /&gt;
The idea is to walk down the tree till the node, and keep track of the path. Climb up the tree and separate the nodes into two different trees. The algorithm is simple.&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;split(z, T)&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; x = z.left&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; y = z.right&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; splitter = z&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; while(z.p is valid)&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; if(z.p.left = z) // z is on left of its parent&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;z = z.p&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;z.left = y&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;y.p = z&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;y = z&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; else &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; // z is on right of its parent&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;z = z.p&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;z.right = x&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;x.p = z&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;x = z&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; // add splitter if required or delete it&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; insert(&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;splitter,x)&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; x.p = nil&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; y.p = nil&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; return x and y as two split trees.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Understanding the algorithm will be simple, if we can draw a tree and its walking as per above pseudo code.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://math-puzzles-computing.blogspot.com/feeds/3664231038726541193/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6085626273415496889/3664231038726541193' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/3664231038726541193'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/3664231038726541193'/><link rel='alternate' type='text/html' href='http://math-puzzles-computing.blogspot.com/2011/02/splitting-binary-search-tree-at-given.html' title='Splitting a binary search tree at a given node'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/09855100606552890649</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6085626273415496889.post-7315900965053260951</id><published>2011-02-19T22:40:00.000-08:00</published><updated>2011-02-19T22:40:21.005-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Data Structures"/><title type='text'>Combining (merge) two binary search trees</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;20th, February 2011&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;Given two binary search trees, how to combine them?&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;Note that the binary search tree has a property that every left subtree element will be less than or equal to the root element, and every right subtree element is greater than or equal to the root element.&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;Based on the property, let us assume that given trees T&lt;sub&gt;1&amp;nbsp;&lt;/sub&gt;and&amp;nbsp;T&lt;sub&gt;2&lt;/sub&gt;, such that every element in&amp;nbsp;T&lt;sub&gt;1&lt;/sub&gt;&amp;nbsp;&amp;lt;= every element in&amp;nbsp;T&lt;sub&gt;2&lt;/sub&gt;. It will be easy to merge these two trees.&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;We need to figure out the root of merged tree. The root element can be either right extreme node in&amp;nbsp;T&lt;sub&gt;1&amp;nbsp;&lt;/sub&gt;or left extreme node in&amp;nbsp;T&lt;sub&gt;2&lt;/sub&gt;&amp;nbsp;to maintain BST property. So, the approach is to remove either of these nodes, make it as root, and attach&amp;nbsp;T&lt;sub&gt;1&amp;nbsp;&lt;/sub&gt;as left subtree,&amp;nbsp;T&lt;sub&gt;2&amp;nbsp;&lt;/sub&gt;as right subtree.&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://math-puzzles-computing.blogspot.com/feeds/7315900965053260951/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6085626273415496889/7315900965053260951' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/7315900965053260951'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/7315900965053260951'/><link rel='alternate' type='text/html' href='http://math-puzzles-computing.blogspot.com/2011/02/combining-merge-two-binary-search-trees.html' title='Combining (merge) two binary search trees'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/09855100606552890649</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6085626273415496889.post-5639627195803060087</id><published>2011-02-10T17:10:00.000-08:00</published><updated>2011-02-12T05:14:07.265-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Data Structures"/><title type='text'>Find K-th smallest element in a binary search tree (BST)</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 12px; line-height: 16px;&quot;&gt;11, Feb 2011&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 12px; line-height: 16px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 12px; line-height: 16px;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 16px;&quot;&gt;The question related to order statistics. A BST (non-linear data structure) represents data in an order&amp;nbsp;fashion&amp;nbsp;that satisfies the binary search tree property of &quot;left node key should be &amp;lt;= root and right node key should be &amp;gt;= root&quot;.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 12px; line-height: 16px;&quot;&gt;Since we need K-th smallest element, we need to maintain # of elements in either of the sub-trees. By maintaining the count we can get the sub tree nodes in O(1) time complexity.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 12px; line-height: 16px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 16px;&quot;&gt;Assume the root has n nodes in its left subtree. If n &amp;lt;= K, we will continue search (recursion) for the smallest element in the left subtree. If K = n + 1, the root has the K-th smallest element. If k &amp;gt; n + 1, then we continue search the right subtree for the (K - n- 1)-th smallest element.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 16px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 12px; line-height: 16px;&quot;&gt;There are other approaches, like inorder traversal by maintaining the count, and stop search at K-th element or&amp;nbsp;trivial&amp;nbsp;case of &amp;nbsp;no K-th node.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 12px; line-height: 16px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 12px; line-height: 16px;&quot;&gt;I have published two approaches for finding k-th smallest element in BST (&lt;a href=&quot;http://geeksforgeeks.org/?p=10379&quot;&gt;See the link&lt;/a&gt;).&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://math-puzzles-computing.blogspot.com/feeds/5639627195803060087/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6085626273415496889/5639627195803060087' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/5639627195803060087'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6085626273415496889/posts/default/5639627195803060087'/><link rel='alternate' type='text/html' href='http://math-puzzles-computing.blogspot.com/2011/02/find-k-th-smallest-element-in-binary.html' title='Find K-th smallest element in a binary search tree (BST)'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/09855100606552890649</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>