The Limber Lambda

Eric Smith’s technical musings

Fun at TechEd 2009

with 3 comments

Today is the penultimate day of my three day stint at Africa’s premiere Microsoft event.  Things have improved significantly from the dull-and-wet environs that I stepped out of the plane into two days ago, to a very pleasant, warm and cloudless tropical playground.  Too bad I haven’t had a chance to do any “fun in the sun”.

The event itself has been very well organised thus far, starting with a cool, hip and happening keynote and intro party on Sunday evening.  Being the staid geek that I am though, and despite the extremely enticing party hats and masks, I left early.image

Monday was a mad-rush to make very efficient use of my time, and not a moment was wasted on anything but sessions … one after another with on average, less than 20 minutes between each one.  From sunrise to sunset, no time for lunch (lunch packs were provided for those whose information appetite overshadowed their gastric cravings).  Sadly, my eagerness has since waned.

Let’s do a walk-through to date:

Monday, August 3rd

Starting the day, Brian Noyes told us about new tools and controls for WPF.  Brian is clearly very sussed when it comes to the relatively new “dub-effs”, namely WF, WCF and WPF (anyone know what happened to CardSpace?) and I’m quite sure it doesn’t stop there.  This demo-driven talk was thorough, and correct, and aimed just right re the audience.  Problem is … I found myself nodding off.  That may of course be an indication of my generally poor ability to focus—I’m going to give Brian the benefit of the doubt here.

Next up was Bart de Smet, with a talk on how to deal with the inevitable paradigm shift from Hertz to cores.  I subscribe to Barts blog, but I have to admit that I usually set aside half a day to read any new entries, since it’s not exactly light reading.  This guy is super-bright;  we’re talking a young Richard Feynman with a sense of humour to match.  In any case, a very technical subject was dealt with deftly, and we got to hear about all the various incarnations of Microsoft’s attempt to give us a developers a way of “spreading out” to multiple cores.  The principle message here being that there’s no silver bullet (meh).

Ahmed Salijee then gave us a fairly comprehensive overview of what’s new in .NET 4.0.  Apart from the new WF stuff, there wasn’t anything that I hadn’t already gleaned from a rather old PDC Anders intro that I’d watched quite some time ago.  Ahmed was our first South African presenter, and held the flag high.  I would say though that the pre-arranged code snippets made things a little difficult to follow, but given the huge amount he got through in the relatively short amount of time, it was a noble effort.  At least everything worked!

Now jQuery is just so super-cool that I couldn’t resist the session by Hilton Giesenow on the same.  Hilton is made for speaking and there wasn’t a moment where I found myself distracted.  Everything was demo’d using the awesome Firebug to such an extent that at one point I was wondering if jQuery’s glory hadn’t been stolen by Firebug.  Of course, all the demos happened in Firefox which made the presentation all the more satisfying.  Hilton has awesome potential, but wow … things went pear-shaped.  The little bits of code that he demo’d were hand-written, and well … didn’t work.  There were regular appeals to the audience of “can someone see the problem here?”.  Just a little bit of prep can make a world of difference, Hilton.

Tuesday, 4th August

I was scheduled to attend Brian Noyes’ talk on developing service oriented workflows, but since I’d forgotten my caffeine tablets at the hotel, I opted to try out Jayesh Mowjee’s non-dev talk.  Jayesh endeavoured to inspire us with a Windows 7 security overview.  Jayesh is a seriously dynamic speaker and could probably do some stand-up moonlighting; he’s that confident.  He also knows his stuff.  Being a dev at heart, I can’t say I was inspired to preach his message from mountain-tops—let that be no reflection on his abilities though.  So what’s new in Windows 7 security?  Erm … lots of cool audit trails that’ll keep your annually visiting Delloitte and Touche droids happy as pigs in the proverbial.

Up next: John deVadoss on Windows Azure and the Cloud.  It’s funny how a small glitch in someone’s talk can ruin the whole thing—like not being able to connect to your box at Redmond so that you can do your key demo—the demo that makes or breaks your session.  Someone should have told John that we have a virtual telecoms monopoly in South Africa that on Monday just so happened to be the victim of a wage strike.  So do an rdesktop with 24-bit colour at high resolution over that thar Internet connection … at your peril.  The content?  I forget … still having flashes of embarrassment on Johns behalf, as his presentation went into an unrecoverable tail-spin.

