## How to parse a boolean expression and load it into a class?

TL;DR: If you want to see the code, jump to the second portion of the answer. I would build a tree from the expression to parse and then traverse it depth first. You can refer to the wikipedia article about Binary Expression Trees to get a feel for what I’m suggesting. Start by adding the … Read more

## Avoid Overflow when Calculating π by Evaluating a Series Using 16-bit Arithmetic?

Take a look at related QA: Baking-Pi Challenge – Understanding & Improving Its using Wiki: Bailey–Borwein–Plouffe_formula which is more suited for integer arithmetics. The real challenge however would be: How do I convert a very long binary number to decimal?. As you probably want to print the number in dec base … Also if you … Read more

Categories c

## StackOverflowError computing factorial of a BigInteger?

The problem here looks like its a stack overflow from too much recursion (5000 recursive calls looks like about the right number of calls to blow out a Java call stack) and not a limitation of BigInteger. Rewriting the factorial function iteratively should fix this. For example: public static BigInteger factorial(BigInteger n) { BigInteger result … Read more

## Procedurally transform subquery into join

Converting a subquery into a JOIN can be pretty straightforward: IN clause FROM TABLE_X x WHERE x.col IN (SELECT y.col FROM TABLE_Y y) …can be converted to: FROM TABLE_X x JOIN TABLE_Y y ON y.col = x.col Your JOIN criteria is where you have direct comparison. EXISTS clause But there are complications when you look … Read more

Categories sql

## 64/32-bit division on a processor with 32/16-bit division

See “Hacker’s Delight”, multiword division (pages 140-145). The basic concept (going back to Knuth) is to think of your problem in base-65536 terms. Then you have a 4 digit by 2 digit division problem, with 2/1 digit division as a primitive. The C code is here: https://github.com/hcs0/Hackers-Delight/blob/master/divmnu.c.txt

## Algorithm for solving Sudoku

Here is my sudoku solver in python. It uses simple backtracking algorithm to solve the puzzle. For simplicity no input validations or fancy output is done. It’s the bare minimum code which solves the problem. Algorithm Find all legal values of a given cell For each legal value, Go recursively and try to solve the … Read more

## Counting bits set in a .Net BitArray Class

This is my solution based on the “best bit counting method” from http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel public static Int32 GetCardinality(BitArray bitArray) { Int32[] ints = new Int32[(bitArray.Count >> 5) + 1]; bitArray.CopyTo(ints, 0); Int32 count = 0; // fix for not truncated bits in last integer that may have been set to true with SetAll() ints[ints.Length – 1] … Read more

## Postfix notation to expression tree

Create a stack containing nodes that could be part of a tree Push operands on a stack (A, 2, B, etc. are operands) as leaf-nodes, not bound to any tree in any direction For operators, pop the necessary operands off the stack, create a node with the operator at the top, and the operands hanging … Read more

## Best algorithm for matching colours.

Convert all of the colors to the CIE Lab color space and compute the distance in that space deltaE = sqrt(deltaL^2 + deltaA^2 + deltaB^2) Colors with the lowest deltaE are the most perceptually similar to each other.

## Distance from a point to a polygon

Your best bet is to iterate over all the lines and find the minimum distance from a point to a line segment. To find the distance from a point to a line segment, you first find the distance from a point to a line by picking arbitrary points P1 and P2 on the line (it … Read more