Recommended Agency

text controls: text only | A A A

Sorry, our twitter status is currently unavailable, posted 9 minutes ago

RSS feed icon What is RSS?

blog.

18th Century Mathematician Helps Us Check An Algorithm

18th Century Mathematician Helps Us Check An Algorithm

The other day, I was discussing with one of the developers here at Focus how to approach a problem whereby we’d send off a message to a third party software service, and we wanted to know when they’d processed our message.

The problem was: we didn’t know exactly how long it would take - perhaps it would be a few seconds, perhaps a minute or more - but it seemed unlikely (from our testing) to take more than a few minutes.

We could have checked to see if they were finished every second, but that seemed a little too often - but we did want to know fairly quickly, so we didn’t want to wait minutes before checking.

We decided on a solution of a simple “backing-off” algorithm - we’d check, wait 1 second, check, wait 2 seconds, check, wait 3 seconds, check, and so on.

This means that if the process does take a while, we get gradually more relaxed about checking again as time goes on; after the 30th check, we wait 30 seconds before checking again.

(Significantly more advanced approaches may be used for this kind of algorithm, but this simple approach seemed good enough here.)

However, I thought we should sanity check ourselves - if we allowed, say, 50 of these checks, how long are we waiting at a maximum? A few minutes? An hour? Several hours? More? How can we tell?

Carl Friedrich Gauss

Gauss was an 18th Century mathematician who made a large number of contributions to many fields in maths and science, and demonstrated his skills from an early age.

There is an anecdote, which may or may not be true, but remains a good story, that whilst at primary school, his teacher asked him (perhaps to “keep him quiet” for a while!) to add up all the numbers from 1 to 100.

His teacher, we can assume, thought it would take him quite some time  to add 1 plus 2 (3), plus 3 (6), plus 4 (10), plus 5 (15), plus 6 (21) ... and so on - and of course the additions would get harder as you went onwards.

The teacher was therefore rather surprised when Gauss gave the correct answer - 5,050 - within just a few seconds.

Gauss had realised that, if you imagine the sequence of numbers in a line, the first and last numbers - 1 and 100 - when added, would produce 101. Imagine, then, the next pair inwards - 2 and 99 - they also add up to 101. So does the next pair - 3 and 98 - 4 and 97 - 5 and 96 - and so on.

In effect, you’re adding 50 pairs of numbers, all of which add up to 101.

50 times 101 is 5,050, as a primary school child can indeed tell you. (A theory I’ve tested successfully on my youngest primary school child!)

Back to our algorithm

Our problem is, of course, the same - we’re adding 1 second + 2 seconds + 3 seconds and so on, up to 50.

So, we have 25 pairs of numbers, each adding up to 51.

25 times 51 = 1,275 seconds, which equals just over 21 minutes.

This seems to fit our requirements well - 20 minutes is a fair amount of time to wait - if the third party service isn’t complete by then, it seems reasonable to assume it’s failed, and we can take the appropriate steps to follow-up manually.

Thanks, Carl

Whether or not the anecdote is true, exaggerated or apocryphal, it’s a lovely example of how a little clever thinking can make what seems like a slow manual process (adding 1, plus 2, plus 3, plus 4, plus 5 .. and so on ...) into a fairly straightforward calculation.

If you’d like the team here at This is Focus to see if we can find any clever solutions to your manual business processes, please do get in touch with us!

(Image from Wikipedia, in the public domain.)

Neil Smith
Neil

Created on Tuesday June 18 2019 09:35 AM


Tags: programming


Comments [0]








Comments


Add a comment