After an extended break I attended Donald Farmers talk on a groundbreaking initiative, namely Gemini.  This presentation was stellar.  Donald went to great lengths to prepare a novel presentation for us—completely different from the somewhat tired bullet-point infested standard PowerPoint slide-set.  He used a great metaphor of the evolution of a bicycle for everyone’s workhorse … Excel.  Tandem bicycles represented Excel extended for workgroups, bicycles with shoe-spoked wheels talked to Excel being used as a square peg being pushed into a round hole, an envious kid looking on as friends ride their shiny new bikes past, communicated how everyone wants the latest-and-greatest incarnation of Excel.  All in a retro setting, for effect … a great, fresh, unexpected approach.

Oh, and the content was good too.  I got excited by this stuff because it promises to address a pervasive issue that’s close to home.  It’s the story of how in any organisation, the systems story doesn’t end at those systems that are looked after by us IT people.  There are a myriad systems out there that we don’t know about.  They may be mentioned idly in conversation—if we’re lucky.  They may be mentioned when they fail, and, unbeknownst to any IT staff, just happen to support a critical function in the business.  They live inside Excel, on a business-persons desktop.  Gemini promises to address this issue—make it maintainable, sustainable and monitor it.  Instead of chastising business for building un-maintainable spreadsheets, it supports this practice, but fills in the gaps, by providing an interactive OLAP-like experience, right inside Excel, and by tracking data that’s used by business through integration with Reporting Services.  An emphatic thumbs-up to Donald for what I’m sure we will see become the next killer app.  Excel hasn’t seen an enhancement this momentous to date.

Now, that’s not really everyone, it’s just everyone who made an impression on me.  I do get the impression though that TechEd Africa is seen as kind of a presenters sandbox.  This is because, in general, there was somewhat of a lackadaisical approach to presentation from the overseas presenters (that is, except for Donald and Bart)—almost as if it didn’t matter too much if there were screw-ups … it’s Africa after all.  The basics just weren’t covered—like doing a little research into the nature of Internet connectivity in South Africa (it’s nothing like what one gets in the States), and not hinging the better part of your presentation on an RDP session to some server at Redmond without testing it beforehand.  That’s just plain unprofessional.

In general there was also a disparity in quality of presentation between overseas presenters and local presenters—for that reason I tended to favour the Americans when choosing sessions.  Eben de Wit and his entourage could have geared up the pre-keynote prep before inflicting numerous demo failures on the audience.  I didn’t appreciate the anti-Linux gag when demo’ing the systems management stuff either, everything about Microsoft’s recent culture shift says that we’ve all moved on from juvenile sniping.

Is there anyone I haven’t managed to slag off yet?  Seriously though, all-in-all a pretty well organised affair, even though the general quality of presentations could be upped a notch.  Apart from Donald’s talk I can’t really say that I learned anything earth-shatteringly new … well … I did get to read about booting VHD’s from Windows 7 on Scott Hanselman’s blog during an inter-session break :).  In fairness, the jury isn’t out—perhaps today I’ll be completely blown away by some newfangled technology that allows you to remote to a Windows server over a flaky 54kbps connection using 10000:1 fractal compression whilst making things seem like you’re on the LAN … but I doubt it.

UPDATE: The ending keynote by Arthur Goldstuck was definitely worthwhile, otherwise Lynn Langits talk on migrating applications to Windows 7 was enlightening.  Morne Blakes “level 400″ talk “Case of the Unexplained 3″ (Mimicing Mark Russinovich’s notable series) left a whole lot to be desired and definitely wasn’t as advanced as it promised to be (pretty much everything he said I already knew).

Written by Eric Smith

August 5, 2009 at 5:05 AM

Posted in Uncategorized

On Exceptions

leave a comment »

A while ago, Justin Etheredge posted an article on best practices for when, when not, how and how not to program with exceptions in .NET.

For the most part, I agree with what he says in this article, but felt that I needed to blow my own horn on some of these issues since I get to see some fundamental misunderstandings (“misgrokkings”?) on the purpose of exceptions in code that is written by some of the guys here.

Part of me wants to start off with a tirade on the definition of the word exception, but I’ll spare you and skip that—suffice to say that exceptions really are that; they’re not supposed to be something we worry about or deal with during most of our programming—they’re … well … exceptional.  They’re supposed to be part of that whole managed thing that everyone keeps talking about.  Do you remember the good old days of C programming where everything returned an int to keep track of errors (even if that int being non-zero occurred only 1% of the time?).

