New site made

2009 October 22
by pythonisms

I just made this site !!

http://www.paskbrothers.co.uk/

Strange ways to do multiplication

2009 September 22
by pythonisms

Open a text editor and type a single letter. Now click in the whitespace and type ctrl+a to select the letter then ctrl+x to cut it.

Now take the first number you wish to multiply and holding down ctrl press v as many times as there are units in the first number.

Now press ctrl a again to select all and then ctrl x to cut all the new letters out.

Now hold ctrl and press v as many times as there are units in the second number to be multiplied.

Now either read the number of bytes in the file from the status bar of the editor or save the file and read the size in bytes from your file manager. Strange, I know, but it does what it claims…

If anyone has any other strange ways to perform arithmetic operations then please share. We might stretch from dna based biological computing to using all the room light switches in your house to calculate in binary. Or might someone approximate integers by sugar lumps and divide hot tea in a measuring jug, then crystallise out the sugar and weigh it. It could be anything.

get thinking !

Pressure and competitiveness in science

2009 September 15
by pythonisms

A dear scientist friend of mine who shall remain nameless was once overwhelmed by grief and made a suicide attempt. I found him afterwards and insisted he go with me to the hospital. I will always remember his response “No I can’t… if any of my colleagues found out I had made a suicide attempt my career would be over.” Fortunately he was ok but it made me think…

Meritocracy usually assumes that excellence is best served by unbridled competition, but are there any scenarios where this inhibits human creativity ? i hold that there may be. Doing something for the love of it may provide a different kind of motivation to doing it to be the best. Sometimes the perfectionism of a craftsman who loves his work leads to better results than someone more based in their ego’s ambition to be the best. I feel this is also the difference between mathematics engaged in aesthetically and philosophically, and math where your aim is just to pass your exams.

Also isn’t it the case that competition helps the individual only when they win or succeed. In this respect it divides self esteem among people rather unfairly. I think we need to examine when and how our meritocratic principles work, and consider keeping control of ambition and striving.

As well as a skill to earn me a living I view math as an adjunct to meditation, and part of a spiritual drive to reach the truth. This has less to do with how good I am compared to others or how high I get ranked in my exams, although that may be a side effect.

Also taking a leisurely approach from time to time helps the knowledge become “my own”. I think this is critical in studying math since it is when you explore for yourself, armed with your unique questions that you see the real territory, more than slavishly following the path your lecturers always set. If math study is only imposed from outside there will always be a pressure to conform and achieve. Maths or other scientific tasks set by yourself for yourself are an enjoyable luxury and a comfort. By building positive associations in your mind like this you help your motivation. By paying attention to personal questions arising from a sense of mystery, you increase the dimensionality of your knowledge and its cohesion into one (or more) unified wholes.

If you enjoy a particular problem or area then that good feeling becomes associated with it in your mind and you can use it as fuel for more learning, and stay centred and at peace while you think.

Another thing about being too driven by competitiveness is that it may lead you to try to run before you can walk. There should be no shame about going back to basics, indeed this is essential. You have to know the basics like the back of your hand, but if someone asks you what you are doing and you say you are working on differential calculus in two variables they may think that is your only level. Humility is a good quality. Complexity is not the only measure of creativity too, many of the most important discoveries of science look so simple in hindsight. If you want to look oh-so-clever and only direct yourself towards complex difficult ideas you may miss simpler solutions that work. I believe we have to aim for a roundedness in this.

I liken my studies to mountaineering. Climbing the highest peak isn’t the only purpose. What about enjoying the view… ? or letting your experiences increase your wisdom and confidence in other areas. And just because yours is not a first ascent doesn’t necessarily mean you have had any less expertise or enjoyment than the pioneer.

Montecarlo Mathemagic

2009 September 13
by pythonisms

Here’s a cool mathemagic trick.

If you inscribe a circle in a square as shown, then what is the ratio of the area of the circle to the area of the square ? Well lets take the case of a unit circle… its area must be pi * r^2 = pi. The area of the square will then be 2^2 = 4

circsqu

so the ratio must be pi/4

We are going to use this fact to obtain an estimate of pi.

Imagine drawing the figure on the ground with chalk. Now scatter one or more handfuls of rice or stones over it until you have a reasonably equal distribution. The rice can fall anywhere around and inside the shapes, it doesn’t matter if the grains spill over.

Now, if the density of the grains inside the circle and the square are reasonably constant then the density per unit area of the grains should be constant, this means that the ratio of the number of grains inside the circle to the number inside the square should be the ratio of the two areas, so count the numbers, divide and multiply by 4 and you have an estimate of pi !

