3
\$\begingroup\$

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 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.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();}
    }

}

}
\$\endgroup\$
2
\$\begingroup\$

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.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
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);

     ArrayList<Integer> primesLL = new ArrayList<Integer>();
     BufferedReader br = new BufferedReader(new FileReader("a.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 = size-1; i > 0; i--)
        {
            primesLL.set(i, Math.abs(primesLL.get(i) - primesLL.get(i-1)));
        }
        primesLL.set(0,0);
     //   primesLL.clear();
    //    primesLL.addAll(tmp);
        lines++;

        //try {Thread.sleep(20);}
        //catch(InterruptedException ex) {Thread.currentThread().interrupt();}
    }

}

}
\$\endgroup\$
  • \$\begingroup\$ [primesLL.set(i, Math.abs(primesLL.get(i) - primesLL.get(i-1)));] This will cause an issue, on the first iteration you set element i to ([i]-[i-1]), then on the next iteration you set [i+1] = [i+1] - [i], and on our last iteration we changed [i], so [i+1] will be wrong. \$\endgroup\$ – KDecker Apr 25 '13 at 14:24
  • \$\begingroup\$ No, that's why we are going backwards (take a look closely). you will set i with ([i]-[i-1]) and you no longer need the content of i, so the next iteration you set i-1 with [i-1]-[i-2] and you no longer need contents of i-1 and so on \$\endgroup\$ – faisal Apr 25 '13 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.