# Sierpinski gasket - patterns from primes

Below is the code I have written. I started yesterday with prime numbers and playing around with them. What the code does it determines the "prime gap" between primes numbers, prints them, then finds the gap between these numbers, prints, etc. It starts to create a very cool pattern you can find some maths about it here and see the pattern that it produces.

Anyways, I was wondering if there is a way to speed this puppy up!? I am not very good with optimization, this is probably my first Java optimization I've ever needed/wanted to do.

Originally I just hardcoded the first 150 primes into the program. Now I read the first 71k from a text file. I have been letting the implementation below run for about an hour and it has gotten to line 17.

The future of this I want to make a visualizer for this pattern. And figure out a way to scroll the TextArea over so it looks at the diagonal where the differences between numbers starts to breakdown to the pattern. If that made sense, probably not. Not much I say does.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class PrimeSandbox
{

public static void main(String[] args) throws IOException
{
JTextArea screen = new JTextArea(5, 20);
Font font = new Font("Times New Roman", Font.BOLD, 8);
screen.setFont(font);
screen.setForeground(Color.BLUE);
JScrollPane scrollPane = new JScrollPane(screen);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
//frame.pack();
frame.setLocation(0, 0);
frame.setSize(100, 100);
frame.setVisible(true);
frame.setAlwaysOnTop(true);

try
{
while (line != null)
{
}
}
finally
{
br.close();
}

int lines = 0;
while(lines < 1000)
{
int size = primesLL.size();

for(int i = 0; i < size; i++)
screen.append(" " + primesLL.get(i));
screen.append("\n");
screen.setCaretPosition(screen.getText().length());

for(int i = 0; i < size; i++)
{
if(i == 0)
else
}
primesLL.clear();
lines++;

}

}

}


EDIT: New code I've tried to optimize a little

public class PrimeSandbox
{

public static void main(String[] args) throws IOException
{
JTextArea screen = new JTextArea(5, 20);
Font font = new Font("Times New Roman", Font.BOLD, 8);
screen.setFont(font);
screen.setForeground(Color.BLUE);
JScrollPane scrollPane = new JScrollPane(screen);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
//frame.pack();
frame.setLocation(0, 0);
frame.setSize(100, 100);
frame.setVisible(true);
frame.setAlwaysOnTop(true);

try
{
while (line != null)
{
}
}
finally
{
br.close();
}

int size = primesLL.size();
for(int lines = 0; lines < 1000; lines++)
{
Arrays.toString(primesLL.toArray());
screen.append(Arrays.toString(primesLL.toArray()) + "\n");
screen.setCaretPosition(screen.getText().length());

for(int i = 1; i < size; i++)

primesLL.clear();
tmp.clear();

}

}

}


Ok, so you do not need temporary List. You can change the code to reuse the same List, primesLL. Also I have changed the List to arrayList, since accessing elements by index is costly for LinkedList. Try this and see if it improves.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class PrimeSandbox
{

public static void main(String[] args) throws IOException
{
JTextArea screen = new JTextArea(5, 20);
Font font = new Font("Times New Roman", Font.BOLD, 8);
screen.setFont(font);
screen.setForeground(Color.BLUE);
JScrollPane scrollPane = new JScrollPane(screen);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
//frame.pack();
frame.setLocation(0, 0);
frame.setSize(100, 100);
frame.setVisible(true);
frame.setAlwaysOnTop(true);

ArrayList<Integer> primesLL = new ArrayList<Integer>();
try
{
while (line != null)
{
}
}
finally
{
br.close();
}

int lines = 0;
while(lines < 1000)
{
int size = primesLL.size();

for(int i = 0; i < size; i++)
screen.append(" " + primesLL.get(i));
screen.append("\n");
screen.setCaretPosition(screen.getText().length());

for(int i = size-1; i > 0; i--)
{
primesLL.set(i, Math.abs(primesLL.get(i) - primesLL.get(i-1)));
}
primesLL.set(0,0);
//   primesLL.clear();