Tuesday, December 27, 2011

3D Pythagoras Tree

Hi :). Inspired by the Pythagoras tree, I created these fractals. They are not really 3D Pythagoras trees because the triangles used aren't right triangles. But it looks somewhat like a Pythagoras three anyway. 




The tree below is of order 10 and is an extension of the first tree.




These trees are created in Blender 2.49 using a Python script. Luxrender is used to render the trees.

Magic Prime Square

This is my first post, and it is about magic prime squares. For those who don't know; a (normal) magic square of order n is an n × n matrix, where the numbers in each row and column add up to the same number. An example of a magic square of order 3 is shown below.






Magic prime squares
Some time ago I was wondering whether it is possible to make a magic square with the property that every row and every column of the square forms a prime number. This means that the number formed by the digits of each row and column must be prime in both directions. For example, in a 3 × 3 matrix the row 1 1 3 forms the number 113 in one direction and 311 in the other, which are both prime. 


These squares turned out to exist. Three magic prime squares of order 5 are shown below.




The 'best' magic prime square I found is of order 7 and sum 17. It is a perfect magic prime square; the diagonals are also prime (in both directions) and the sum of the digits in the diagonals also add up to 17.


Order 7 perfect magic prime square.


So far, I have found all magic prime squares of order 5 and 6. There are 414 magic prime squares of order 5 and 28470 of order 6.


Construction
I have written a program in c++ to find these squares. Even though computers are very fast, it is still hard to find magic squares because of the huge number of possible squares. For example, the number of possible 5 × 5 squares is 1025, simply too much to check one by one.


There are, however, a number of ways to reduce the number of squares you have to check (by check I mean determine whether a given square is a magic prime square). The first thing I noticed about prime squares is that the first and last row of a square never contains the digits 0, 2, 4, 5, 6 and 8. This is because a prime number can never end in these digits. A number ending in 0, 2, 4, 6 or 8 is always evens (divisible by 2) and can therefore never be a prime number (except the number 2, the only even prime number). A number ending in 5 is always divisible by 5 and can also, except for the number 5, never be prime. Because each column of a magic prime square must be prime in bot directions, not only the last digit has these constraints, but also the first digit. This information greatly reduces the number of prime numbers allowed in the first and last row. Implementing this in my program made it about ten times faster (the speed gain depends on and increases with the order of the magic prime square).


Here are some basic steps to find magic prime squares of order n and magic sum s:

  • Determine all double prime numbers (primes who are still prime when you reverse the digits) consisting of n digits and filter out the numbers for which the digits add up to s. Put these numbers in a list L.
  • Make another list, consisting of all the prime numbers in L that do not contain the digits 0, 2, 4, 5, 6 and 8. Call this list L'.
  • Now you can start creating (potentially magic prime) squares. Put the first number of list L' in the first and last row, and the first number of L in the rows in between. Then check whether this square is magic and prime. To go to the next square, put the second number of list L' in the last row. After you put the last number from L' in the last row, change the number in the row above to the next number in L and set the last row back to the first number of L'. 

In this way, you will find all magic prime squares.