I wrote a fairly simple program that works basically like this:
- Take a natural number (starting at 2) and test if it is prime
- If it is prime, switch direction (from up to down, or from down to up)
- Move up 1 up pixel if facing up, or move down 1 pixel if facing down
- Move 1 pixel to the right, and draw a point at the current position (move back to left side of the screen if it’s reached the right edge)
- Increment the integer
- Repeat (1-6)
Heres the program I used to run this:
//Note: this is written in processing, so it won't run as a normal java program
void setup() {
size(500,500);
}
long p= 1;
long hi = 0;
boolean up = true;
long average = 0;
void draw() {
frameRate(999);
stroke(255,0,0);
line(0,height/2,width,height/2);
for (int i = 0; 20000 > i; i++) {
stroke(p/300.%255);
point(p/300.0 % width,((hi)/100.)+width/2.0);
if(prime(p)) {
up = !up;
}
if (up) {
hi-- ;
}
else {
hi++;
}
average += (hi);
if (frameCount % 100 == 0) {
System.out.println("prime" + p);
System.out.println("average" + average);
}
p++;
}
}
class point {
int x = 0;
int y = 0;
}
boolean prime(long p) {
for (long i = 2; Math.sqrt(p) >= i; i++) {
if (p % i == 0) {
return false;
}
}
return true;
}
After running this program, it appears that there is a very significant bias towards the starting direction. Here is an image of running this program, all the way up about the integer 5000000. (The red is the center line, where it started)
So my question is, why do prime numbers have this strong bias towards one direction?
Edit:
Here are the first few points plotted:
2 is prime? —> True, Now direction is up, point at (1,1)
3 is prime? —> True, Now direction is down, point at (2,0)
4 is prime? —> False, Direction still down, point at (3,-1)
5 is prime? —> True, Now direction is up, point at (4,0)
6 is prime? —> False, Direction is still up, point at (5,1)
Small prime gaps, would mean it would change direction often.
2->down, 3 to up, 5 down, 7( same line as 3) up, 11 ( just 2 lines above 5) down, 13( same line as 5) up, 17(just 2 lines above 11) down, 19 (same line as 11) up, 23 down, 29 ( same line as 13) up, 31 ( same line as 11,19) down, 37 ( first time below center -1) up, 41 ( same line as 5,13)
only a larger gap facing down could possibly go below center the more long gaps up the less shorter gaps can eat away at that safety net.
Bertrands postulate guarantees us, we won't go more than $p-2$ below the center by the time we hit $2p$ because there's always a prime between $p$ and $2p-2$
You'll also note that unless you go back toward the left, you'll run off your screen edge...
same with height unless you have an infinite screen...
Largest gap is only 154, but gap of 6 is more common showing up 54545 times ( about 15.65%), with a gap of 12 being second most common. so a lot of overlapping lines.