In any case, a list of Smith’s rules around exceptions follow.

Rule 1: Relax, don’t Catch It

For the most part, really—carefully consider the reasons for catching exceptions.  The only times you should catch exceptions are:

  • When you need to do some kind of teardown or cleanup, regardless.  That’s why we have finally—so this isn’t really a reason for catching an exception after all;
  • when you need to align the exception with the context of what you were trying to do—a typical example of this is where an unexpected runtime exception such as NullReferenceException is thrown and you want to provide some more problem context, so wrap it in a more meaningful exception and throw;
  • When you absolutely have to recover gracefully from a condition that would normally terminate the application—be really careful here – continuing on in what will essentially be an errant state can lead to even more problems;
  • In order to log an error using a logging subsystem.  There should really only be one of these—just before the exception is about to fall through to the runtime.

Rule 2: Be Specific

When catching exceptions (and after some long, hard deliberation to reach this decision), try to be as specific as possible.  We’ll want to do:

catch (IOException ioException)
{
    // Stuff...
}

… and not:

catch
{
    // Stuff...
}

Rule 3: Don’t Throw Stuff Away

Ok, I couldn’t resist that small pun—by doing this sort of thing, we are throwing information away:

catch (NullReferenceException e)
{
    throw new InvalidOperationException(
        “The dooda can’t be used with a gidgimigadge: “ + e);
}

We would rather want to do:

catch (NullReferenceException e)
{
    throw new InvalidOperationException(
        “The dooda can’t be used with a gidgimigadge“, e);
}

The latter preserves all stack trace information, the former throws it away.

What you hide will bite you

Someone once said to me:

Ignore small problems, they’ll either go away or become big problems.

I’ve tried to keep this article positive, but I just have to have a word on this one … as someone pointed out, everybody loves code they can hate, and this is my personal sleeping blanket:

try
{
    // Stuff...
}
catch { }

This is known as exception burying.  image

In my earlier days as a developer, tinkering with various obscure scripting languages (the more arcane, the better), I bumped into Perl.  When they say that you can do something in more than one way with Perl, that’s a gross understatement—this is why I said my goodbyes to the language some time ago.  Apart from not being able to remember all of the syntax due to it’s sheer size, because the syntax is so ambiguous, you can end up writing stuff that is perfectly legal but doesn’t do what you want it to.  My initial self-high-five after writing my first Perl program and having it run without any errors quickly turned to dismay and frustration when it didn’t appear to work.  That’s because unless you “use strict” in Perl (or the ‘-w’ command-line switch), it just works … in particular, things like referencing variables that you’ve neither declared nor assigned to is just hunky-dory.

My point is: warnings and errors are good, not bad, they provide visibility into your program and give you hints about what may turn out to be problems later on.  They allow you to avoid days of trying to track a bug down because an errant condition is being buried.  Don’t bury exceptions.

A word on the appropriateness of exceptions being thrown

The .NET base class library provides a range of generic exceptions that can be used to report errors (including ArgumentException, InvalidOperationException, NotImplementedException, ArgumentOutOfRangeException and various others), and these should be used if at all possible.  The important thing though it to distinguish between exceptions that have been built to be thrown by your code, and ones that haven’t.

I can’t really think of valid situations where NullReferenceException and OutOfMemoryException should be thrown in your own code—these are things that you would catch (once again, that decision needs to be made carefully) but not throw.

Written by Eric Smith

July 15, 2009 at 6:17 AM

Posted in Development

“Sharpen Up” Series: Episode 7

leave a comment »

Do you know what a pandigital number is?  Following is the definition from Wikipedia:

“In mathematics, a pandigital number is an integer that in a given base has among its significant digits each digit used in the base at least once, for example: 1223334444555567890.”

Project Euler problem number 32 modifies the definition a little for the sake of convenience:

“We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once”
Note the deviation from the official definition:
  1. 1 to 9 instead of 0 to 9
  2. Each digit occurs only once, instead of at least once.

Now if the definition is extended to a multiplicand/multiplier/product identity, that is, like this:

39 × 186 = 7254

Then the problem becomes:

“Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.”

