Category Archives: Mathematics

Mind The Gap

University of Chicago statistics major and admitted “geography and data nerd” Sasha Trubetskoy has use the iconography of the London Underground map to develop a map detailing the extent of Roman Roads circa 125AD. The result is spectacular.

Via Big Think

Flattr this!

A European adult with a computer can be as smart as a Vietnamese Eight year old

I’m quite liking the puzzles coming out of Alex Bellos’s Adventures in Numberland. This week’s challenge: Can you do the maths puzzle for Vietnamese eight-year-olds that has stumped parents and teachers?

You have a simple arithmetic equation and you have to place the digits from one to 9 in the grid so that the result is 66. And I thought that sounded pretty easy – there are only 362880 possible combinations, I just need a trial and error method to work through the combinations until I find the right one.

Thank you Python.

Firstly, a function to yield all the possible permutations in a list

def yield_permutations(the_list):
    """ Yields all permutations for a list """
    length = len(the_list)
    if length <= 1:
        yield the_list
    else:
        for i in range(0, length):
            for j in yield_permutations(the_list[:i] + the_list[i+1:]):
                yield [the_list[i]] + j

And then I just need to plug the values into the formula

digits = []
for i in range(1, 10): 
    digits.append(i)

for x in yield_permutations(digits):
    result = x[0] + 13 * x[1] / x[2] + x[3] + 12 * x[4] - x[5] - 11 + x[6] * x[7] / x[8] - 10
    print(x, result)
    if result == 66:
        break

I’m sure there is a more elegant way of doing this, but after checking my result by hand, I can confirm that this approach also works.

Flattr this!

Einstein’s election riddle

Back before we were all online, I used to spend quite a lot of time doing logic puzzles. These are problems in which you have a series of groups, a series of statements and have to figure out which elements make up each group. So when Alex Bellos posted an election themed puzzle a few days before the big day, I couldn’t resist.

There are five houses with the outside walls painted in five different ways. David, Ed, Nick, Nicola and Nigel each live in one of the houses. They each drink a certain type of coffee, have a preferred mode of transport and keep a certain pet. No owners have the same pet, the same preferred mode of transport or drink the same type of coffee.

Who owns the fish?

You will need to click through to see the actual statements about who lives where, what they drink and how they travel.

It took me a couple of hours (spread over most of a day) but I solved it, and then I checked the published solution. What struck me as interesting is that, while my approach worked, it was not the same approach as the one Alex used. You can see the approach taken by Alex, along with the solution, by clicking here. The approach I took is as follows:

I started with a grid like this one (except the grid I used was hand drawn with a ruler and pencil).

fish0

The first two statements tell us that Nicola lives in the tartan house and Ed has a guinea pig. This also tells us that the owner of the guinea pig doesn’t live in the tartan house.

fish1

Statement three tells us that David drinks mochaccino. Which means that the mochaccino drinker does not live in the tartan house and does not own a guinea pig.

fish2

And so on and so forth. And once the grid is filled you have your answer.

The article repeats the claim that only two per cent of the population are smart enough to solve it. I don’t think this is a question of being smart.

With any sort of logic problem you need to have some method of systematically capturing what is true and what is not true. Evidently more than one such method exists, but once you have a working methodology, these problems are solvable for anyone.

So if it is true that only two percent of the population are able to solve the puzzle, this does not tell us how smart people are but, instead, indicates that far too many people lack the skills to process information methodologically.

Also, what the hell is mochaccino?

Flattr this!

Exponential Origami

Raju Varghese (via Sploid) claims that if you fold a piece of paper 103 times, the thickness of your paper will be larger than the diameter of the observable universe: 93 billion light years. The explanation is simple enough – every time you fold a piece of paper you double its thickness and when you start doubling things they get very large very quickly – but I couldn’t leave this without checking the numbers for myself.

Of course, I couldn’t resist checking this for myself and pulled out a calculator. I soon found that the mental juggling needed to get from fractions of millimetres to kilometres was too much for my little brain and converting between millimetres and light years was going to be impossible.

So I wrote a script. The code is pretty simple, as you can see below, although I did have a four fold discrepancy when I first ran it (I came up with 107 folds needed, rather than 103). It turned out that my initial thickness of the paper was out by a factor of 10. Once I fixed this, everything matched.

#!/usr/bin/env python
""" Foldpaper
    Calculates the thickness of a piece of paper after n folds """

thickness = 0.1
folds = 0
meter = 1000
kilometer = 1000000
lightyear = 1000000 * 9000000000000
size_of_universe_in_mm = 93000000000 * lightyear

while thickness < size_of_universe_in_mm:
    thickness *= 2
    folds += 1
    if int(thickness / lightyear) > 1:
        print(folds, int(thickness/lightyear), 'light years')
    elif int(thickness / kilometer) > 1:
        print(folds, int(thickness/kilometer), 'kilometers')
    elif int(thickness / meter) > 1:
        print(folds, int(thickness/meter), 'meters')
    else:
        print(folds, thickness, 'mm')

And then, with a slight edit, I dumped the results into a table so that I could add a few comparative distances.

