Pi Day MMXVII

Every year on March the 14th, for one day the world gets irrationally excited about the famous constant \pi. As is tradition, you try to calculate \pi in unusual ways, demonstrating the constant’s ubiquity as it crops up in the most unexpected circumstances.

                        lnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn       
                    nJ$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$w       
                 `v$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$        
                n$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$z        
               u$$mnn:       Y$$$i        .@$$$$$,                    
              $$n           )$$$*         W$$$$$m                     
             -n[            $$$$.        ]$$$$$$                      
                           h$$$w         $$$$$$Y                      
                          [$$$$         X$$$$$$                       
                         "$$$$n        '$$$$$${                       
                        .$$$$$         8$$$$$$                        
                        *$$$$}        :$$$$$$+                        
                       #$$$$u         $$$$$$%                         
                     t$$$$$$         ;$$$$$$`                         
                    u$$$$$$!         $$$$$$W                          
                   Y$$$$$$M         .$$$$$$,                          
                 f$$$$$$$$.         Z$$$$$Z          nn               
               `w$$$$$$$$|          $$$$$$(         v$z               
              n$$$$$$$$$W           $$$$$$$1      'X$8                
             Y$$$$$$$$$$            *$$$$$$$8nnnn$$$p                 
             $$$$$$$$$@.             W$$$$$$$$$$$$$n                  
             _$$$$$$${                x$$$$$$$$$0>                    
                -n{.                     !|nt_.                       

A fairly well-known way to approximate pi is to randomly choose points in a square (often thought of as throwing darts at a square piece of cardboard), determine their distance to a circle’s center and do a division, as I did in my π Generator post.

However, \pi does not only appear in the formula for a circle’s area, A=\pi \cdot r^2, yet also in the formula for a sphere’s volume, V=\frac{4}{3} \cdot \pi \cdot r^3, and for all the infinite hyperspheres above dimension three (view this Wikipedia article for more about volumes of higher-dimensional spheres).

In particular, the formula for the hypervolume of a hypersphere in four dimensions is defined as being V=\frac{\pi^2}{2} \cdot r^4. Using this formula, my Python script randomly chooses four-dimensional points (each in the interval \left[0, 1\right)), calculates their distance to the point \left(0.5, 0.5, 0.5, 0.5\right) and determines if they are in the hypersphere around that point with radius 0.5.
By dividing the number of random points which lie in the hypersphere by the number of iterations used (10^6 in the example below), the script approximates the hypersphere’s hypervolume. By then rearranging the equation V=\frac{\pi^2}{2} \cdot r^4 with r=0.5 to \pi=\sqrt{V\cdot 32}, the desired constant can be approximated.

$ python pi.py
3.14196371717

# Python 2.7.7 Code
# Jonathan Frech, 13th of March 2017

Continue reading

Spinning Shapes

Drawing lines according to length and angle, which change over time, this program creates pretty shapes. The changing values for both length and angle are saved in the image’s name.

Inwards star Spinning square Doubled Origin logo


# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech  4th of December, 2015
#         edited  6th of December, 2015
#         edited 25th of December, 2015

Continue reading

Prime Spiral II

My first attempt at making a prime spiral worked, but it worked with 90° angles. Trying to make it look smoother, I now used an angle and a distance, drawing circles at calculated position.
If the number is prime, it gets white. If it is not, the circle will be gray.

Prime spiral


# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 18th of July, 2015

Continue reading

Sierpinski Triangle

Having recently heard something about chaos theory, I was interested in the sierpinski triangle. Specifically in the random generation. To proof, if it can really be generated using a dice, I wrote this program.

How it works

  • You start with any point inside the triangle (I chose the center)
  • A triangle has three points, let us call them A, B and C
  • You choose randomly between one of those three points, go half the distance and set a pixel right there
  • Repeating this process over and over again (always starting at your last point), you eventually get the sierpinski triangle

Controls

  • Space toggles if the triangle is generated
  • m toggles if the current position is shown (indicated by a blue circle)

Starting with an empty triangle Slowly but surely... Finished sierpinski triangle


# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 15th of July, 2015

Continue reading

Primes

Being fascinated with how prime spirals look, I tried another layout for primes. Starting at the upper left and writing out numbers like a normal text, starting at 0 and coloring every prime number red, every other number white, this is the result.
Scale: 50 Scale: 10 Scale: 5
It is interesting, that – like in prime spirals – the red squares form visible patterns. Randomly assigned squares would not as often form such patterns.


# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 27th of June, 2015
#         edited 28th of June, 2015

Continue reading

Boxes

Inspired by a pattern on a storage box I own I decided to try program something simulating it. This program shows variously colored boxes with a black gap in between. You can adjust the box size, the gap size and even how many boxes will appear on the screen (all changeable in the code).
The pattern will be newly generated every half a second.
Boxes 2 Boxes 3 Boxes 10 Boxes 11 Boxes 6


# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 28th of May, 2015

Continue reading

Prime Spiral

Being fascinated by prime numbers I wanted to create a prime number spiral generator myself. It just goes through the numbers from 0 to … and checks if they are prime. If they are prime, this rectangle in the spiral gets lit red, if it is not prime it gets lit dark gray. Because my algorithm for prime checking is very basic, I implemented a performance helper. That way the spiral’s creation will slow down after time (due to the numbers that need to be checked getting bigger and bigger). The really interesting thing is, that – although they may seem random – prime numbers often form lines if shown in such a spiral. To see the comparison I included an image of a randomly (50% to 50%) lit up board.
Prime Spiral (Bigger) Prime Spiral (stretched) Prime Spiral (bigger pixels) Random Spiral


# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 28th of May, 2015

Continue reading

Graph Sim

Thinking of graphs – especially bar graphs – as a pretty piece of art I wanted to create something related. But due to me not having any data I could graph, I decided to just use randomly generated data. It has no real purpose rather than look good.
The First Example The Second Example The Third Example

Some math

  • All of these calculations are based on random.randint() being perfectly random. Due to it being pseudo-random this will just be an approximation. To get further information, click on the equation to view them in Wolfram Alpha.
  • The chance of two adjacent bars having the same color
    • \Big(\big(\frac{1}{256}\big)^3\Big)^{2-1} = 5.96 \cdot 10^{-8} = \text{basically }0
  • The chance of two adjacent bars having the same color and size
  • (\text{max. size} = \text{window height} = 720)
    • \Big(\big(\frac{1}{256}\big)^3 \cdot \frac{1}{720}\Big)^{2-1} = 8.28 \cdot 10^{-11} = \text{basically }0
  • The chance of the screen not showing anything
  • (\text{which means every bar has the background color})
  • (\text{max. quantity of bars on screen} = \frac{\text{window height}}{\text{bar width}} = \frac{720}{4} = 180)
    • \Big(\big(\frac{1}{256}\big)^3 \Big)^{180} = 3.55 \cdot 10^{-1301} = \text{basically }0.0
  • Although these probabilities are very unlikely, they – theoretically – could occur.

# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 13th of May, 2015

Continue reading

φ Generator

This program generates φ, also called the golden ratio. It creates the fibonacci sequence \big\{1, 1, 2, 3, 5, 8, 13, \dots\big\} and divides the newly generated number by the last one. In theory this program would generate exactly φ.Terminal Output

Fibonacci sequence

\text{Start: } x_1 = 1 \text{ and } x_2 = 1
\text{Generation: } x_n = x_{n-1} + x_{n-2}

1 + 1 = 2
1 + 2 = 3
2 + 3 = 5
3 + 5 = 8
5 + 8 = 13
8 + 13 = \dots

The golden ratio (φ)

\phi \text{ is the ratio between } x_n \text{ and } x_{n-1}\text{.}
\phi = \frac{x_n}{x_{n-1}}


# Python 2.7.7 Code
# Jonathan Frech 6th of April, 2015

Continue reading

Bobbles

In this program you can spawn in circular entities. Their color is defined by their position and they will move randomly across the screen. You can also toggle if their spawn time (in ticks) is shown or not and if the screen gets refilled.

Usage

  • Left-clicking spawns in a new entity (at current mouse position)
  • ‘f’ toggles if the screen gets refilled (view example number 3 for reference)
  • ‘n’ toggles if numbers are shown

The First Example The Second Example (numbers shown) The Third Example (not refilling the screen)


# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 15th of March, 2015
#         edited  6th of April, 2015

Continue reading