A “hint” is provided, although I think that this is less of a hint and more of “additional instructions”. As it turns out, the set of answers has multiple identities that result in the same product (and these are not the obvious transpositions of multiplicand and multiplier as one might think), so where there are duplicates, only one instance of the product should be added to the total. As an example:

18 x 297 = 5346

27 x 198 = 5346

In this case, the second identity is ignored.

This isn’t a hugely challenging problem – I would say the only interesting bit is finding the possible 9 digit permutations. We know that the number of permutations is 9! = 9 x 8 x 7 x 6 x 5 x 4 x 2 = 36288o.  Given the sort of computing power we have these days, churning through that list should be an absolute doddle.

Finding Permutations

I generally try not to defer to “official algorithms” when trying to solve problems like these, and as a result end up implementing something that may work but probably is not the most efficient.  There are various algorithms for calculating permutations, but the one that I came up with is of the recursive variety (recursion is fun).  It goes something like this:

  1. mySwapper = { Given a set of n digits (numbered 1 … n), swap digit i with the remaining digits where i goes from 1 … n.  For each iteration of i, return mySwapper(<remaining digits>) }
  2. mySwapper(<all the digits>)

Note that we obviously don’t need to swap digit k with digit k, but to keep the algorithm neat (read: exception-free) we’ll sacrifice a little performance.

Producing the Sum

Once we have all the possible permutations of 9 digits, what remains is to split them up into multiplicand/multiplier/product parts and test that the product is correct.  Now, since we only have 9 digits, we can make use of some common sense rules re products, that is, a product will always have at least as many digits as the sum of multiplicand and multiplier digits minus 1. The only multiplicand/multiplier/product combinations that satisfy this condition are 2/3/4 and 1/4/4 (well … 3/2/4 and 4/1/4 as well, but these provide duplicate answers).

What remains is to check that we don’t add duplicate products (like the one shown above), and to calculate the sum.  I’ve included the source code in C# below–I’m not claiming that this is particularly efficient at all though, I’m quite sure there are much faster ways of doing this using System.Int32‘s only:

using System;
using System.Collections.Generic;

class ProjectEuler32
{
    static void Main(string[] args)
    {
        var permutations = GetDigitPermutations();
        var alreadyHave = new HashSet<string>();
        Console.WriteLine(
            "Total: " +
            (GetMultiplicationComboSum(1, 4, permutations, alreadyHave) +
            GetMultiplicationComboSum(2, 3, permutations, alreadyHave)));
        Console.ReadLine();
    }

    static int GetMultiplicationComboSum(int multiplicandLen, int multiplierLen, char[][] permutationSet,
        HashSet<string> alreadyHave)
    {
        int sum = 0;
        for (int i = 0; i < permutationSet.Length; i++)
        {
			var productString = new String(permutationSet[i], multiplicandLen + multiplierLen,
				9 - multiplicandLen - multiplierLen);
			var multiplicandString = new String(permutationSet[i], 1, multiplicandLen);
			var multiplierString = new String(permutationSet[i], multiplicandLen, multiplierLen);
			var product = Convert.ToInt32(productString);
            if (Convert.ToInt32(multiplicandString) * Convert.ToInt32(multiplierString) == product)
            {
                if (alreadyHave.Contains(productString))
                {
                    Console.Write("Already have: ");
                }
                else
                {
                    alreadyHave.Add(productString);
                    sum += product;
                }
                Console.WriteLine(multiplicandString + " x " + multiplierString + " = " + productString);
            }
        }
        return sum;
    }

    static char[][] GetDigitPermutations()
    {
        var toFill = new char[9 * 8 * 7 * 6 * 5 * 4 * 3 * 2][];
        var toFillIndex = 0;
        Action<char[], int> permutationFinder = null;
        permutationFinder =
            (ca, ix) =>
            {
                if (ix == 9)
                    toFill[toFillIndex++] = ca;
                else
                {
                    var myca = new char[9];
                    ca.CopyTo(myca, 0);
                    for (var i = ix; i < 9; i++)
                    {
                        var c1 = myca[ix]; myca[ix] = myca[i]; myca[i] = c1;
                        permutationFinder(myca, ix + 1);
                        c1 = myca[ix]; myca[ix] = myca[i]; myca[i] = c1;
                    }
                }
            };
        permutationFinder(new char[] { '1', '2', '3', '4', '5', '6', '7', '8', '9' }, 0);
        return toFill;
    }
}