Folds Height Notes
15 3 metres Taller than the average human
22 419 metres Taller than The Shard in London
27 13 kilometres We’re now standing higher than Mount Everest
42 439804 kilometres Now we’ve just passed the Moon
51 225179981 kilometres And the Sun
56 7205759403 kilometres And finally we reach Pluto
69 6 light years With a single fold, we have shot past Alpha Centuri
83 107460 light years And now the thickness of our piece of paper is larger than the Milky Way
88 3438722 light years And in a few short folds, we pass Andromeda
103 112680053353 light years And with that final fold, we have exceeded the size of the Universe

Exponentiation is awesome.

Flattr this!

Prime pair sets

I started playing around with Project Euler way back in November 2011, not least as an opportunity to hone my still nascent Python skills. And I’m still learning.

Problem 60 states:

The primes 3, 7, 109, and 673, are quite remarkable. By taking any two primes and concatenating them in any order the result will always be prime. For example, taking 7 and 109, both 7109 and 1097 are prime. The sum of these four primes, 792, represents the lowest sum for a set of four primes with this property.

Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime.

This took a bit of thinking about but the approach I eventually came up with was to create a set of concetanatable primes for each prime (up to an arbitarily chosen value of 10000). Then all I would need to do is search each set for intersecting elements until I found five intersecting sets. Since the primes are generated in order, from lowest to highest, the first set of five intersecting sets will give me the answer.

Then for the implementation…

I already have a function that returns a list of primes, so that was easy, and using a dictionary keyed by prime was a no-brainer, but looking for set intersections was a bit of a struggle. Until I discovered that Python will do it for me. Using the set class it becomes remarkably easy to build a dictionary of sets and then work through them, checking intersections, until I find five sets that intersect.

My actual code is neither nice nor fast, and it isn’t going to scale at all – but I am quite pleased to have gained a handle on yet another iterable type.

Flattr this!

Comparing exponentials with logarithms

Project Euler problem 99 is as follows:

Comparing two numbers written in index form like 211 and 37 is not difficult, as any calculator would confirm that 211 = 2048 < 37 = 2187.

However, confirming that 632382518061 < 519432525806 would be much more difficult, as both numbers contain over three million digits.

Using base_exp.txt, a 22K text file containing one thousand lines with a base/exponent pair on each line, determine which line number has the greatest numerical value.

This had me stumped for a while because, as far as I could find, there is simply no realistic way of calculating the large exponentials in the list. And then I learned something:

log(xy) = ylog(x)

And once you know that, finding the solution is very easy indeed.

Flattr this!

Statistics with Bad Drawings

Ben Orlin at Math with Bad Drawings has embarked on a series of stories on probability. Chapter 2 is out and it’s well worth a read – if nothing else, The Blindfold and the Chestnuts teaches that wasabi can be both evil and educational.

Go read it, the story entertainingly highlights the fact that probability is about the comparison. This is a fact that is far too frequently forgotten.

Flattr this!

Dice combinations

I’m always a little uncertain as to how far I can rasonably talk about Project Euler problems. The site is a superb resource that challenges you to develop programs to solve mathematical problems. For myself, it has helped both to improve my Python programming skills and to broaden my appreciation of just how diverse and fascinating a field Mathematics is.

However, the site does ask you not to share solutions and I can see the reason for this, If you can just copy and past the answer to a problem, then it all becomes a little pointless.

On the other hand, I do sometimes come up with solution for which I feel (I think) justifiably proud. In the case of Problem 250, the solution I was pleased with is not the solution to the problem, so I feel reasonably justified in talking about it.

So here goes…

Peter has nine four-sided (pyramidal) dice, each with faces numbered 1, 2, 3, 4.
Colin has six six-sided (cubic) dice, each with faces numbered 1, 2, 3, 4, 5, 6.

Peter and Colin roll their dice and compare totals: the highest total wins. The result is a draw if the totals are equal.

What is the probability that Pyramidal Pete beats Cubic Colin? Give your answer rounded to seven decimal places in the form 0.abcdefg

This looked to me like a simple question question of number crunching. If I know all of the combinations and frequencies that six six-sided dice can produce, and all the combinations and frequencies for nine four-sided dice, then I can simply work through every possible dice roll and see who wins.

The fun began when I decided I wanted a generic function that would give me, for any number of a dice, the frequency of every possible combination. It’s taken a while, but the final result turned out to be a lot simpler than I expected.

from itertools import product

def list_dice_combinations(number, dice):
    die = []
    for i in range(1, dice + 1):
        die.append(i)

    combinations = {}
    for roll in product(die, repeat = number):
        total = sum(roll)
        if total in combinations:
            combinations[total] += 1
        else:
            combinations[total] = 1
    return(combinations)

How you use this function is, of course, completely up to you.

Flattr this!

Fifty solutions in sixteen months

Observant readers (both of you) will notice a graphic has just popped up in the sidebar of this blog. This should update automatically to reflect my Project Euler progress.

And, yes, after 16 months I have finally solved the first 50 problems.

I have to admit that some of my solutions are a little slower than they should be, but I am quite pleased to have made it this far and it is quite nice to be able to see the extent to which my Python is improving.

Flattr this!