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

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

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