I had my old Windows Pocket PC 2003 stolen from my office and until now I have developed on the emulator. However, now the need for a physical device has come up and I got the HP IPAQ 214.
The old PDA was plug’n’play with respect to developing, that is, deploying from Visual Studio and so is the emulators. However, the iPAQ 214 did not allow me to deploy due to some security permissions.
Project Euler problem number #24 deals with permutations in lexicographical order. A non-brute force approach involves a mixed base number system based on factorials.
This problem is one of those that I solved using pen and paper after some research. I used the following Wikipedia pages:
Permutation where I learned that there is a connection between the lexicographical order of a permutation and
the factorial number system representation of that particular number
Finally, you will need to remember that the Wikipedia articles both counts 0,1,2,… while the Euler problem counts 1,2,3,….
To create the rotations I make a “detour” by converting the number into a string and then performing the rotations on the string.
#light
let primes_below v = Euler_utils.primes | Seq.takeWhile (fun p - p Set.of_seq
let rotate number =
let number_str = number.ToString()
let len = number_str.Length - 1
seq { for i in [ 1..len ] do
yield int64 (number_str.Substring(i) + number_str.Substring(0,i))
}
let has_rotated set number =
let numbers = rotate number
Seq.
I have never completely understood why Visual Studio had to start another (graphical) utility in order to create GUIDs. I’m all for the UNIX kind of small (text based) utilities than can be combined. Furthermore, this utility creates GUID in formats that are useful to C++ programmers. Why have they not used the built in Macro system?
On the other hand it makes it possible for me to try out Visual Studio macro programming.
For a long time I had a strange problem on my workstation. Some programs crashed before showing a window. For example, I was unable to run programs in the standard Visual Studio projects folder under my documents and settings. But they ran fine when moved somewhere else. I suspected some permission problem but I could not find and thus fix it.
Another program that I have helped develop, FarmGuard, had an even more bizarre behaviour.
The familiar for loop featured in most if not all impertive langagues is not part of most functional languages if any because it has the mutable loop index. The standard way of looping in F# is to use recursion.
In the solution to Project Euler problem 7 I have the following function
let rec findprime primes test =
if isprime primes test then test
else findprime primes (test+2)
Notice that the recursive call is placed as the very last operation in the function and it is therefore a tail call.
In order to keep the numbers in check we use the rule that
$$ a \equiv v (\bmod m) \Rightarrow a b \equiv v b (\bmod m) $$
This rule is implemented in the powermod function. Note, that the powermod function is not yet “tail recursive” and is therefore vulnerable to stack overflow for (very) large b.
#light
let rec powermod a b m =
if b = 0I then
1I
else
(a*(powermod a (b-1I) m)) % m
let sum = let numbers = [1I .
My previous task switcher application had a serious performance problem as it was programmed in .NET. In order to start the small utility you had to load up the CLR and a bunch of assemblies (dlls). If you are a .NET developer it might not be that big a problem as most of the code was already loaded up. However, for general use it was not the best option.
Hence, I ported the utitly to C++ with the MFC framework to help me.
F# has a nice syntax for creating sequences where elements are created on demand. For instance
let asequence1 = seq { 1 .. 20 };;
creates a sequence of the numbers 1 to 20. No supprise here. We are also able to include steps, that is,
let asequence2 = seq { 2 .. 2 .. 20 };;
where we only take even numbers. However, in several of the Euler problems we do not have an upper limit on the search space.
What is the average distance between two points on a circle?
We define the distance as going through the circle as going around the circle reduces the problem to the problem of average distance on a line, see [Average Distance on a Line](Average Distance on a Line).
The circle as one of the most symmetric shapes we can thing of. Thus we can select the (1,0) on the unit circle and compute the average distance to any other point.