And here’s the output:permutations

Written by Eric Smith

May 13, 2009 at 5:58 PM

Posted in Fun

“Sharpen Up” Series: Episode 6

leave a comment »

A colleague brought my attention to this brain-teaser, from Project Euler:

The series, 11 + 22 + 33 + 44 + … + 1010 = 10405071317.

Find the last ten digits of the series, 11 + 22 + 33 + 44 + … + 10001000

Having a Unix background, my first instinct was to fire up bc which I know can deal with integers of any size you like—such a calculator is known as an arbitrary precision calculator.

Here’s the one-liner, together with execution time:

image

Now, if you’re a Java programmer, you’ll know about java.math.BigInteger which is the implementation of an arbitrary precision integer.  Sadly, in the .NET world there is no built-in equivalent (soon to change in .NET 4.0).

What I did find interesting though was that the default (and only) implementation of integer in a lot of languages, including Python and Ruby is of the arbitrary precision variety (this also applies to Scheme and Lisp).  Since I have two implementations of python installed on my PC, namely cpython and IronPython, I could benchmark them from the command line.

First, cpython: image And … IronPython:

imageUnfortunately the initialisation of the python runtime puts a nasty skew on our comparison in the case of IronPython.

Somehow, that just feels like cheating though, and even though there aren’t any set rules on Project Euler about how you come to the answer, of course, you just feel like you should be doing it a clever way; read: no use of a big integer library, built-in or otherwise.

The solution is simple, but deceptively so—why would we need to keep track of all those “more significant digits” if we never need them?  As long as we keep our eight byte integer from overflowing, we should be fine.  Hence our BigInteger-free C# version:

class Program
{
    static void Main(string[] args)
    {
        var largest = 10L*10*10*10*10*10*10*10*10*10;
        var tot = 0L;
        for (int i = 1; i < 1001; i++)
        {
            var exp = 1L;
            for (int j = 0; j < i; j++)
                exp = (exp * i) % largest;
            tot = (tot + exp) % largest;
        }
        Console.WriteLine(tot);
    }
}

And the result:

image

Written by Eric Smith

May 12, 2009 at 2:58 PM

Posted in Fun

The Great Microsoft Imposed Development Culture

with 3 comments

imageA while ago I followed up on investigating a popular metaphor amongst developers, namely, “Mort, Elvis, Einstein” (MEE).  I find it ironic that the origin of the MEE meme is Microsoft itself because there seems to be an undercurrent of bigotry surrounding it.  In short, it’s oft cited anywhere where there’s heated discussion about what I call “The Great Microsoft Imposed Development Culture”.

What constantly amazes me is the apparent lack of metrics and measures in development.  Perhaps I just haven’t done enough research, but everything I read (bar Steve Mcconnell’s writings) is full of opinion-laden anecdotal “evidence”.  There’s a whole lot of gut feel, but far too little accounting.  In fairness, that’s probably got a lot to do with how difficult it is to analyse development in that way.  Perhaps though, the most profitable software company in the world has got it right.  They may just have access to “the numbers”, and a neat little formula that takes as one of its inputs an enumerated type, namely, Mort, Elvis, Einstein, and spits out profit.

Wouldn’t such a scenario lead to an optimised developer culture—one that has been optimised for profit?  Of course, a chosen culture leads to a style in tooling as well.  Tools need to be optimised for use by the most cost-effective developer stereotype: you guessed it: Mort.

So, let’s follow this path a little further:

Q: How does Microsoft attract businesses to its platform?

A: Through the provision of a frictionless, feature-rich development platform; one that promotes visibly quick, effective solutions to business problems.

So, sure, IoC is neat and Context/Specification is da-bom, but do these things beat click-drag-and-F5 within Visual Studio backed up by a bit of copy-and-paste?  That question posed within the context of the answer above, as shocking as it may seem, has no objective answer.  Where are the numbers?

I’ve heard Bob Martin go on about the benefits of SOLID, and they all feel right to me, but if this really was as much a science as it should be (compared to say, civil engineering, for example), wouldn’t the sort of heated debate that was sparked by an episode of the StackOverflow podcast be moot?  Dead in the water.  Direct anyone who doubts the benefits of exhaustive unit testing to a neat, ratified theorem or an undeniably convincing set of statistical evidence.  If only we had such a thing.