This was done by using a randomising method, and thus falls loosely under the category of what are called Montecarlo Simulations. They are used in many areas from finance to weather prediction.

Bayes’ Theorem and how to Dismiss Paranoia…

2009 August 30

It is amazing how a very simple mathematical formula can hold such a world of detail and be such a powerful tool in understanding reality.

One such formula is Bayes’ Theorem

This theorem tells us about the conditional probability of one event given that another event has been observed, what that is really about is the following:

How do we assess the likelihood of an event given a piece of evidence that may or may not have bearing on it?

The idea can be built up quite simply. let’s think of two events called A and B. The probabilities of each of these happening are represented as P(A) and P(B).

We’ll immediately make this concrete to explain it best. Let’s denote as follows

A : John is guilty of the murder
B : John was found at the scene after the dead body was found with blood on his hands

A has yet to be established, but we know B has already happened.

Well Bayes’s Theorem gives us one possible way to do some calculation about this. Time for notation…

P(A|B) is how we write “The probability of A happening given that B has happened.

it turns out that P(A|B) can be expressed as

P(A|B) = P(A) * ( P(B|A) / P(B) )

So in this case P(A) will be the probability of a freely chosen individual in the sample being guilty of a murder. This is the basic rate when we have absolutely no evidence to narrow it down. In other words P(A) will be the murder rate in whatever country we are in.

P(B) will be the likelihood of a random individual being found with blood on their hands at a crime scene

and P(B|A) is the probability of someone being found with blood on their hands when it is given and therefore certain they have just committed a murder. In other words this is a quantity that is the correlation or degree of connection between murder and having blood on your hands.

To look at our example again, maybe if someone worked at an abbatoir or butcher’s they might frequently be found with blood on their hands, so it is not self evident that the given evidence guarantees the probability P(A) must be 1. However it does seem likely that this kind of evidence is compelling, therefore we expect the correlation of bloody hands with murders to be quite high. P(B) is the denominator in the right hand term though, so if B is rare and therefore P(B) small then that enhances our evidence’s power to convict. P(A) is the starting likelihood of john being a murderer before we found the evidence. So you can see that

P(B|A) / P(B) is the amount by a factor of which our evidence alters the likelihood of A. If P(B|A) is small then our evidence may not help us that much, unless P(B) is even smaller. If P(B|A) is big, i.e. if the two events of bloody hands and guilt are strongly correlated then the evidence may be helpful. If there’s slight correlation but B is a common event then once again the evidence may not be much help. You can play around with the figures yourself but its such a beautifully simple formula that it becomes quite easy to visualise.

One of the noted applications of Bayesian Reasoning of this kind is in using healthcare data about the error rates of clinical tests. A scientist online quoted an example of the Mammogram as a test for breast cancer, and to what extent we can expect a positive mammogram test to lead us to believe that the subject has breast cancer. Interestingly the figures work out to show that a small but appreciable false positive rate and a similarly small nonzero false negative rate seriously diminish the validity of the test. Here’s how it goes:

1% of women at age forty who participate in routine screening have breast cancer. 80% of women with breast cancer will get positive mammographies. 9.6% of women without breast cancer will also get positive mammographies. A woman in this age group had a positive mammography in a routine screening. What is the probability that she actually has breast cancer?

quoted from http://yudkowsky.net/rational/bayes

well if A is having cancer and B is testing positive then P(B|A) should be the frequency of people who test positive while definitely and verifiably having cancer.

so P(A), the incidence of cancer in the group, is clearly 0.01

P(B) is a little subtler, since it has to include the false positives rate, I make

P(B) = (0.01 * 0.8) + (0.096 * (1- 0.01)) = 0.10304

this is the incidence of positive tests, real illness notwithstanding

and finally

P(B|A) = 0.8

which is 1 minus the false negative rate (number of people with cancer who don’t test positive)

so here:

P(A|B) = 0.01 * (0.8 / 0.10304) = 0.07763 = 7.763%

In other words the test is really not a conclusively strong piece of evidence. The catch is that there are so many more women who don’t have breast cancer that even a relatively small proportion of false positives (people who the test says have cancer but who don’t in reality) changes the figures quite drastically.

So there we have it ! The great thing about this stuff is that there is such a myriad profusion of examples we can think up. Any area where some basic stats are known can really elucidate and sharpen our understanding of how evidence based reasoning works out in practice.

I have also personally found that going to the trouble of some rule of thumb calculations can even help allay personal fears about nasty and dangerous events, which duly do prove to be rather unlikely. Maths beats paranoia every time !!

Of Magic Squares and Combinatorial Explosions

