<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>blog :: -&#62; [std::String]</title>
	<atom:link href="http://nthrgeek.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://nthrgeek.wordpress.com</link>
	<description>me and my weirdness ...</description>
	<lastBuildDate>Mon, 04 Jan 2010 15:19:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='nthrgeek.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>blog :: -&#62; [std::String]</title>
		<link>http://nthrgeek.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://nthrgeek.wordpress.com/osd.xml" title="blog :: -&#62; [std::String]" />
	<atom:link rel='hub' href='http://nthrgeek.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Combination ( n choose k) Unleashed</title>
		<link>http://nthrgeek.wordpress.com/2009/12/09/combination-n-choose-k-unleashed/</link>
		<comments>http://nthrgeek.wordpress.com/2009/12/09/combination-n-choose-k-unleashed/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 20:07:21 +0000</pubDate>
		<dc:creator>nthrgeek</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Tricks]]></category>
		<category><![CDATA[Binomial Showdown]]></category>
		<category><![CDATA[Combinations]]></category>
		<category><![CDATA[N choose K]]></category>
		<category><![CDATA[Project Euler 53.]]></category>
		<category><![CDATA[SPOJ Marbles]]></category>

		<guid isPermaLink="false">http://nthrgeek.wordpress.com/2009/12/09/combination-n-choose-k-unleashed/</guid>
		<description><![CDATA[What is “n choose k”&#160; ? In combinatorics,&#34;n choose k&#34;&#160; (formally represented as &#160; ) is called the choose function of n and k.It is interpreted as the number of ways that k things can be &#8216;chosen&#8217; from a set of n things.Alternative representation of &#34;n choose k&#34;&#160; include C(n, k), nCk or . We [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nthrgeek.wordpress.com&amp;blog=9609689&amp;post=75&amp;subd=nthrgeek&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>What is <em>“n choose k”&#160; ?</em></p>
<p>In <a href="http://en.wikipedia.org/wiki/Combinatorics">combinatorics</a><em>,&quot;n choose k&quot;</em>&#160; (formally represented as <em>&#160;<img src="http://upload.wikimedia.org/math/3/8/2/382c5908d125a08662b2fedc22f4d02c.png" /></em> ) is called the <b>choose function</b> of n and k.It is interpreted as the number of ways that <i>k</i> things can be &#8216;chosen&#8217; from a set of <i>n</i> things.Alternative representation of <em>&quot;n choose k&quot;</em>&#160; include C(<i>n</i>, <i>k</i>), <sub><i>n</i></sub><i>C</i><sub><i>k</i></sub> or <img src="http://upload.wikimedia.org/math/d/8/e/d8e708c07c7c1d240d6b1096ef5cc353.png" /> .</p>
<p>We define C(<i>n</i>, <i>k</i>) in the following ways:</p>
<p>&#160;</p>
<p><img height="51" src="http://upload.wikimedia.org/math/a/c/1/ac1206fd17b984fbcd6549b60eb1aa2e.png" width="586" /> </p>
<p>and</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <img src="http://upload.wikimedia.org/math/4/3/8/43837d1ea4c03a00f27c95c3ef78c1e6.png" /> </p>
<p>where <i>n</i>! denotes the <a href="http://en.wikipedia.org/wiki/Factorial">factorial</a> of <i>n</i>.</p>
<p>Alternatively, a recursive definition can be written as</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <img src="http://upload.wikimedia.org/math/2/c/2/2c21499ae4eea72d1be19608805c9c14.png" /> </p>
<p>where,</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <img src="http://upload.wikimedia.org/math/c/f/5/cf5fbcae49d619147c98e2af9155fe01.png" /> </p>
<p>Now having that&#160; mathematics in our hands let us now try to&#160; develop a program ,we will start with&#160; simple and direct implementation and then go for efficient ones.</p>
<p> <span id="more-75"></span>
<p>But before proceed any further, let us recall one more property : <strong>C(<i>n</i>, <i>k</i>) = C(n, n-k) </strong>,this is actually a very good optimization when<strong> (k &gt; (n/2))</strong> </p>
<p>First direct implementation in&#160; C++ :&#160;&#160;&#160;&#160;&#160; </p>
<p><strong></strong></p>
<pre class="brush: js">int Factorial(int num) {
   int res = 1;
    while(num &gt; 0){
        res = res * num;
        num = num - 1;
    }
   return res;
} 

int iterative_C(int n, int k){
     if(k &gt; (n/2))
        k = n - k;
    return (Factorial(n)/(Factorial(k)*Factorial(n - k)));
}  </pre>
<p>The recursive version (C++):</p>
<pre class="brush: js">int recursive_C(int n,int k) {
    if(k &gt; (n/2))
       k = n-k;
    else if((0 == k) || (n == k))
         return 1;
    else
      return recursive_C(n-1,k) + recursive_C(n-1,k-1);
}</pre>
<p>But, both of the above two version will be very poor as per C/C++ is concerned.The reason is that C/C++ doesn’t support any BigInteger class to handle large results as produced in the intermediate steps.</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; To understand the problem let us consider one small example,Let <strong>n = 100 and k = 9 </strong>then <strong>C(100,9),</strong> Computing 100! (which looks like <a href="http://codepad.org/aPv5tVfd" target="_blank"><strong>this</strong></a>) will certainly produce overflow even if we use unsigned long long. (long long is not part of the C++09 standard but only (usually) supported as extension).</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; But in most problems of online judges inputs are like these only,so <strong>do we have to learn python or Java for solving those problems or start breaking head in writing an arbitrary high-precision integers in C++ ?</strong> <strong>The answer is NO.</strong></p>
<p>Hopefully we have a workaround , by breaking up the steps of <strong>n choose k </strong>we can easily notice that there is no need of computing all the factorials.Let us see,</p>
<p>C(8,2) = (8*7)/(2!) = (8*7)/(1*2)</p>
<p>C(15,3) = (15*14*13)/(3!) = (15*14*13)/(1*2*3)</p>
<p>C(100,5) = (100*99*98*97*96)/(5!) = (100*99*98*97*96)/(1*2*3*4*5) </p>
<p>In general,</p>
<p>C(n,k) = (n * n-1 * n-2 * n-3 * … * n-k+1) / (1 * 2 * 3 * … * k).</p>
<p>This is a simple but&#160; huge optimization. </p>
<p>A straight forward implementation in C++ :</p>
<pre class="brush: js">int optimized_integer_C(int n,int k){
    int i,temp = 1;
    if(k &gt; (n/2))
         k = n-k;
    for(i = n; i &gt;= (n-k+1); i--){
       temp = temp * i;
    }
    return (temp/Factorial(k));
}</pre>
<p>Is it all ? Can’t we optimize more ? </p>
<p>The answer is No. C/C++ has very powerful data type called double.(I am calling it powerful as I personally use this data type while solving many contests problems.)But how to use double here ? The answer is quite simply, see this code snippet :</p>
<pre class="brush: js">typedef typedef unsigned long long int UL;

double optimized_double_C(UL n,UL k){
    double answer = 1.0;
    UL i;
    if(k &gt; (n/2))
        k = n-k;
    for(i = 0; i &lt; k; i++){
         answer = answer * ((double)(n-i)/(double)(i+1));
      }
    return answer;
}

int main(void){
   double res = optimized_double_C(100,4);
   double res_1 = optimized_double_C(98,3);

   printf(&quot;C(100,4) = %.0lf \n&quot;,res);
   printf(&quot;C(98,3) = %.0lf \n&quot;,res_1);

   return 0;
}</pre>
<p>This is best solution (AFAIK) possible in C++&#160; for the concerned problem.I will soon also add a program that will present the relative differences between the discussed approaches.</p>
<p>After you finished reading this article you may be interested in solving some problems :</p>
<p>1. <a href="http://uva.onlinejudge.org/index.php?option=onlinejudge&amp;page=show_problem&amp;problem=305" target="_blank">Combinations</a></p>
<p>2. <a href="http://uva.onlinejudge.org/index.php?option=onlinejudge&amp;page=show_problem&amp;problem=471" target="_blank">Binomial Showdown</a></p>
<p>3. <a href="http://www.spoj.pl/problems/MARBLES/" target="_blank">Marbles</a></p>
<p>4. <a href="http://projecteuler.net/index.php?section=problems&amp;id=53" target="_blank">Project Euler 53.</a></p>
<p>&#160;</p>
<p>&#160;</p>
<p><strong>References: </strong></p>
<p><a title="http://en.wikipedia.org/wiki/Binomial_coefficient" href="http://en.wikipedia.org/wiki/Binomial_coefficient">http://en.wikipedia.org/wiki/Binomial_coefficient</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nthrgeek.wordpress.com/75/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nthrgeek.wordpress.com/75/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nthrgeek.wordpress.com/75/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nthrgeek.wordpress.com/75/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/nthrgeek.wordpress.com/75/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/nthrgeek.wordpress.com/75/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/nthrgeek.wordpress.com/75/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/nthrgeek.wordpress.com/75/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nthrgeek.wordpress.com/75/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nthrgeek.wordpress.com/75/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nthrgeek.wordpress.com/75/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nthrgeek.wordpress.com/75/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nthrgeek.wordpress.com/75/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nthrgeek.wordpress.com/75/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nthrgeek.wordpress.com&amp;blog=9609689&amp;post=75&amp;subd=nthrgeek&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://nthrgeek.wordpress.com/2009/12/09/combination-n-choose-k-unleashed/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">nthrgeek</media:title>
		</media:content>

		<media:content url="http://upload.wikimedia.org/math/3/8/2/382c5908d125a08662b2fedc22f4d02c.png" medium="image" />

		<media:content url="http://upload.wikimedia.org/math/d/8/e/d8e708c07c7c1d240d6b1096ef5cc353.png" medium="image" />

		<media:content url="http://upload.wikimedia.org/math/a/c/1/ac1206fd17b984fbcd6549b60eb1aa2e.png" medium="image" />

		<media:content url="http://upload.wikimedia.org/math/4/3/8/43837d1ea4c03a00f27c95c3ef78c1e6.png" medium="image" />

		<media:content url="http://upload.wikimedia.org/math/2/c/2/2c21499ae4eea72d1be19608805c9c14.png" medium="image" />

		<media:content url="http://upload.wikimedia.org/math/c/f/5/cf5fbcae49d619147c98e2af9155fe01.png" medium="image" />
	</item>
		<item>
		<title>Practical Numbers Unleashed</title>
		<link>http://nthrgeek.wordpress.com/2009/11/08/practical-numbers-unleashed/</link>
		<comments>http://nthrgeek.wordpress.com/2009/11/08/practical-numbers-unleashed/#comments</comments>
		<pubDate>Sun, 08 Nov 2009 16:10:04 +0000</pubDate>
		<dc:creator>nthrgeek</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Number Theory]]></category>
		<category><![CDATA[Algorithm for checking or generating Practical number]]></category>
		<category><![CDATA[Algorithm for practical number]]></category>
		<category><![CDATA[Check Practical numbers]]></category>
		<category><![CDATA[code for practical number in c++]]></category>
		<category><![CDATA[Generate practial number]]></category>
		<category><![CDATA[practical number algorithm]]></category>
		<category><![CDATA[Practical numbers]]></category>
		<category><![CDATA[practical numbers algorithm]]></category>
		<category><![CDATA[practical numbers in c]]></category>

		<guid isPermaLink="false">http://nthrgeek.wordpress.com/2009/11/08/practical-numbers-unleashed/</guid>
		<description><![CDATA[What is a practical number ? In number theory a positive integer N (say) is referred to as practical number or panarithmic number if every positive integer less than N is either a divisor or a sum of distinct divisors of N. For example: say N = 12 the divisors of 12 are 1,2,3,4,6. So [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nthrgeek.wordpress.com&amp;blog=9609689&amp;post=70&amp;subd=nthrgeek&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>What is a practical number ?</p>
<p>In number theory a positive integer <strong>N</strong> (say) is referred to as practical number or panarithmic number if every positive integer less than <strong>N</strong> is either a divisor or a sum of distinct divisors of <strong>N</strong>.</p>
<p>For example: say N = 12 the divisors of 12 are 1,2,3,4,6. So 1,2,3,4 &amp; 6 are represented by the divisors themselves.Then we have 5=3+2, 7=6+1, 8=6+2, 9=6+3, 10=6+3+1, and 11=6+3+2.So 12 is a practical number.</p>
<p>The first few practical numbers are: 1, 2, 4, 6, 8, 12, 16, 18, 20, 24, 28, 30, 32, 36, 40, 42, &#8230;</p>
<p>Careful observation of practical number sequence <a href="http://www.research.att.com/%7Enjas/sequences/A005153">A005153</a> in <a href="http://en.wikipedia.org/wiki/On-Line_Encyclopedia_of_Integer_Sequences" target="_blank">On-Line Encyclopedia of Integer Sequences(OEIS)</a>, reveals:</p>
<blockquote><p>&#160;</p>
<p>1.If a number is a power of 2 then it is a practical number.</p>
<p>2.No odd number can be a practical number.All practical numbers are even.</p>
<p>3.Every perfect number is a practical number.(Perfect numbers n: n is equal to the sum of the proper divisors of n.)</p>
<p>&#160;</p>
</blockquote>
<p>Keeping all this together I tried to devise an efficient algorithm : </p>
<p> <span id="more-70"></span>
<p>1.If the number is less than Zero or an Odd number, return false.</p>
<blockquote><p>2.If the number is a power of 2, return true.</p>
<p>3.Else,find all the divisors of the number.</p>
<p>4.Compute the the divisors summation of the number keeping in track current sum must be greater than or equal to the ( current divisors – 1 ).<strong>If any number failed in this this test then return false.</strong></p>
<p>5.Check if the Final sum is greater than or equal (for perfect numbers) to the number in question.If yes return true else return false.</p>
</blockquote>
<p><strong>You can speed up the process by building a prime table and then computing for the sum in the divisor computation itself.</strong></p>
<p>The algorithm put in C++ looks like this:</p>
<pre class="brush: js">typedef unsigned long int UL;

bool ispractical(UL num){
       if(0 &gt;= num)
            return false;
       if (1 == num)
            return true;
       if((1&amp;num))
            return false;
       if(!(num &amp;(num - 1)))
            return true;

      UL i, sum = 0;
      vector&lt;UL&gt; Divisors;
<ul>      Divisors.push_back(1);

      for(i=2;i*i&lt;=num;i++){
        if(0 == (num%i)){
          if(i*i == num){
            Divisors.push_back(i);
            continue;
           }
          Divisors.push_back(i);
          Divisors.push_back(num/i);
        }
      } 

     sort(Divisors.begin(),Divisors.end()); 

     UL size = Divisors.size();
     for(i = 0; i= Divisors[i]-1)
          sum+=Divisors[i];
       else
         return false;
    }
    return (sum &gt;= num);
}</ul>
</pre>
<p>&#160;</p>
<p>If you are interested you can check the entire <a href="http://codepad.org/GET8ErBo" target="_blank">program</a> of mine.It includes comments which&#160; I added during the debugging phase.</p>
<p>List of Practical numbers I generated with my program:</p>
<p><a href="http://sites.google.com/site/nthrgeekproject/practicalnumbers/PracticalNumbers100.text?attredirects=0&amp;d=1" target="_blank">Practical numbers up to 100</a> (0.028 sec)</p>
<p><a href="http://sites.google.com/site/nthrgeekproject/practicalnumbers/PracticalNumbers1000.text?attredirects=0&amp;d=1" target="_blank">Practical numbers up to 1000</a> (0.067 sec)</p>
<p><a href="http://sites.google.com/site/nthrgeekproject/practicalnumbers/PracticalNumbers10000.text?attredirects=0&amp;d=1" target="_blank">Practical numbers up to 10000</a> (0.623 sec)</p>
<p><a href="http://sites.google.com/site/nthrgeekproject/practicalnumbers/PracticalNumbers100000.text?attredirects=0&amp;d=1" target="_blank">Practical numbers up to 100000</a> (14.218 sec)</p>
<p>&#160;</p>
<p>References :</p>
<p><cite><a href="http://en.wikipedia.org/wiki/Practical_number">http://en.wikipedia.org/wiki/Practical_number</a></cite></p>
<p><cite><a href="http://mathworld.wolfram.com/PracticalNumber.html">http://<cite>mathworld.wolfram.com/PracticalNumber.html</cite></a></cite></p>
<p><cite><strong>&#160;</strong></cite></p>
</p>
<ul></ul>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nthrgeek.wordpress.com/70/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nthrgeek.wordpress.com/70/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nthrgeek.wordpress.com/70/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nthrgeek.wordpress.com/70/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/nthrgeek.wordpress.com/70/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/nthrgeek.wordpress.com/70/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/nthrgeek.wordpress.com/70/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/nthrgeek.wordpress.com/70/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nthrgeek.wordpress.com/70/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nthrgeek.wordpress.com/70/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nthrgeek.wordpress.com/70/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nthrgeek.wordpress.com/70/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nthrgeek.wordpress.com/70/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nthrgeek.wordpress.com/70/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nthrgeek.wordpress.com&amp;blog=9609689&amp;post=70&amp;subd=nthrgeek&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://nthrgeek.wordpress.com/2009/11/08/practical-numbers-unleashed/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">nthrgeek</media:title>
		</media:content>
	</item>
		<item>
		<title>Algorithm Basics</title>
		<link>http://nthrgeek.wordpress.com/2009/10/16/algorithm-basics/</link>
		<comments>http://nthrgeek.wordpress.com/2009/10/16/algorithm-basics/#comments</comments>
		<pubDate>Fri, 16 Oct 2009 12:42:34 +0000</pubDate>
		<dc:creator>nthrgeek</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[algorithm notations]]></category>
		<category><![CDATA[Asymptotic Notation]]></category>
		<category><![CDATA[different notations in the algorithm world]]></category>
		<category><![CDATA[notations in algorithm]]></category>

		<guid isPermaLink="false">http://nthrgeek.wordpress.com/2009/10/16/algorithm-basics/</guid>
		<description><![CDATA[1.What are the different notations in the algorithm world ? &#160; A book can be written to answer this question.Well,in simple terms there are two basic notations Big Oh notation &#38; Big Omega notation. Big Oh notation: Big Oh notation describes the upper limit or worst case computation of an algorithm. This notation doesn&#8217;t indicate [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nthrgeek.wordpress.com&amp;blog=9609689&amp;post=61&amp;subd=nthrgeek&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong><em></em></strong></p>
<p><strong><em>1.What are the different notations in the algorithm world ? </em></strong></p>
<blockquote><p>&#160;</p>
<p>A book can be written to answer this question.Well,in simple terms there are two basic notations <strong>Big Oh notation</strong> &amp; <strong>Big Omega notation</strong>.</p>
<p><strong>Big Oh notation:</strong> Big Oh notation describes the upper limit or worst case computation of an algorithm. This notation doesn&#8217;t indicate best case or average case it rather looks for the maximum amount of time an algorithm can take. </p>
<p>For example if you can solve some algorithm using (n^2) time complexity and after optimization, the best case might give you (nlogn) complexity but Big Oh notation will be still (n^2). Big Oh will give the idea for keeping the maximum resource to run the algorithm smoothly that is the worst case. </p>
<p>&#160;</p>
<p>Again it will be better to conclude that Big Oh gives the upper bound of an algorithm instead of saying that its the worst case to be politically correct.</p>
<p><strong>Big Omega notation:</strong> It is just opposite to the Big Oh notation. That means it is corresponding to the lower limit of any algorithm.</p>
<p>EDIT: There are also other few notation like theta notation,little-O notation,little omega Notation.Inquisitive persons may visit <a href="http://en.wikibooks.org/wiki/Data_Structures/Asymptotic_Notation#Theta_Notation" target="_blank">here</a>.</p>
<p>&#160;</p>
</blockquote>
<p>&#160;<strong><em>2.What do you mean by logn order of complexity?</em></strong>&#160; </p>
<blockquote><p>&#160;</p>
<p>If a problem can be solved by divided into sub problem and each problem can be again divided into further sub problem until it comes under control to solve by looking into it.</p>
<p>Then the problem is solvable in logn order of complexity.</p>
<p>&#160;</p>
</blockquote>
<p><strong><em>3. What is the base of logn complexity? Is the base is constant for all algorithm that gives logn complexity ?</em></strong>&#160;</p>
<blockquote><p>&#160;</p>
<p>Well,we don’t have any fixed base.The base of the log is taken from the way the problem is divided. If the problem can be divided exactly into two sub problem and then again two then the base will be 2. If the problem can be divided into 3/2 sub problem ( 1/3 ,2/3,.. each time) then the base will be 3/2. </p>
<p>.</p>
</blockquote>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nthrgeek.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nthrgeek.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nthrgeek.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nthrgeek.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/nthrgeek.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/nthrgeek.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/nthrgeek.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/nthrgeek.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nthrgeek.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nthrgeek.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nthrgeek.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nthrgeek.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nthrgeek.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nthrgeek.wordpress.com/61/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nthrgeek.wordpress.com&amp;blog=9609689&amp;post=61&amp;subd=nthrgeek&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://nthrgeek.wordpress.com/2009/10/16/algorithm-basics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">nthrgeek</media:title>
		</media:content>
	</item>
		<item>
		<title>Bit Rotate</title>
		<link>http://nthrgeek.wordpress.com/2009/10/15/bit-rotate/</link>
		<comments>http://nthrgeek.wordpress.com/2009/10/15/bit-rotate/#comments</comments>
		<pubDate>Thu, 15 Oct 2009 10:46:53 +0000</pubDate>
		<dc:creator>nthrgeek</dc:creator>
				<category><![CDATA[C/C++ interview question]]></category>
		<category><![CDATA[Puzzles]]></category>
		<category><![CDATA[Tricks]]></category>
		<category><![CDATA[Bit rotate]]></category>
		<category><![CDATA[Bit Rotate in C/C++]]></category>

		<guid isPermaLink="false">http://nthrgeek.wordpress.com/2009/10/15/bit-rotate/</guid>
		<description><![CDATA[Simulating RRC &#38; RLC in C/C++ is a bit interesting,Well Google-ing can give you some results but almost all are pretty long job. Recently I encountered this puzzle in algogeeks where it is&#160; supposed to rotate desired number of bits in an integer. Not after much thinking I am able to write two macros:(I supposed [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nthrgeek.wordpress.com&amp;blog=9609689&amp;post=54&amp;subd=nthrgeek&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Simulating RRC &amp; RLC in C/C++ is a bit interesting,Well Google-ing can give you some results but almost all are pretty long job. Recently I encountered this puzzle in <a href="http://groups.google.com/group/algogeeks" target="_blank">algogeeks</a> where it is&#160; supposed to rotate desired number of bits in an integer.</p>
<p>Not after much thinking I am able to write two macros:(I supposed 32 bit integer)</p>
<pre>

#define RLC(num,pos) ((num &lt;&lt; pos)|(num &gt;&gt; (32 - pos)))
#define RRC(num,pos) ((num &gt;&gt; pos)|(num &lt;&lt; (32 - pos)))
</pre>
<p><span id="more-54"></span></p>
<p>For guys who prefer functions instead of macros for obvious reasons :</p>
<pre class="csharpcode">unsigned <span class="kwrd">long</span> Rotateleft(unsigned <span class="kwrd">long</span> n,unsigned <span class="kwrd">long</span> i)
{
   <span class="kwrd">return</span> (n &lt;&lt; i)|(n &gt;&gt; (32 - i));
}

unsigned <span class="kwrd">long</span> Rotateright(unsigned <span class="kwrd">long</span> n,unsigned <span class="kwrd">long</span> i)
{
   <span class="kwrd">return</span> (n &gt;&gt; i)|(n &lt;&lt; (32 - i));
}</pre>
<p>&#160;</p>
<p>there nothing much to explai<strong></strong>n in this code,it’s pretty clean job but if you still struggle take a pencil &amp; paper <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nthrgeek.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nthrgeek.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nthrgeek.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nthrgeek.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/nthrgeek.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/nthrgeek.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/nthrgeek.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/nthrgeek.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nthrgeek.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nthrgeek.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nthrgeek.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nthrgeek.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nthrgeek.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nthrgeek.wordpress.com/54/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nthrgeek.wordpress.com&amp;blog=9609689&amp;post=54&amp;subd=nthrgeek&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://nthrgeek.wordpress.com/2009/10/15/bit-rotate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">nthrgeek</media:title>
		</media:content>
	</item>
		<item>
		<title>Maximum non adjacent subsequence sum</title>
		<link>http://nthrgeek.wordpress.com/2009/10/09/maximum-non-contiguous-subsequence/</link>
		<comments>http://nthrgeek.wordpress.com/2009/10/09/maximum-non-contiguous-subsequence/#comments</comments>
		<pubDate>Fri, 09 Oct 2009 15:16:53 +0000</pubDate>
		<dc:creator>nthrgeek</dc:creator>
				<category><![CDATA[C/C++ interview question]]></category>
		<category><![CDATA[Puzzles]]></category>
		<category><![CDATA[Tricks]]></category>
		<category><![CDATA[Maximum non adjacent subsequence sum]]></category>
		<category><![CDATA[Maximum non adjacent sums]]></category>
		<category><![CDATA[Maximum Non Contiguous Subsequence]]></category>

		<guid isPermaLink="false">http://nthrgeek.wordpress.com/2009/10/09/maximum-non-contiguous-subsequence/</guid>
		<description><![CDATA[Given an array all of whose elements are positive numbers, find the maximum sum of a subsequence with the constraint that no 2 numbers in the sequence&#160; should be adjacent in the array. For example, &#160; * i) 3 2 7 10 should return 13 (sum of 3 and 10) * ii) 3 2 5 [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nthrgeek.wordpress.com&amp;blog=9609689&amp;post=46&amp;subd=nthrgeek&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Given an array all of whose elements are positive numbers, find the maximum sum of a subsequence with the constraint that no 2 numbers in the sequence&#160; should be adjacent in the array. For example, </p>
<p>&#160; <br />* i) 3 2 7 10 should return 13 (sum of 3 and 10)     <br />* ii) 3 2 5 10 7 should return 15 (sum of 3, 5 and 7) </p>
<p> <span id="more-46"></span>
<p>An interesting interview problem a slight variation of the longest common subsequence problem.Many solution can be possible for me using DP here may be a overkill <img src='http://s2.wp.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' />  My straight forward O(n) solution:</p>
<p>&#160;</p>
<pre class="csharpcode"><span class="kwrd">int</span> MaxNoncontSum(<span class="kwrd">int</span> buf[],size_t cnt)
{
     <span class="kwrd">int</span> incl = 0,    <span class="rem">// max sequence including the previous item </span>
         excl = 0,   <span class="rem">// max sequence excluding the previous item </span>
         excl_new = 0; 

     size_t i; 

     <span class="kwrd">for</span>(i = 0; i&lt;cnt; i++)
       {
         excl_new = max(incl,excl); <span class="rem">// current max excluding i </span>

         incl = excl + buf[i]; <span class="rem">// current max including i </span>
         excl = excl_new;
       } 

       <span class="kwrd">return</span> max(incl,excl);
}</pre>
<p>&#160;</p>
<p>For lame guys <a href="http://codepad.org/XG0l2hxl" target="_blank">here</a> is the entire program.</p>
<p>Well this was the straight forward old interview question,What if we consider a slight variation ? Lets allow&#160; negative numbers within the array too. I am too lazy to construct a new solution for this variation,luckily a slight variation of my above solution holds good for negative numbers too <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Here is the solution:</p>
<p>&#160;</p>
<pre class="csharpcode"><span class="preproc">#define</span> max3(a,b,c) ((a)&gt;(b))?(((a)&gt;(c))?(a):(c)):(((b)&gt;(c))?(b):(c))

<span class="kwrd">int</span> __MaxNoncontSum(<span class="kwrd">int</span> buf[],size_t cnt)
 {
     size_t i;
     <span class="kwrd">int</span> incl = 0,
         excl = 0,
         excl_new = 0;

     <span class="kwrd">for</span>( i = 0; i&lt;cnt; i++)
        {
            excl_new = max3(incl,excl,0);

            incl = excl + buf[i];

            excl = excl_new;
        }

      <span class="kwrd">return</span> max3(incl,excl,0);
 }</pre>
<p>&#160;</p>
<p>Lame persons help yourself <img src='http://s2.wp.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nthrgeek.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nthrgeek.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nthrgeek.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nthrgeek.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/nthrgeek.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/nthrgeek.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/nthrgeek.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/nthrgeek.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nthrgeek.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nthrgeek.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nthrgeek.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nthrgeek.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nthrgeek.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nthrgeek.wordpress.com/46/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nthrgeek.wordpress.com&amp;blog=9609689&amp;post=46&amp;subd=nthrgeek&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://nthrgeek.wordpress.com/2009/10/09/maximum-non-contiguous-subsequence/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">nthrgeek</media:title>
		</media:content>
	</item>
		<item>
		<title>Declaration versus Definition</title>
		<link>http://nthrgeek.wordpress.com/2009/10/02/declaration-versus-definition/</link>
		<comments>http://nthrgeek.wordpress.com/2009/10/02/declaration-versus-definition/#comments</comments>
		<pubDate>Fri, 02 Oct 2009 01:36:58 +0000</pubDate>
		<dc:creator>nthrgeek</dc:creator>
				<category><![CDATA[C/C++ interview question]]></category>
		<category><![CDATA[Tricks]]></category>
		<category><![CDATA[Declaration in C/C++]]></category>
		<category><![CDATA[Declaration vs Definition in C]]></category>
		<category><![CDATA[Definition in C/C++]]></category>

		<guid isPermaLink="false">http://nthrgeek.wordpress.com/2009/10/02/declaration-versus-definition/</guid>
		<description><![CDATA[These two are very&#160; ambiguous for any newbie,there exist&#160; a very subtle difference between the two. Declaration is a promise that an object (or function) has been created. Definition is an instruction to create it. Therefore, definitions effectively count as declarations, but not vice versa. Just like all cats are animals, but not all animals [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nthrgeek.wordpress.com&amp;blog=9609689&amp;post=39&amp;subd=nthrgeek&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>These two are very&#160; ambiguous for any newbie,there exist&#160; a very subtle difference between the two.</p>
<p>Declaration is a promise that an object (or function) has been created. Definition is an instruction to create it. Therefore, definitions effectively count as declarations, but not vice versa. </p>
<p>Just like all cats are animals, but not all animals are cats.In mathematics&#160; the declaration can be considered a superset of definition or definition the subset of declaration.</p>
<p> <span id="more-39"></span>
<p>A possible clarification of this from standard can be : </p>
<p>Because of how terminology is used in the ISO Standard,    <br />in a function like this &#8212; </p>
<pre class="csharpcode"><span class="kwrd">int</span>  foo(){
        <span class="kwrd">int</span> x;
        x = 3;
        <span class="kwrd">return</span>  x;
    } </pre>
<p>the line <strong>&#8216;int x;&#8217;</strong> is a <strong>&quot;definition&quot;</strong> as the Standard uses the term, even though many or most developers would refer to this line just as a declaration. </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nthrgeek.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nthrgeek.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nthrgeek.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nthrgeek.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/nthrgeek.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/nthrgeek.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/nthrgeek.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/nthrgeek.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nthrgeek.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nthrgeek.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nthrgeek.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nthrgeek.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nthrgeek.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nthrgeek.wordpress.com/39/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nthrgeek.wordpress.com&amp;blog=9609689&amp;post=39&amp;subd=nthrgeek&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://nthrgeek.wordpress.com/2009/10/02/declaration-versus-definition/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">nthrgeek</media:title>
		</media:content>
	</item>
		<item>
		<title>Adding two numbers without &#8216;+&#8217; operator</title>
		<link>http://nthrgeek.wordpress.com/2009/10/01/adding-two-numbers-without-operator/</link>
		<comments>http://nthrgeek.wordpress.com/2009/10/01/adding-two-numbers-without-operator/#comments</comments>
		<pubDate>Thu, 01 Oct 2009 07:21:02 +0000</pubDate>
		<dc:creator>nthrgeek</dc:creator>
				<category><![CDATA[C/C++ interview question]]></category>
		<category><![CDATA[Puzzles]]></category>
		<category><![CDATA[Tricks]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[add without arithmetic operator]]></category>
		<category><![CDATA[Adding two numbers without + operator]]></category>

		<guid isPermaLink="false">http://nthrgeek.wordpress.com/2009/10/01/adding-two-numbers-without-operator/</guid>
		<description><![CDATA[Well this problem has many solution the simplest of which is num1 &#8211; (-num2); I took up one of such solution: int main() { int num1 = 3, num2 = 2,sum; sum=printf(&#34;%*s%*s&#34;,num1,&#34;&#34;,num2,&#34;&#34;); printf(&#34;%d&#34;,sum); } this trick works good, i&#8217;m pretty sure this is conforming C, but the approach is pretty hackish.&#160; It does seem pretty [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nthrgeek.wordpress.com&amp;blog=9609689&amp;post=35&amp;subd=nthrgeek&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Well this problem has many solution the simplest of which is <font color="#0000ff">num1 &#8211; (-num2);</font></p>
<p>I took up one of such solution:</p>
<pre class="csharpcode"><span class="kwrd">int</span> main()
{
    <span class="kwrd">int</span> num1 = 3, num2 = 2,sum;
   sum=printf(<span class="str">&quot;%*s%*s&quot;</span>,num1,<span class="str">&quot;&quot;</span>,num2,<span class="str">&quot;&quot;</span>);
   printf(<span class="str">&quot;%d&quot;</span>,sum); 

}  </pre>
<p><span id="more-35"></span></p>
<p>this trick works good, i&#8217;m pretty sure this is conforming C, but the approach is pretty hackish.&#160; It does seem pretty creative, though. </p>
<div id='extendedEntryBreak'></div>
<p><strong>7.19.6.4(c99)</strong>&#160; </p>
<p>&quot; the field width takes the form of an asterisk * (described later) or a <strong>nonnegative decimal integer</strong> &quot; </p>
<p>if an asterisk is used, the corresponding positional argument is used to<br />
  <br />determine the field width. </p>
<p>so, the line </p>
<p><font color="#0000ff">printf(&quot;%*s%*s&quot;, 3, &quot;&quot;, 2, &quot;&quot;); </font></p>
<p>prints &quot;&quot; within a field of 3 chars, and &quot;&quot; within a field 2 chars.<br />
  <br /><font color="#0000ff">printf()</font> returns the total number of chars printed, which is 5 + 7 in this </p>
<p>case. this effectively computes a sum without using the &#8216;+&#8217; operator. </p>
<p>but what happen if either of num1 or num2 is negative ? will it invoke undefined behavior ?</p>
<p>the answer is <strong>no</strong>. in the same paragraph of the standard (7.19.6.1, paragraph 5): </p>
<p>
  <br /><strong>&quot;a negative field width argument is taken as a &#8211; flag followed by a positive field width.&quot; </strong></p>
<p>paragraph 6 describes the effect of the &#8211; flag:&#160; it will cause<br />
  <br />the string to be left-justified within the field, rather than </p>
<p>right-justified (this obviously has no effect on the output </p>
<p>when the string has length 0). </p>
<p><font color="#0000ff">printf(&quot;%*s%s\n&quot;, 10, &quot;hello&quot;, &quot;hello&quot;); </font></p>
<p><font color="#0000ff"><br />
    <br /></font><strong>output:<br />
    <br /></strong>&#160;&#160;&#160;&#160; hellohello </p>
<p><font color="#0000ff">printf(&quot;%*s%s\n&quot;, -10, &quot;hello&quot;, &quot;hello&quot;);</font> </p>
<p><strong>output:<br />
    <br /></strong>hello&#160;&#160;&#160;&#160; hello </p>
<p>&#160;</p>
<p>thus for the following line:</p>
<p>&#160;&#160;&#160;&#160;&#160; <font color="#0000ff">printf(&quot;%*s%*s&quot;, num1, &quot;&quot;, num2, &quot;&quot;);</font></p>
<p>&#160; <br />is the sum of the <strong>absolute values</strong> of num1 and num2, which is the </p>
<p>sum of num1 and num2 only if both are nonnegative. </p>
<p>so a more elegant solution of this problem will be :</p>
<p><font color="#0000ff">int add(int x, int y)<br />
    <br />{ </p>
<p>&#160; return y ? add(x ^ y, (x &amp; y) &lt;&lt; 1) : x; </font></p>
<p><font color="#0000ff">}</font></p>
<p>explanation:<font color="#0000ff"></font></p>
<p>lets consider you want to add 34590 and 987387065 in decimal notation. </p>
<p>
  <br />&#160;&#160; 9 8 7 6 8 7 0 6 5 </p>
<p>+&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 3 4 5 5 9 0 </p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; </p>
<p>&#160;&#160; 9 8 7 9 2 2 5 5 5 &#8212;-&gt; addition of digits neglecting carry </p>
<p>+ 0 0 0 1 1 0 1 0 0 &#8212;&gt; represents carry </p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; </p>
<p>&#160;&#160; 9 8 7 0 3 2 6 5 5 </p>
<p>+ 0 0 1 0 0 0 0 0 0 </p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; </p>
<p>&#160;&#160; 9 8 8 0 3 2 6 5 5 </p>
<p>+ 0 0 0 0 0 0 0 0 0 </p>
<p>so answer is 988032655. </p>
<p>similarly, in terms of bit representation, for addition of two numbers <strong>&#8216;x&#8217;</strong> </p>
<p>and <strong>&#8216;y&#8217;</strong>, (x&amp;y)&lt;&lt;1 represents the carry number and x^y represents the number neglecting the carry. so for example let the number is 43 &amp; 14. </p>
<p>x = 43 = 1 0 1 0 1 1<br />
  <br />y = 14 = 0 0 1 1 1 0 </p>
<p>&#160;&#160; 1 0 1 0 1 1<br />
  <br />+ 0 0 1 1 1 0 </p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; </p>
<p>&#160;&#160; 1 0 0 1 0 1 &#8212;&gt; x^y </p>
<p>+ 0 1 0 1 0 0 &#8212;&gt; (x&amp;y)&lt;&lt;1 </p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; </p>
<p>&#160;&#160; 1 1 0 0 0 1 </p>
<p>+ 0 0 1 0 0 0 </p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; </p>
<p>&#160;&#160; 1 1 1 0 0 1 </p>
<p>+ 0 0 0 0 0 0 </p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; </p>
<p>hence 43 + 14 = 111001 = 57 </p>
<p>there are many other possible solution for this,but if i ever faced this in my life i will write the above one,short,simple &amp; elegant <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nthrgeek.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nthrgeek.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nthrgeek.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nthrgeek.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/nthrgeek.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/nthrgeek.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/nthrgeek.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/nthrgeek.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nthrgeek.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nthrgeek.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nthrgeek.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nthrgeek.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nthrgeek.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nthrgeek.wordpress.com/35/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nthrgeek.wordpress.com&amp;blog=9609689&amp;post=35&amp;subd=nthrgeek&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://nthrgeek.wordpress.com/2009/10/01/adding-two-numbers-without-operator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">nthrgeek</media:title>
		</media:content>
	</item>
		<item>
		<title>The Next Palindrome</title>
		<link>http://nthrgeek.wordpress.com/2009/09/28/the-next-palindrome/</link>
		<comments>http://nthrgeek.wordpress.com/2009/09/28/the-next-palindrome/#comments</comments>
		<pubDate>Mon, 28 Sep 2009 19:22:28 +0000</pubDate>
		<dc:creator>nthrgeek</dc:creator>
				<category><![CDATA[Puzzles]]></category>
		<category><![CDATA[SPOJ]]></category>
		<category><![CDATA[Tricks]]></category>
		<category><![CDATA[PALIN]]></category>
		<category><![CDATA[PALIN solution]]></category>
		<category><![CDATA[SPOJ PALIN solution]]></category>
		<category><![CDATA[SPOJ:PALIN]]></category>
		<category><![CDATA[The Next Palindrome]]></category>

		<guid isPermaLink="false">http://nthrgeek.wordpress.com/2009/09/28/the-next-palindrome/</guid>
		<description><![CDATA[Given any integer you have to compute the next palindrome. Seems very easy right ? um..how about without Brute force ? Well again the inspiration came from a SPOJ classical problem PALIN. A naive brute force will always show TLE ! Just go through the next section you may solve PALIN , Let us first [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nthrgeek.wordpress.com&amp;blog=9609689&amp;post=23&amp;subd=nthrgeek&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>Given any integer you have to compute the next palindrome. </strong>Seems very easy right ? um..how about without Brute force ? </p>
<p>Well again the inspiration came from a <a href="http://www.spoj.pl/" target="_blank">SPOJ</a> classical problem <a href="http://www.spoj.pl/problems/PALIN/" target="_blank">PALIN</a>.</p>
<p>A naive brute force will always show TLE ! Just go through the next section you may solve <a href="http://www.spoj.pl/problems/PALIN/" target="_blank">PALIN</a> ,    <br />Let us first simply rotate the number around its center,Let us&#160; refer to this as mirroring from here onwards.</p>
<p> <span id="more-23"></span>
</p>
<p>Suppose a number <strong>15110</strong>, mirror the first half values across the central <strong>&#8217;1&#8242;</strong> to give <strong>15151</strong>. If the number has an even number of digits, just mirror it about the center of the number, like <strong>4629</strong> to <strong>4664</strong>.</p>
<p>That strategy works as long as the resulting <b>&#8216;mirror-imaged result&#8217;</b> is greater than the original number.</p>
<p>If it results in a number less than or equal to the <strong>Inputted number</strong> then we need to do some more work.Now there are few cases to consider here:</p>
<blockquote><p><b>If the number had odd number of digits:</b>       <br />For this we have to increment the middle digit and then do a mirror image.       <br />So,<strong>121</strong> becomes <strong>131 </strong>and some bigger number like <strong>12321</strong> will become <strong>12421</strong>. </p>
</blockquote>
<blockquote><p><b>If the number had even number of digits: </b>      <br />For this let us increment the digit to the left of the middle , and do the mirror image.       <br />Thus,<strong>4334</strong> becomes <strong>4444.</strong></p>
</blockquote>
<p>There is one more problem ,what happen when <strong>9 </strong>is encountered,Lets, see <strong>31925</strong>. Simple mirroring would give <strong>31913</strong>, but that’s not the correct answer!Incrementing the <strong>9 to 10</strong> would give <strong>311013</strong>, which is again not a palindrome. </p>
<p>I will tackle that with carrying over ,Wherever the 10 occurred (incrementing 9), we would keep the 0 there itself, carry over the 1 to the previous digit.</p>
<p>&#160; <br />So here, <strong>98989</strong> would become <strong>99089</strong> which on applying mirroring (according to the number of digits it now have) would give <strong>99099</strong>.</p>
<p>One of the advantage of this Algorithm is the cases such as ‘999999’&#160; take care of themselves and no <b>&#8216;special cases&#8217;</b> is necessary. </p>
<p>Wow ! I explained a lot this time So I will not give away my AC solution this time instead of that you may have <a href="http://codepad.org/ejkyKyk1" target="_blank">this</a>,It’s an exact implementation of the above algorithm in C.</p>
<p><strong>Hint for <a href="http://www.spoj.pl/problems/PALIN/" target="_blank">PALIN</a>:</strong>Either implement your Bignum library in C/C++ or convert <a href="http://codepad.org/ejkyKyk1" target="_blank">this code</a> into Python or Java <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  </p>
<p>&#160;</p>
<blockquote><p><strong>EDIT:</strong> After couple of months today (October 21,2009) I solved PALIN again (On Pinged by my friend <a href="http://www.orkut.co.in/Main#Profile?uid=17858176128094951472" target="_blank">Saurabh</a>) but this time I used a bit different approach,although I got AC but this time the time complexity is&#160; bad compared to my previous solution !,Well today I am not in a mood to explain the whole Algorithm&#160; so I give away the <a href="http://codepad.org/z8XwLaYo" target="_blank">code</a> this time.</p>
<p>PS: If you think of cheating,go ahead but you are cheating no one except yourself ! <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
</blockquote>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nthrgeek.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nthrgeek.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nthrgeek.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nthrgeek.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/nthrgeek.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/nthrgeek.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/nthrgeek.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/nthrgeek.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nthrgeek.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nthrgeek.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nthrgeek.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nthrgeek.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nthrgeek.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nthrgeek.wordpress.com/23/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nthrgeek.wordpress.com&amp;blog=9609689&amp;post=23&amp;subd=nthrgeek&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://nthrgeek.wordpress.com/2009/09/28/the-next-palindrome/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">nthrgeek</media:title>
		</media:content>
	</item>
		<item>
		<title>Variation of an old Puzzle</title>
		<link>http://nthrgeek.wordpress.com/2009/09/28/variation-of-an-old-puzzle/</link>
		<comments>http://nthrgeek.wordpress.com/2009/09/28/variation-of-an-old-puzzle/#comments</comments>
		<pubDate>Mon, 28 Sep 2009 01:23:59 +0000</pubDate>
		<dc:creator>nthrgeek</dc:creator>
				<category><![CDATA[C/C++ interview question]]></category>
		<category><![CDATA[Puzzles]]></category>
		<category><![CDATA[SPOJ]]></category>
		<category><![CDATA[Tricks]]></category>
		<category><![CDATA[BCEASY]]></category>
		<category><![CDATA[C puzzles]]></category>
		<category><![CDATA[C/C++Interview Question]]></category>
		<category><![CDATA[Print 20 '*']]></category>
		<category><![CDATA[SPOJ:BCEASY]]></category>

		<guid isPermaLink="false">http://nthrgeek.wordpress.com/2009/09/28/variation-of-an-old-puzzle/</guid>
		<description><![CDATA[Let us consider the the old one first : int i, n=20; int main() { for(i=0; i&#60;n; i--) { printf("*"); } } Now the challenge is to modify this code such that it prints exactly 20 ‘*’ and nothing more, you are only permitted to make exactly ONE of these modifications to the source: 1) [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nthrgeek.wordpress.com&amp;blog=9609689&amp;post=17&amp;subd=nthrgeek&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Let us consider the the old one first :
<pre>int i, n=20;
int main() {
  for(i=0; i&lt;n; i--) {
    printf("*");
  }
}</pre>
<p>Now the challenge is to modify this code such that it prints exactly <strong>20 ‘*’</strong> and nothing more, you are only permitted to make exactly ONE of these modifications to the source: </p>
<p><span id="more-17"></span></p>
<p><p><strong>1) Add one character to the source. </strong></p>
<p><strong>2) Delete one character from the source. </strong></p>
<p><strong>3) Replace one character in the source by a different one. </strong></p>
<p>This is quite an easy and the three&nbsp; solution of this&nbsp; puzzle can be found <a href="http://codepad.org/WZZ3VOHE">here</a>. </p>
<p>But I haven’t found any solution using the constraint <strong>2</strong>,If you have it let me know. </p>
<p>Now the variation that interests me is to modify the same code such that it prints exactly <strong>21 ‘*’</strong> and nothing more, the constraints remaining the same. </p>
<p>Well this is now a bit interesting isn’t ? </p>
<p>Well I can think of a solution with bitwise complement operator (~) </p>
<p>The bitwise complement operator, the tilde, ~, flips every bit. if you have a 1, it’s a 0, and if you have a 0, it’s a 1. For sample program&nbsp; visit <a href="http://www.java2s.com/Tutorial/C/0100__Operator/Complement.htm">here</a>. </p>
<p>So now the solution of the variant using constraint 1:
<pre>int i, n=20;
int main() {
  for(i=0; ~i&lt;n; i--) {
    printf("*");
  }
}</pre>
<p>Well this is an implementation dependent solution considering that most modern computers typically use the two’s-complement representation.But fails otherwise.Have a look <a href="http://en.wikipedia.org/wiki/Signed_number_representations">here.</a> </p>
<p>But considering it’s an interview question my solution is not that bad at all <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  </p>
<p>Well,still if you had some other solution don’t forget to share. </p>
<p>Now as you completed reading this may be you are interested in solving a SPOJ tutorial problem <a href="http://www.spoj.pl/problems/BCEASY/">BCEASY</a>
<pre class="csharpcode"></pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nthrgeek.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nthrgeek.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nthrgeek.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nthrgeek.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/nthrgeek.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/nthrgeek.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/nthrgeek.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/nthrgeek.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nthrgeek.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nthrgeek.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nthrgeek.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nthrgeek.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nthrgeek.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nthrgeek.wordpress.com/17/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nthrgeek.wordpress.com&amp;blog=9609689&amp;post=17&amp;subd=nthrgeek&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://nthrgeek.wordpress.com/2009/09/28/variation-of-an-old-puzzle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">nthrgeek</media:title>
		</media:content>
	</item>
		<item>
		<title>Internet usage computation program</title>
		<link>http://nthrgeek.wordpress.com/2009/09/25/net-usage-computation-program/</link>
		<comments>http://nthrgeek.wordpress.com/2009/09/25/net-usage-computation-program/#comments</comments>
		<pubDate>Fri, 25 Sep 2009 06:22:39 +0000</pubDate>
		<dc:creator>nthrgeek</dc:creator>
				<category><![CDATA[Tricks]]></category>
		<category><![CDATA[Internet usage]]></category>
		<category><![CDATA[netstat -e]]></category>

		<guid isPermaLink="false">http://nthrgeek.wordpress.com/2009/09/25/net-usage-computation-program/</guid>
		<description><![CDATA[Ever tried finding how much you surf a session ? Most of us who had some download limit use to do the same by logging into the ISP’s website. Well,there is another option you have with the dos command NETSTAT.The documentation of the command can be found here. For this program I am not at [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nthrgeek.wordpress.com&amp;blog=9609689&amp;post=12&amp;subd=nthrgeek&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Ever tried finding how much you surf a session ? Most of us who had some download limit use to do the same by logging into the ISP’s website.</p>
<p>Well,there is another option you have with the dos command <strong>NETSTAT</strong>.The documentation of the command can be found <a href="http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/netstat.mspx?mfr=true" target="_blank">here</a>.</p>
<p>For this program I am not at all interested in options,other than <strong>–e</strong>,</p>
<p><strong>NETSTAT [-a] [-b] [-e] [-f] [-n] [-o] [-p proto] [-r] [-s] [-t] [interval] </strong></p>
<p><strong></strong></p>
<p><strong>&#160; <br />&#160; -e&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Displays Ethernet statistics. </strong></p>
<p>&#160;</p>
<p><strong></strong></p>
<p><strong>NWTSTAT –e </strong>outputs in the following format :</p>
<p> <span id="more-12"></span>
</p>
<p><strong>Interface Statistics </strong></p>
<p><strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Received&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Sent </strong></p>
<p><strong>Bytes&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 105168&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 358931      <br />Unicast packets&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 279&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 1507       <br />Non-unicast packets&#160;&#160;&#160;&#160;&#160;&#160; 836&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 1216       <br />Discards&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 0&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 0       <br />Errors&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 0&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 0       <br />Unknown protocols&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 0</strong></p>
<p>Now of these things what we need is bytes received &amp; sent,this gives your net usage in bytes but am too lazy to divide it every time&#160; by 1024×1024 or 1048576 bytes ,to get the usage in MB. So why not write a program which shows the output in MB every time it’s get executed <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  </p>
<p>well,this is actually not my idea it’s rather a friend of mine <a href="http://www.orkut.co.in/Main#Profile?uid=16054498008740516920" target="_blank">Sai Krishna</a> who first created this,his code can be found <a href="http://codepad.org/9NYfKSAv" target="_blank">here</a>.But it needs a <a href="http://codepad.org/EFSF66ap" target="_blank">batch file</a> which generate the output,Sai’s code independently do not produces the output.</p>
<p>I just made it to work from a single source file for that I send the output of the <strong>netstat –e</strong> to a temporary file and then use fscanf() to get it from there.Pretty nice trick <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .Well, <a href="http://codepad.org/mU1wEzrX" target="_blank">here</a> is what I the final code stands. <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nthrgeek.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nthrgeek.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nthrgeek.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nthrgeek.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/nthrgeek.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/nthrgeek.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/nthrgeek.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/nthrgeek.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nthrgeek.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nthrgeek.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nthrgeek.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nthrgeek.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nthrgeek.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nthrgeek.wordpress.com/12/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nthrgeek.wordpress.com&amp;blog=9609689&amp;post=12&amp;subd=nthrgeek&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://nthrgeek.wordpress.com/2009/09/25/net-usage-computation-program/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">nthrgeek</media:title>
		</media:content>
	</item>
	</channel>
</rss>
