Recursion Examples

Feb 28, 2014
During my college days, we simply refer to recursion as a method that calls itself.

Here's a simple program.
Our goal is to compute the factorial of positive integer N.
A non-recursive definition would look like this:

int val = 1;
for(int x=1; x <= N; x++)
{
 val = val * x;
}

But when we define a recursive method, it would be something like this:

int factorial(int N)
{
 if(N == 1)
  return 1;
 else
  return N * factorial(N-1);
}

Here are other examples of recursive methods.

/** Getting the product of two integer values **/

int product (int x, int y)
{
 if (y == 1)
  return x;
 else
  return x + product(x, y-1);
}

/** Displaying digits of a given number in a separate line **/

void display(int x)
{
 if (x >= 10)
  display(x/10);

 System.out.println(x%10);
}

/** Getting the sum of the square from 1 to n **/

int sumofsquare(int n)
{
 if(n == 1)
  return 1;
 else
  return (n*n) + sumofsquare(n-1);
}

/** Prints odd numbers between 1 to N **/

void printodd(int N)
{
 if (N!=1)
 {
  printodd(N-1);
  if(N%2 != 0)
   System.out.println(N);
 }
}

/** Calculates the length of a linked list **/

int length(NodeOp p)
{
 if(p == tail)
  return 1;
 else
  return 1 + length(p.next);
}
Read more ...

How to Add Syntax Highlighter in Blogger

Feb 27, 2014
Follow these very simple steps on how to add syntax highlighter for your Blogger blog.

  1. Go to Dashboard > Template.
  2. Click on Edit HTML.
  3. Copy the following code before the </head> tag.
    <!-- Syntax Highlighter Additions START -->
    <link href="http://alexgorbatchev.com/pub/sh/current/styles/shCore.css" rel="stylesheet" type="text/css" />
    <link href="http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <script src="http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js" type="text/javascript" />
     
    <script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushAS3.js" type="text/javascript" />
    <script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushBash.js" type="text/javascript" />
    <script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushColdFusion.js" type="text/javascript" />
    <script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCSharp.js" type="text/javascript" />
    <script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCpp.js" type="text/javascript" />
    <script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCss.js" type="text/javascript" />
    <script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushDelphi.js" type="text/javascript" />
    <script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushDiff.js" type="text/javascript" />
    <script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushErlang.js" type="text/javascript" />
    <script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushGroovy.js" type="text/javascript" />
    <script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js" type="text/javascript" />
    <script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js" type="text/javascript" />
    <script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJavaFX.js" type="text/javascript" />
    <script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPerl.js" type="text/javascript" />
    <script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPhp.js" type="text/javascript" />
    <script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPlain.js" type="text/javascript" />
    <script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPowerShell.js" type="text/javascript" />
    <script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPython.js" type="text/javascript" />
    <script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushRuby.js" type="text/javascript" />
    <script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushScala.js" type="text/javascript" />
    <script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushSql.js" type="text/javascript" />
    <script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushVb.js" type="text/javascript" />
    <script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js" type="text/javascript" />
      
    <script language="javascript" type="text/javascript">
     SyntaxHighlighter.config.bloggerMode = true;
     SyntaxHighlighter.all();
    </script>
    <!-- Syntax Highlighter Additions END -->
    
    • You may change the theme on this line. Click here for the list of themes.
      <link href="http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css" rel="stylesheet" type="text/css" />
      
    • You may prefer to include only the brushes that you will be using to save load time.
  4. Save template.
  5. On creating a new post, when a code snippet is to be added, go to HTML mode.
  6. Place your code snippet in between the following tags:
    <pre class="brush:csharp;">
    // code here
    </pre>
    
  7. Click Publish.

Reference(s):

Read more ...

Sample Post with Syntax Highlighter

Feb 27, 2014
This is just a sample post using syntax highlighter.
Will discuss how to install the highlighter in a different post.

/// <summary>
/// Fetches number of scripts
/// </summary>
/// <param name="rowIndex">Row index where number of scripts would be placed</param>
private void DisplayNoOfScripts(int rowIndex)
{
 int totalScripts = 0;

 string filterScript = string.Format(@"{0} NOT LIKE NULL"
  , Admin_Script.script_name.ToString(),);

 DataRow[] dr = dtScripts.Select(filterScript);

 totalScripts = dr.Length;

 dgvCategoryCountry.Rows[rowIndex].Cells[COL_DETAIL_SCRIPTS].Value = totalScripts;
}

Read more ...

"There are no games." in PSP

Feb 26, 2014
Not sure if this glitch still occurs but please allow me to share.

My PSP shows this message "There are no games." but I am pretty sure there are.
This happened when I added the game Rock Band Unplugged.

Well, it seems that installing this game deletes the GAME folder, but not the games.

To fix this, follow the steps below:

  1. Create a new folder and name it "GAME".
  2. Place the "GAME" folder inside the "PSP" folder.
  3. Create a new text file and name it "new.txt".
  4. Place "new.txt" inside the "GAME" folder.
That should fix things.

Credits to afterdawn


Read more ...