2009 August 27

A magic square is an n x n grid of numbers where each row, column and diagonal sum to the same amount. Usually the first n^2 integers are used.

There’s a famous painting by Durer containing a 4 x 4 magic square.
durer

Shall we try bravely to write some code to generate these objects ?

Let’s start with a 3 x 3. There are nine integers, so the most apparent brute force method would be to try every permutation of these nine counting sequentially from top left to bottom right, and check with a conditional statement whether the rows and columns etc add up. It sounds pretty straightforward… How do we get every permutation ? well there’s a built in module in the python library called itertools, which includes a function called permutations that does exactly that., so we’ll start with

from itertools import permutations

This can take a list as its input, to get every one for 1-9 we could say:

a=[1,2,3,4,5,6,7,8,9]
for x in permutations(a,9):
    print(x)

which starts:


(1,2,3,4,5,6,7,8,9)
(1,2,3,4,5,6,7,9,8)

etc

each row and column must add up to (1+2+3+4+5+6+7+8+9)/3 = 15

then for each possible permutation we need to check whether the rows add up to 15, this can be done like this
from itertools import permutations
x=[1,2,3,4,5,6,7,8,9]
for a in permutations(x,9):
    if a[0]+a[1]+a[2]==15 and a[3]+a[4]+a[5]==15:
        if a[6]+a[7]+a[8]==15 and a[0]+a[3]+a[6]==15:
            if a[1]+a[4]+a[7]==15 and a[2]+a[5]+a[8]==15:
                if a[0]+a[4]+a[8]==15 and a[2]+a[4]+a[6]==15:
                    print(a[0:3])
                    print(a[3:6])
                    print(a[6:])
                    print()

You could of course put the conditional all on one line, but i staggered it so it looks better on the page width for this blog. When run we get:

>>>
(2, 7, 6)
(9, 5, 1)
(4, 3, 8 )

(2, 9, 4)
(7, 5, 3)
(6, 1, 8 )

(4, 3, 8 )
(9, 5, 1)
(2, 7, 6)

(4, 9, 2)
(3, 5, 7)
(8, 1, 6)

(6, 1, 8 )
(7, 5, 3)
(2, 9, 4)

(6, 7, 2)
(1, 5, 9)
(8, 3, 4)

(8, 1, 6)
(3, 5, 7)
(4, 9, 2)

(8, 3, 4)
(1, 5, 9)
(6, 7, 2)

>>>

(This counts reflections and transpositions as different squares by the way.)

Ok so that is fairly straightforward, and the code runs quite fast too. You might think that we can easily scale up to find some bigger ones using this method… what about trying one to find all possible 4×4 squares ?

the magic sum must be (1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16)/4 = 34

the code would be:


from itertools import permutations
x=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
for a in permutations(x,16):
    if a[0]+a[1]+a[2]+a[3]==34 and a[4]+a[5]+a[6]+a[7]==34:
        if a[8]+a[9]+a[10]+a[11]==34 and a[12]+a[13]+a[14]+a[15]==34:
            if a[0]+a[4]+a[8]+a[12]==34 and a[1]+a[5]+a[9]+a[13]==34:
                if a[2]+a[6]+a[10]+a[14]==34 and a[3]+a[7]+a[11]+a[15]==34:
                    if a[0]+a[5]+a[10]+a[15]==34 and a[3]+a[6]+a[9]+a[12]==34:
                        print(a[0:4])
                        print(a[4:8])
                        print(a[8:12])
                        print(a[12:])
                        print()

but when you start it running nothing happens for a long time. Ok maybe this brute force method would be too slow. How many permutations is it going to try ? well there are 16 digits in the list, so in fact there will be 16!, 16 factorial lists in the set.

A quick bit of code may show us how big this number is:

def fac(x):
    if x==1:
        return 1
    return x*fac(x-1)
print (fac(16))

it is 20922789888000 or about 21 trillion. So the conditionals in the 4×4 code will be executed that many times. how long does each execution of the conditionals take ? Let’s time it. this code just puts the permutations in a list, which would be quicker, but we can still use this to get an approximate lower bound for runtime

from time import time
from itertools import permutations
for a in range(2,15):
    ti=time()
    c=[]
    for x in permutations(range(a),a):
        c.append(x)
    print(a,len(c),time()-ti)
print ()

well to process all the 3628800 permutations of digits 1-10 takes about 35 seconds on my machine. so using very approximate estimates it should take about 16×15x14×13x12×11x35 seconds. this is 201801600 seconds or roughly 6 years.

Ok so maybe we have to rethink this…

