When you know a thing, to hold that you know it, and when you do not know a thing, to allow that you do not know it - this is knowledge. -Confucius-

Wednesday, December 7, 2016

Segmented Sieve to find primes in a range

There are many approaches to find the primes below a specific number. few better approaches will be Sieve of Eratosthenes and Segmented Sieve (for larger limit). Below are the Java implementation of both. Sieve of Eratosthenes Java Implementation /** * @param limit : The limit can not be large as 1000,000,000 * @return List of primes up to limit, i.e. in the range [0, limit] */ static List< integer > simpleSieve(int limit) { boolean mark[] = new boolean[limit]; Arrays.fill(mark, true); for (int p =...

Tuesday, November 22, 2016

Fenwick Tree Java Implementation

A Fenwick tree or Binary Indexed Tree is a data structure that can efficiently update elements and calculate prefix sums in a table of numbers. This structure was proposed by Peter Fenwick in 1994 to improve the efficiency of arithmetic coding compression algorithms. There are situation where we need to execute both update and query operations on an array (typically a number array). Naive solutions may take O(n) time to do a prefix sum search. And when the length of the array get increased this solution will give low performance. BIT can do the...

Monday, May 23, 2016

Finding the unpaired element in an array

Given an integer array there are odd number of elements (each are between 1 and 100). Except for one all others have a duplicate in the array. ex : [1, 3, 4, 5, 7, 6, 7, 5, 3, 1, 4] in here only unpaired element is 6. Target is to find the unpaired element of this array. We can use the Exclusive Or operation to solve this. The property we are going to use is for an integer 'a' the vale a^a is zero and...

Friday, May 13, 2016

Bit Masking

Lightweight, small sets of Booleans (native support in C/C++/Java). An integer is stored in a computer’s memory as a sequence/string of bits. " In computer science, a mask is data that are used for bitwise operations, particularly in a bit field. Using a mask, multiple bits in a byte, nibble, word (etc.) can be set either on, off or inverted from on to off (or vice versa) in a single bitwise operation " [Mask (computing)] The...

Tuesday, April 5, 2016

Ignoring in GIT

Let's say we have an eclipse project. And we have a file called MyFile.java in that project. We want to make sure that this file must not go to a remote repository when we push this to GIT (version control system) The basic thing we can do is to add this file name in .gitignore file. If your file has no changes (not modified or newly added) then this will work at first time. But assume you added this file to .gitignore...

Tuesday, March 1, 2016

JavaScript here and there

Variables created without the keyword var , are always global, even if they are created inside a function A closure is an inner function that has access to the outer (enclosing) function’s variables—scope chain. The closure has three scope chains: it has access to its own scope (variables defined between its curly brackets), it has access to the outer function’s variables, and it has access to the global...

Thursday, February 18, 2016

Why it's bad to use floting point numbers for equality or condition checking ?

Most of the suggestions are to avoid floating-point numbers from equality checking or in condition checking. Because small errors in the rightmost decimal places are likely to arise when calculations are performed on floating-point numbers, And two float or double values (float and double are also different) rarely are exactly equal. assume , double oneThird = 1.0 / 3.0; double x = oneThird + oneThird + oneThird; actually...