I am a firm proponent of all things “good” (as opposed to evil, which is what many would label Mort and his cohorts), namely the SOLID principles, continuous integration, consistency of shared code, the DRY principle and various agile practices.  My gut just says it’s the right thing to do.  The question is, is it the most profitable?

Written by Eric Smith

April 30, 2009 at 12:18 PM

Posted in Methodology

The Key to Successful Software

leave a comment »

With a title like that, you’re probably expecting to hear about the elusive silver bullet–that one ingredient that makes projects finish on-time and within budget.  We all know that that’s a pipe dream … or is it?  If there were three things that were the most critical factors in determining if a software endeavour were to succeed, what would those be?  What would the most important of those three be? One word: People (capatilisation intentional).

Joel Spolsky runs a successful software company, and ‘casts to the masses on a regular basis; and guess what makes him excited?  Figuring out the best way to get the very best people on board.  He’s identified the proverbial Goose (that lays the golden egg) and things can only get better.  Developers have the latest toys at their disposal; enormous, luxurious displays, motorised desks and their own offices (with a view!).  Because Joel has realised that once you’ve bagged the alpha geek, the story is only beginning—a really productive “smart and gets things done” geek works best when surrounded by the most scintillatingly cool toys (multi-screen/big screen and really fast machine) and quiet … yes quiet (or put another way–”freedom from interruption/distraction”) makes an enourmous difference to productivity.  Justin Etheredge echoes some of these sentiments in this well-written whinge-fest.  Joel has spared no expense in clearing the path for his people because he groks it.  That being said though, not just anyone is hired at FogCreek and often good people are overlooked, but at the end of the day it’s all in aid of attracting the Best of the Best because once again … he groks it.

When the lone hero development model breaks down due to sheer project size, we need to move onto teams … or groups of co-working people.  It’s just an extension, so excellent people beget excellent teams and as Martin Fowler points out, it isn’t methodologies that succeed or fail, it’s teams that succeed or fail.  Excellent teams will always succeed.

Written by Eric Smith

January 30, 2009 at 6:54 AM

Posted in Methodology

Tagged with

“Sharpen Up” Series: Episode 5

leave a comment »

This one got me thinking for a long time, mostly because I subconsciously eliminated “cheat” solutions.  The problem is to calculate an estimate of π by working out the perimeter of an n-sided circle-inscribing polygon.  Let’s start with some pictures:

Now, the obvious solution (focus on Figure A) is to use a trigonmetric function to solve the problem.  Using a convenient r = 0.5 (implies that the perimeter of the circle is π) one side of our polygon (d) can be determined as follows:

d = 2rsin θ

And since the perimeter of the polygon = p = nd, and r = 0.5, and θ = 2π/2n, then:

p = nsin (π/n)

All too easy, and well … we’re using π to find an approximation of π.  Somehow that doesn’t sit well with me.  Which is the reason why I took two days to come up with a solution that didn’t involve trigonometric functions, or π.  Enter Figure B.

We apply a method of virtual construction–let’s assume a cartesian plane, our circle having centre (0,0), and radius r = 0.5.  The method is iterative, so we’ll need to start with a guess of the length of a side of the polygon, Rguess =Pguess / n, where Pguess is our “perimeter guess”.  Pguess can’t be any larger than π, so let’s make the upper bound = 3.2.  Rguess then forms the radius of a “construction circle”, with centre (x,y), starting at (x0,y0) in the diagram.  The intersection of said construction circle and our original circle will determine a polygon corner; of course, since there are two intersection points, we eliminate the one that has already been “visited” (or occurs where y is negative in the case of the first attempt).  We progressively “construct” our way around the circle n times, each time using the previously determined “corner” as the centre of the next construction circle, finally arriving at the final intersection point: (xn, yn).  If Rguess was correct, then (xn, yn) would equal (x0,y0).  Adjusting Rguess for the next iteration then becomes a case of increasing Pguess if yn turned out negative and decreasing Pguess if yn turned out positive.  A simple zero-by-halving (aka Bisection) technique is employed to get a value for the perimeter that satisfies the accuracy constraint (1e-9).

The source code for my π-less solution is below:


using System;