Well that’s all I’ll say for now, but it serves as a useful demonstration of the power of what are called combinatorial explosions. With combinations or permutations of even relatively small sets the numbers rapidly get real big. If anyone can work out a clever way to generate all the 4×4 squares then please post.

Regarding these explosions a vivid and beguiling image I have is of having to consider all the ways that each of the ~10^80 particles in the universe could be listed in order. This is a large number. That is an understatement.

Evolving an effective prime sieve

2009 August 21
by pythonisms

Here’s a naive yet short sieve of eratosthenes in python. 

This one removes the composites from the list in situ

 until there are none left and only primes remain. 

A flaw is that many numbers are checked more than once.

 

a=list(range(2,1000))
for x in a:
   n=x
    while x*n <= max(a):
        if x*n in a:
            a.remove(x*n)
        n+=1
print(a)

How can we eliminate the redundancy ? 

The next one i tried uses a slightly different approach, 

using two lists, one of composites which is populated in the first loop 

and one of the primes which are every integer not in the composites, 

populated in the second loop.

 

comp=[]
for a in range(2,1000):
    if a not in comp:
        n=a
        while n*a <=1000:
            comp.append(n*a)
            n+=1
primes=[]
for c in range(2,1000):
    if c not in comp:
        primes.append(c)
print(primes)

but this next  one below is even better since it seems to avoid any repetition. 

For each starting factor a temporary list of composites is generated, 

these are then removed from the primes list, 

and then the logic moves to the next prime factor in the list 

and repeats the same process until its done. 

a=list(range(2,1000))
for x in a:
    comp=[]
    for y in a:
        comp.append(x*y)
    for z in comp:
        if z in a:
            a.remove(z)
print(a)

aha ! but there was redundancy in that one too.the first unchecked composite is n*n, but we were counting n*2,n*3, n*5 etc up to n*n, this can be remedied. Also we need to avoid checking the numbers when they are too large, so here is our final version, far and away the most elegant in my view.

a=list(range(2,1000))

for x in a:
    comp=[]
    for y in range(a.index(x),len(a)):
        q=x*a[y]
        if q<=max(a):
            comp.append(q)
        else:
            break
    for z in comp:
        a.remove(z)

print(a)

when you optimise code like this it becomes more scalable, for a large input the slowdown caused by inefficiencies is minimised. Also it is more aesthetic, don’t use a sledge hammer to crack a walnut !

sums of twin primes

2009 August 21
by pythonisms

This uses the fact that twin primes are always of the form 6n+-1. run the code and then enter the output numbers in the online encyclopedia of integer sequences. you might find some interesting facts. 

# numbers that can't be expressed as sums of pairs of twin primes

def isprime(x):
    """naive primality test"""
    for a in range(2,int(x**0.5)+1):
        if x%a==0:
            return False
    return True

p=[3]
for n in range(1,1000):
    b,c=6*n-1,6*n+1
    if isprime(b) and isprime(c):
        p.append(b)
        p.append(c)

c=[]
for a in range(len(p)):
    for b in range(a,len(p)):
        c.append(p[a]+p[b])

c.sort()

for a in range(2,1000,2):
    if a not in c:
        print(a)
    

Double factorial primes

2009 August 21

The double factorial of n is written n!! and is given by n * n-2 * n-4 … * 1.

we have looked at primes of the form n!+-1 in another post. What about primes n!!+-1 ?

Once again simple code can show us some interesting mathematical facts, but don’t forget that it’s human brainpower that is still the clincher.

# n!! is n * n-2 * n-4 * ... * 1
# n!!+1 is rarely prime
# only for n=1,2,2596 and anything bigger than that
# gets pretty hard to test
# recursive fac function can't handle 2596
# iterative can
# prime test can't handle 2596!!+1 - way too big

def dubfac(a):
    if a==2:
        return 2
    elif a==1:
        return 1
    else:
        return a*dubfac(a-2)

def isprime(x):
    """naive primality test"""
    for a in range(2,int(x**0.5)+1):
        if x%a==0:
            return False
    return True

def dubfaciter(a):
    tot=1
    while a>0:
        tot*=a
        a-=2
    return tot

for a in range(1,25):
    z=dubfaciter(a)
    print(a,z,isprime(z+1),isprime(z-1))

There are a few of form n!!-1 but not many n!!+1.

According to my book n!!+1 is prime for n=1 and n=2, then for no other numbers until 2596. This is too big a number for a recursive double factorial function, so we use an iterative function, but even then the result is too big for the prime test function here too. Small moves Ellie.

Journeys In Spacetime

2009 August 19
by pythonisms

