Find Sum (i … j) in a list

The problem is to sum the ith to jth element of a given list. The easiest solution runs in \mathcal{O}(n) time, which starts are the ith index and adds up the numbers in the list until it reaches the jth index. In this post i will show the process of calculating the sum of elements i through j both inclusive (where i \le j), which takes \mathcal{O}(1) time for finding the sum, and \mathcal{O}(n) time and space for preprocessing the given list of length n.
Continue reading “Find Sum (i … j) in a list”

Find a String in an Alphabet Grid

Everybody of us has definitely played the word search game in which a grid of characters are given, and a list of words are given which are to be found in the grid horizontally, vertically or diagonally. In this post i will show a simple solution to a more generalized version of the search which finds the strings in the grid with backtracking.
Continue reading “Find a String in an Alphabet Grid”

Towers of Hanoi Iterative process: simulating recursion

The last post Recursive Solution to Towers of Hanoi described the well-known recursive definition and implementation of the Towers of Hanoi problem. This post is an extension presenting the same problem iteratively by simulating the recursion stack. This implementation will simply to simulate the recursion presented on the previous post by using an explicit manual stack.

Continue reading “Towers of Hanoi Iterative process: simulating recursion”

Sieve of Eratosthenes : A basic implementation

The Sieve of Eratosthenes is a process to find all primes not exceeding a specific positive integer. The process is simple. First all the numbers within the range is populated in a list.The process strikes out all the numbers in the list which are multiple of some previous number. This strike-out process starts with the integer 2, thus first all the multiple of 2 upto the limit n are striked out. Note that the number 2 itself is not striked out, because it is not a multiple of any integer lesser than it (except 1). Next the just following unstriked integer is selected, which is in this case 3, and then all the multiples of 3 are striked out from the like, and again the next unstriked out integer is selected and the process continues until no such integer remains in the list which is unstriked and a multiple of some integer lesser that it. Note that the next integer to strike out after the ith unstriked integer is i * i , as all the multiples of i less than i are striked in the previous passes. At the end if the process only those numbers will be still unstriked in the list which are not a multiple of some other integer lesser that it, thus only the prime numbers will remain unstriked in the list. At the end of the process the list can be rescanned and the unstriked integers could be found.
Read more about Sieve of Eratosthenes here:
Read the complete implementation

Hero’s Method : Evaluating square root of a real number

A floating point number is given. the task is to evaluate the value of its square root. We will discuss how to find the square root of a real number in this post, and also present a C Language code which does this job. The value of the root will be evaluated with the Hero’s method.
Continue reading “Hero’s Method : Evaluating square root of a real number”