Prime Numbers Generator

I believe I don’t have to describe what primes are, what are their properties and what not. This post is more a tribute to geek-ness of 2 friends-and-colleagues (@lucabox) that have fun thinking of algorithms to solve stupid (or less stupid), and always useless problems ;-) . Optimus Prime :-P - yeah yeah, a stupid joke Briefing This code is based on the assumption that we want to generate very very large primes, so it uses unsigned long long to store the values, instead of classical unsigned int. Live with that. ...

January 23, 2010 Â· 4 min Â· 760 words

Use Backtracking to print all Subsets

I’m “studying” this algorithmic technique: Backtracking. This is an algorithmic approach to problem solution that I trust every good Computer Scientist already uses; but taking a good theo-practical look at it is something better. A Backtracking Tree I believe you can find enough informations about it online (the 2 links I provided are more than enough), so I’ll go straight to the problem. Problem definition Given an integer n, and the set S = { 1 
 n }, calculate (print) all the possible subsets of S. For example, for n = 1, all the possible subsets are { 1 } and { } (empty set). For n = 2, all the possible subsets are: { 1 2 } { 1 } { 2 } { }. In general, for the set made of the first n Integers, the number of possible subsets is 2n. ...

January 22, 2010 Â· 4 min Â· 833 words

Pascal's Triangle generator

What’s Pascal’s Triangle? That’s what it is (Wikipedia has all the theory, if you need). Pascal’s Triangle first 6 rows The thing I wrote here is a generator of the n-th row of the triangle, that doesn’t use more then the memory needed to store the solution. Instead of allocating a Triangular Matrix, and building every row based on the one above, solution is built in place. How does it work The result is generated “filling the row from right to left”. I start initiating the element on the right hand side to ‘1’. Then, I run something like: ...

January 11, 2010 Â· 3 min Â· 480 words