Category Archives: Uncategorized

Project Euler

I know I’m a little late, but I’ve only recently discovered the interesting site of projecteuler.net. For anybody not familiar with it, Project Euler is a site offering a vast collection of programming puzzles of mathematical nature for anybody to solve. It has a ranking system for its members, allowing every member to see others’ statistics with solving the offered puzzles. Most of the puzzles are pretty hard, even for the gifted mathematicians among us, and the majority of them can not be solved using brute force methods (it would just take far too long), so usually an efficient algorithm is required. Once you solve a problem you gain access to a forum thread which discusses the problem, its solution, and the various techniques and algorithms other users came up with.

Continue reading Project Euler

boost::optional and its internals

boost::optional is a very handy generalization of returning a null when there’s nothing to return. Consider a function such as strstr – either the required sub-string is found within the given string, in which case a pointer to it is returned, or a NULL value is returned to indicate that there was no such sub-string. In terms of boost::optional we would either return the same pointer to the occurrence of that sub-string (as before), or we would simply return nothing – no value at all. In this post we will demonstrate the usage of boost::optional and discuss its implementation.

Continue reading boost::optional and its internals

Enumerating permutations

There are exactly n! different permutations of n numbers. This challenge was about writing a function which is able to enumerate all these permutations, i.e. function permute(n, idx) which is able to return permutation with index idx of n numbers. The requirement is of course that all these permutations must be unique – this is in order to go over all possible permutations.

Continue reading Enumerating permutations

Using the state design pattern to implement FSMs

The State design pattern is a very useful design pattern. In this article we will exploit it to provide a very slick and elegant implementation of a Finite State Machine (FSM).

First of all, a FSM consists of a finite number of states and a predefined set of rules defining the transitions between all these states. Each state can either ACCEPT or REJECT the input – the FSM accepts the input IFF it ends up on an accepting state at the end of execution. In our design, each state will be modeled by a distinct class derived from an abstract base State class. Each state class will also contain all of its relevant transition logic. This architecture will enable us to provide a very flexible, powerful, yet intuitive and simple, implementation of any FSM.

Continue reading Using the state design pattern to implement FSMs

Reset an array in constant time

Ever wondered how to reset an entire array of N elements in a constant slice of time? This post will introduce the algorithm along with an implementation.

Let me lay out the problem. There’s an array of N integers. We would like to be able to reset that array (set all elements to zero), in a set amount of time – regardless of the value of N. The reset operation should take the same amount of time whether it operates on 100 elements or 10,000 of them.

Continue reading Reset an array in constant time

Recent blog updates

I have spent quite a few hours during the last two days making thorough changes to this blog.

One of those new updates is the change of the previous blog name, “c++ talk” (at http://cpptalk.wordpress.com), to the new name “cplusplus.co.il” –  followed by the addition of a new domain name: http://cplusplus.co.il.
However, it is important to note that the blog will remain fully reachable through the old wordpress domain and its links.

Continue reading Recent blog updates