When they removed Einstein’s brain (don’t worry it was after his death) they found the region that usually takes the role of spatial reasoning was enlarged, actually it had crowded out part of the language region.

If you want to be like Einstein I suggest you get into visualising geometry and topology of all sorts. The easiest is to visualise a perfectly flat plane, with straight and curved journeys, triangles, squares and other shapes, but once you’ve done that you can move on up to subtler and more complex manifolds, like the sphere and then the torus, which is the shape of a ring doughnut. It’s got a hole.

Let’s allow the proportions of our torus to be about the same as the doughnut. Imagine you were an ant confined to crawl on the surface of this torus. In how many directions could you set out (following a straight line), such that you would eventually return to where you started ?

There are two immediately obvious ways to achieve a closed loop. Let’s call North a circle resting on the topmost bit, as if you had placed a doughnut on a flat table and rested a piece of paper on top. the bit where the paper contacts the torus is the circle that represents north, journeys north have to intersect this circle at a right angle. If you start on an outermost point of the torus and head due north then you will circumnavigate the body by travelling along the shorter circumference which means you will be going through the hole. If you move due east, which must be a line at right angles to north you will circumnavigate the torus in the longest circumference, and this means you will be circumnavigating the hole.

Setting out at an angle to the meridians is a little subtler. lets denote the angle from due north you set out in as arctan a. as long as a is a rational number then you will trace a curved helix and come back to your start eventually, although in many cases this will take several orbits around. If however a is irrational then you will never reach start exactly. Of course that means you will never return to the exact zero dimensional point of your initial position, although you may come arbitrarily near.

Contrast this with a sphere… if you are on a sphere it is apparent that every straight line journey will return you to start eventually.

But now lets move to an analogous case. We do the clever trick of trying to consider what a toroidal space of 4 dimensions might be like. In the same way as the ant is only able to move on the surface of the 3 torus, we will now be constrained to the 3 dimensional space that is the surface of the 4 torus. Although its hard or even impossible to visualise 4D objects, by using analogies we can still make some deductions.

I make it that journeys north will be journeys in a specific plane. in this case by analogy a direction that specified one of our neverending jourmeys would be given by two angles, one relative to the north plane and one to the east west. Journeys like this would be circumnavigating the hole. Here’s a problem for you, do the angles given by arctan a and arctan b need to have irrational a and irrational b or just one of the two for the neverending journey ? Its a good one for using a mixture of visualisation and reasoning by analogy.

Another intersting phenomenon is that if you pointed a sufficiently powerful telescope in one of these directions you might see the back of your own head ! When the apparent size of the back of your head is a maximum, you are looking north. Move away from north just a fraction and the size gets a lot smaller. An infinitesimal movement again and it might disappear, you’ve hit another irrational value of a. Of course if toroidal spacetime is like ours then infinitesimals are pretty hard to find, since even the planck length is infinitely huger. Math spaces are not exactly like physics spaces…

Are there possible directions in a closed 4d manifold such as our own spacetime may be where you will never return ? if the manifold is a 4 torus then by analogy this should be the case. If our spacetime is a 4 sphere then it seems this would not happen. every direction eventually returns you to start. Of course if the space is not topologicaly closed then you can go off forever, just as you could on a flat and open plane. I think closed universes are much more fun, and also they may avoid endless heat death and expansion where the average separation of all particles tends to infinity as time goes on to infinity. To me this is a depressing thought, I want a big crunch and a second chance at everything !

I am fascinated by the idea that a journey in a specific direction in a 4D toroidal universe would go on forever without ever returning to start, even though the actual size of the universe is finite. What an everlasting voyage of discovery this would be ! You would be circumnavigating your homespace endlessly on a different path each circuit, and this could go on forever. You would also be going round the hole forever too. It’s hard to see what this hole would be like. Maybe superadvanced aliens in a universe like that would learn to take hyperspatial shortcuts by jumping out of their 3d volume, crossing the hole in a straight line, and popping back in on the other side.

It’s also worth adding that if distances in any of these spaces have a minimum value, i.e. if space itself is quantised then there can be no endless journeys, since there are only a finite range of locations you can be in. Here’s another thought – what must the angles be to ensure you visit every possible location. This would be useful if you ran a tourist agency promising a “see absolutely everything” grand tour of your universe ! In our universe the planck length is the smallest distance we can make observations about, I think that means that space is effectively quantized. But doesn’t that mean that there can be no straight line journeys.? only zig zags as we pop into the nearest cell of space that touches our ideal straight line ? This reminds me of trying to make neat diagonals in a paint program, which is limited by the square grid of pixels on the screen !