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.BufferedReader;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.util.Arrays;import java.util.LinkedList;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.add(BorderLayout.CENTER, scrollPane);     //frame.pack();     frame.setLocation(0, 0);     frame.setSize(100, 100);     frame.setVisible(true);     frame.setAlwaysOnTop(true);     LinkedList<Integer> primesLL = new LinkedList<Integer>();     BufferedReader br = new BufferedReader(new FileReader(F:\\First100kPrimes.txt));     try      {         String line = br.readLine();         while (line != null)          {             primesLL.add(Integer.decode(line));             line = br.readLine();         }     }      finally      {         br.close();     }    int lines = 0;    while(lines < 1000)    {        int size = primesLL.size();        LinkedList<Integer> tmp = new LinkedList<Integer>();        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)                tmp.add(0);            else                tmp.add(Math.abs(primesLL.get(i) - primesLL.get(i-1)));        }        primesLL.clear();        primesLL.addAll(tmp);        lines++;        //try {Thread.sleep(20);}        //catch(InterruptedException ex) {Thread.currentThread().interrupt();}    }}}EDIT: New code I've tried to optimize a littlepublic 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.add(BorderLayout.CENTER, scrollPane);     //frame.pack();     frame.setLocation(0, 0);     frame.setSize(100, 100);     frame.setVisible(true);     frame.setAlwaysOnTop(true);     LinkedList<Integer> primesLL = new LinkedList<Integer>();     BufferedReader br = new BufferedReader(new FileReader(F:\\First100kPrimes.txt));     try      {         String line = br.readLine();         while (line != null)          {             primesLL.add(Integer.decode(line));             line = br.readLine();         }     }      finally      {         br.close();     }     int size = primesLL.size();     LinkedList<Integer> tmp = new LinkedList<Integer>();    for(int lines = 0; lines < 1000; lines++)    {        Arrays.toString(primesLL.toArray());        screen.append(Arrays.toString(primesLL.toArray()) + \n);        screen.setCaretPosition(screen.getText().length());        tmp.add(0);        for(int i = 1; i < size; i++)            tmp.add(Math.abs(primesLL.get(i) - primesLL.get(i-1)));        primesLL.clear();        primesLL.addAll(tmp);        tmp.clear();        //try {Thread.sleep(20);}        //catch(InterruptedException ex) {Thread.currentThread().interrupt();}    }}}
Sierpinski gasket - patterns from primes
java;optimization;primes;swing