class Archimedes
{
    public double approximatePi(int numSides)
    {
        double upperPi = 3.5;
        double lowerPi = 0.0;
        while (Math.Abs(upperPi - lowerPi) > 1e-10)
        {
            bool first = true;
            double cx = 0.5;
            double cy = 0.0;
            double piGuess = (upperPi + lowerPi) / 2;
            double lastCx = 0.0, lastCy = 0.0;
            for (int i = 0; i < numSides; i++)
            {
                double[][] intersections = GetCirclesIntersection(cx, cy, piGuess, numSides);
                if (intersections[0][0] == Double.NaN ||
                    intersections[0][1] == Double.NaN ||
                    intersections[1][0] == Double.NaN ||
                    intersections[1][1] == Double.NaN)
                {
                    cy = 1;
                    break;
                }
                if (first)
                {
                    if (intersections[0][1] > 0)
                    {
                        lastCx = cx;
                        lastCy = cy;
                        cx = intersections[0][0];
                        cy = intersections[0][1];
                    }
                    else
                    {
                        lastCx = cx;
                        lastCy = cy;
                        cx = intersections[1][0];
                        cy = intersections[1][1];
                    }
                    first = false;
                }
                else
                {
                    if (Math.Abs(intersections[0][0] - lastCx) < 1e-10 &&
                        Math.Abs(intersections[0][1] - lastCy) < 1e-10)
                    {
                        lastCx = cx;
                        lastCy = cy;
                        cx = intersections[1][0];
                        cy = intersections[1][1];
                    }
                    else
                    {
                        lastCx = cx;
                        lastCy = cy;
                        cx = intersections[0][0];
                        cy = intersections[0][1];
                    }
                }
            }
            if (cy < 0.0)
            {
                lowerPi = piGuess;
            }
            else
            {
                upperPi = piGuess;
            }
        }
        return upperPi;
    }

    private double[][] GetCirclesIntersection(double xo, double yo, double piGuess, int n)
    {
        double e = xo;
        double f = yo;
        double p = Math.Sqrt(e*e + f*f);
        double k = (p * p + 0.25 - (piGuess / n) * (piGuess / n)) / (2 * p);
        return new double[2][] {
            new double[] {
                (e*k)/p + (f/p) * Math.Sqrt(0.25-k*k),
                (f*k)/p - (e/p) * Math.Sqrt(0.25-k*k) },
            new double[] {
                (e*k)/p - (f/p) * Math.Sqrt(0.25-k*k),
                (f*k)/p + (e/p) * Math.Sqrt(0.25-k*k)
            }
        };
    }
}

Written by Eric Smith

November 20, 2008 at 5:33 PM

Posted in Fun

Tagged with

“Sharpen Up” Series: Episode 4

leave a comment »

Continuing my “Sharpen Up” Series, herewith the next episode.

The problem is to find digits that:

  1. For an integer n, divide exactly into n;
  2. Also divide exactly into the sum of the digits of n.

For example, 3 divides exactly into 81 and also divides exactly into 8+1 = 9.  Except that the problem gets harder–there’s a requirement to count the digits for a particular base numbering system for which this property holds.  Using base 10, only 3 and 9 satisfy this property.  The problem statement indicates that if a digit appears to be a candidate for all possible numbers with less than 4 digits of the relevant base, then it is deemed true for all numbers.  The trivial cases of 0 and 1 should not be included in the count.

This problem took me 36 minutes to complete–here’s my solution:


using System.Collections.Generic;

// 4:30
// 5:06
// SRM150DIV1_250
public class InterestingDigits
{
    private int[] num = new int[3];

    public int[] digits(int Base)
    {
        List<int> interesting = new List<int>();
        for (int d = 2; d < Base; d++)
        {
            for (int i = 0; i < 3; i++)
                num[i] = 0;
            num[0] = d;
            bool exception = false;
            while (true)
            {
                if (((num[0] + num[1] + num[2]) % d == 0 &&
                     (num[0] + num[1]*Base + num[2]*Base*Base) % d != 0) ||
                    ((num[0] + num[1] + num[2]) % d != 0 &&
                     (num[0] + num[1]*Base + num[2]*Base*Base) % d == 0))
                {
                    exception = true;
                    break;
                }
                if (++num[0] == Base)
                {
                    num[0] = 0;
                    if (++num[1] == Base)
                    {
                        num[1] = 0;
                        if (++num[2] == Base)
                        {
                            break;
                        }
                    }
                }
            }
            if (!exception)
            {
                interesting.Add(d);
            }
        }
        return interesting.ToArray();
    }
}

