Fol­low­ing my back-to-back talks with the UK Devel­op­ers Group and NxtGenUG Southamp­ton, I just like to say thanks those guys for hav­ing me, it’s been a great pleasure :-)

For any­one inter­ested, here are the links to the slides and the source code I used for the demo.

Slides: http://www.slideshare.net/theburningmonk/introduction-to-aspect-oriented-programming

Source Code: http://aop-demo.s3.amazonaws.com/AopDemo.zip

Share

Prob­lem

The square root of 2 can be writ­ten as an infi­nite con­tin­ued fraction.

image7

The infi­nite con­tin­ued frac­tion can be writ­ten, ?2 = [1;(2)], (2) indi­cates that 2 repeats ad infini­tum. In a sim­i­lar way, ?23 = [4;(1,3,1,8)].

It turns out that the sequence of par­tial val­ues of con­tin­ued frac­tions for square roots pro­vide the best ratio­nal approx­i­ma­tions. Let us con­sider the con­ver­gents for ?2.

image11

Hence the sequence of the first ten con­ver­gents for ?2 are:

1, 3/2, 7/5, 17/12, 41/29, 99/70, 239/169, 577/408, 1393/985, 3363/2378, …

What is most sur­pris­ing is that the impor­tant math­e­mat­i­cal constant,

e = [2; 1,2,1, 1,4,1, 1,6,1 , … , 1,2k,1, …].

The first ten terms in the sequence of con­ver­gents for e are:

2, 3, 8/3, 11/4, 19/7, 87/32, 106/39, 193/71, 1264/465, 1457/536, …

The sum of dig­its in the numer­a­tor of the 10th con­ver­gent is 1+4+5+7=17.

Find the sum of dig­its in the numer­a­tor of the 100th con­ver­gent of the con­tin­ued frac­tion for e.

Solu­tion

If you look at the con­ver­gents of ?2 and the numer­a­tors in the con­ver­gents of e, you’ll see a pat­tern emerging:

1    +    2    *   1    =    3

2    +    3    *   2    =    8

3    +    8    *   1    =    11

8    +   11   *   1    =    19

11  +   19   *   4    =    87

If you look at the sequence of numer­a­tors in the con­ver­gents of e ( 2, 3, 8, 11, … ) and the sequence of num­bers in the con­ver­gents of ?2 ( 1, 2, 1, 1, 4, 1, … ), given the cur­rent numer­a­tor ( n ) and the pre­vi­ous numer­a­tor ( n-1 ) in the sequence and the cor­re­spond­ing num­ber in the con­ver­gents of ?2 ( i )the next numer­a­tor ( n+1 ) can be cal­cu­lated using the formula:

( n+1) = ( n-1 ) + n * i

Once we have this for­mula to work with, the rest is sim­ple, the solu­tion runs in 7 mil­lisec­onds on my machine.

Share

Prob­lem

Con­sider the frac­tion, n/d, where n and d are pos­i­tive inte­gers. If n < d and HCF(n,d)=1, it is called a reduced proper fraction.

If we list the set of reduced proper frac­tions for d <= 8 in ascend­ing order of size, we get:

1/8, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 3/8, 2/5, 3/7, 1/2, 4/7, 3/5, 5/8, 2/3, 5/7, 3/4, 4/5, 5/6, 6/7, 7/8

It can be seen that 2/5 is the frac­tion imme­di­ately to the left of 3/7.

By list­ing the set of reduced proper frac­tions for d <= 1,000,000 in ascend­ing order of size, find the numer­a­tor of the frac­tion imme­di­ately to the left of 3/7.

Solu­tion

This prob­lem is fairly easy, given that the answer we’re look­ing for much be very close to 3 / 7 (0.4285714286) I sim­ply iter­ate through the denom­i­na­tors, d, and find the clos­est numer­a­tor, n, which will pro­duce a value less than 3 / 7. Then fil­ter the set so we end up with only the n, d pairs that have a GCD of 1 and pick the numer­a­tor from the n, d pair whose n / d frac­tion is the biggest.

Share

Note: Don’t for­get to check out Bench­marks page to see the lat­est round up of binary and JSON serializers.

Fol­low on from my pre­vi­ous test which showed that the ServiceStack.Text JSON seri­al­izer was the top dog in town, I came across a lit­tle library called fastJ­son on code­plex so nat­u­rally I had to test it out and see how it com­pares to the rest!

So using my Sim­ple­SpeedTester and repeat­ing the same test as before I got the fol­low­ing results:

image

And graph­i­cally, this is how they look:

image

fastJ­son was the fastest in seri­al­iza­tion and ServiceStack.Text was fastest in dese­ri­al­iza­tion but there is very lit­tle sep­a­rat­ing the two libraries in both cases. Given a dif­fer­ent data struc­ture to serialize/deserialize you might end up with slightly dif­fer­ent results but at the end of the day the two seri­al­iz­ers have sim­i­lar per­for­mance char­ac­ter­is­tics and both are some way ahead of the other JSON seri­al­iz­ers I’ve tested.

Update 2011/11/12:

Fol­low­ing on from request by @ICooper, I’ve included Jay­Rock in the mix. How­ever, as I had trou­ble dese­ri­al­iz­ing (seri­al­iz­ing was fine) the List<int> with Jay­Rock I mod­i­fied the test object slightly:

image

Oth­er­wise, the con­di­tions of the test are as before, and the results are as follows:

image

And graph­i­cally:

image

Ser­viceS­tack and fastJ­son still offer by far the best per­for­mance, espe­cially with dese­ri­al­iza­tion, but this time around Ser­viceS­tack proved to be the slight bet­ter of the two, why that’s the case with an int[] instead of a List<int> is beyond me though!

Again, if you’re inter­ested in see­ing the test code your­self, you can browse it here on Code­plex.

Share

Indexer

If your have a type that rep­re­sents a col­lec­tion of val­ues, adding a cus­tom indexer gives you a nat­ural way to index directly into the object using the .[ ] operator.

Take this sim­ple Cal­en­dar class for instance, which keeps a map (F# equiv­a­lent of a Dictionary<TKey, TValue>) of notes against Date­Time values:

image

By adding a cus­tom indexer here I’m now able to write code like this:

image

Also, as you may have noticed already, F# allows you to use non-integer para­me­ters in your indexer! In this case, I’m using a tuple of int * string * int. You can use this indexer from your C# code too:

image

One-Dimensional Slicer

That’s pretty cool, but we can go a step fur­ther by allow­ing you to use slicers on our types too. To allow users to get all the notes asso­ci­ated with a range of years, e.g. from 1999 to 2009, let’s add a slicer:

image

So now we can do this:

image

Two-Dimensional Slicer

Pretty awe­some, right? But what if we want to refine our search cri­te­ria even more and let you spec­ify a year range as well a range of months, e.g. get me all notes for April/May/June in the years 1999 to 2009. Thank­fully, F# lets you define a two-dimensional slicer too:

image

This two-dimensional slicer allows me to query the cal­en­dar with a year range as well as a month range:

image

As you can see, index­ers and slicers give the con­sumers of your code a much more intu­itive way to inter­act with the data encap­su­lated in your type. Keep in mind though, that F# does not allow you to add slicer with more than two dimen­sions, but I think two-dimensional slicers should be good enough for most day-to-day requirements.

Update 2011/11/08:

Swapped out my ver­bose optional argu­ment han­dling with default­Arg as Arseny Kapoulkine pointed out in the comments.

Share