Written by Eric Smith

November 7, 2008 at 2:20 PM

Posted in Fun

“Sharpened Up”: Episode 3 Optimised

leave a comment »

Lesson learned–there’s a way of approaching TopCoder problems, and speeding through the problem description (missing some important, sometimes subtle hints as you go) is not the way.  Give yourself some time, maybe pull out a pencil and paper, doodle a bit, let your mind free.  Only then do you avail yourself of that light-bulb moment.

So, it turns out that the fast-food joint problem really isn’t that hard at all.  In a nutshell the time that a customer waits can be summed up as follows:

Waitn = Arrival0 – Arrivaln + Waitn-1 + Servicen-1

… but only if Arrival0 + (Waitn-1 + Servicen-1) is later than (greater than) Arrivaln, otherwise Waitn = 0

… and of course, Wait0 = 0

That’s it–here’s the source:


public class BigBurger
{
    private int _MaxWait = 0;

    public int maxWait(int[] arrival, int[] service)
    {
        _MaxWait = 0;
        WaitFor(arrival, service, arrival.Length-1);
        return _MaxWait;
    }

    private int WaitFor(int[] arrival, int[] service, int ix)
    {
        if (ix == 0)
            return 0;
        int w = WaitFor(arrival, service, ix - 1) + service[ix - 1];
        w = (arrival[0] + w) > arrival[ix] ? arrival[0] + w - arrival[ix] : 0;
        _MaxWait = _MaxWait < w ? w : _MaxWait;
        return w;
    }
}

Written by Eric Smith

November 6, 2008 at 6:10 AM

Posted in Fun

“Sharpen Up” Series: Episode 3

leave a comment »

This is officially horribly embarrassing: 86 out of 250 points, and 1.5 hours later.  I simply could not get my mind around this one and out of sheer frustration ended up simulating the scenario (no doubt the most suboptimal solution) in painstaking detail.

This fast food joint has a queue (as they do), and a bunch of people arrive at integer value times provided in an array.  Congruent to this array is an array of integer durations of the same units indicating the time taken to serve the customer.  So if we have arrival = [1, 2, 3] and service = [10, 2, 10] then there are three customers, each arriving at time 1, 2 and 3 respectively; customer 1 has to wait 10 minutes for his order, customer 2, 2 minutes and customer 3, 10 minutes.  If consecutive customers arrive at the same time, they should be dealt with in the order in which they are encountered in the array.  The arrivals values will always be in non-descending order, and there won’t ever be a (fictitious) service of 0 minutes.  So, find the maximum time any one customer needs to wait to get to order.

It occurred to me that all of this isn’t very much help for anyone who chooses to try these problems themselves without benchmark input values and corresponding answers.  Here are the ones I worked against: {3, 3, 9} {2, 15, 14} = 11, {182} {11} = 0, {2,10,11} {3, 4, 3} = 3, {2,10,12} {15,1,15} = 7.

… and here’s my solution:


using System.Collections.Generic;

public class BigBurger
{
    public int maxWait(int[] arrival, int[] service)
    {
        int[] waitingTime = new int[arrival.Length];
        Queue<int> queue = new Queue<int>();
        int maxwait = 0;
        int index = 0;
        int time = 0;
        int lastStart = 0;
        bool started = false;
        while (queue.Count > 0 | !started | index < arrival.Length)
        {
            time++;
            foreach (int v in queue)
            {
                waitingTime[v]++;
            }
            if (queue.Count > 0 && (time - lastStart) >= service[queue.Peek()])
            {
                queue.Dequeue();
                lastStart = time;
                if (queue.Count == 0 && index < arrival.Length)
                {
                    started = false;
                }
            }
            while (index < arrival.Length && arrival[index] == time)
            {
                if (!started)
                {
                    lastStart = time;
                    started = true;
                }
                queue.Enqueue(index);
                index++;
            }
        }
        for (int i = 0; i < waitingTime.Length; i++)
        {
            maxwait = maxwait < (waitingTime[i]-service[i])
                ? (waitingTime[i]-service[i]) : maxwait;
        }
        return maxwait;
    }
}

Written by Eric Smith

November 5, 2008 at 6:03 PM

Posted in Fun