Guest User

Untitled

a guest
Jun 25th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.51 KB | None | 0 0
  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3.  
  4. import javax.swing.JButton;
  5. import javax.swing.JFrame;
  6. import javax.swing.JOptionPane;
  7. import javax.swing.UIManager;
  8.  
  9.  
  10. public class PrimerNumber {
  11.  
  12.     private static final long[] primes = new long[61521918];//new long[(int) (Runtime.getRuntime().maxMemory()/11)];
  13.     static {
  14.         System.out.println(primes.length);
  15.         primes[0] = 2;
  16.         primes[1] = 3;
  17.         primes[2] = 5;
  18.         //these are known and needed in the beginning
  19.     }
  20.    
  21.     private static int idx = 3; //already assigned 3 primes
  22.  
  23.     public static void main(String[] args){
  24.         Runtime.getRuntime().addShutdownHook(new Thread(){ //to be run when program exits
  25.             public void run(){
  26.                 System.out.print("\nCalculated " + idx + " primes\n" + idx + "th prime: " + primes[idx - 1]);
  27.             }
  28.         });
  29.         try {
  30.             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); //set UI to Windows/Mac/Linux (whatever native UI is)
  31.         } catch (Exception useless){}
  32.         final JFrame frame = new JFrame();//frame for gui
  33.         frame.setUndecorated(true);//no buttons in pane
  34.         final JButton button = new JButton("Exit");
  35.         button.addActionListener(new ActionListener(){//when Exit button clicked
  36.             public void actionPerformed(ActionEvent e) {
  37.                 frame.setVisible(false);
  38.                 frame.dispose();
  39.                 JOptionPane.showMessageDialog(null, "\nCalculated " + idx + " primes\n" + idx + "th prime: " + primes[idx - 1]);
  40.                 System.exit(0);
  41.             }
  42.         });
  43.         frame.add(button);
  44.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  45.         frame.setSize(500, 500);
  46.         frame.pack();
  47.         frame.setLocationRelativeTo(null);//center frame
  48.         frame.setVisible(true);
  49.         while(frame.isVisible()){
  50.             System.out.println(calculateNextPrime());
  51.         }
  52.  
  53.     }
  54.  
  55.     private static long calculateNextPrime(){
  56.         if(idx == primes.length){
  57.             System.out.println("\nCannot calculate more prime numbers (reached max memory usage).");
  58.             System.out.print("Last prime calculated: ");
  59.             System.exit(0);
  60.             return primes[idx - 1];
  61.         }
  62.         primeSearch:
  63.             for(long i = primes[idx - 1] + 2;; i += 2){ //use last prime as a starting point, but add 2 so it stays odd
  64.                 for(int j = 0; j < idx; j++){
  65.                     if(i % primes[j] == 0){ //check if any of the previous primes are a factor of current number
  66.                         //if no previous primes are a factor, then current number is a prime
  67.                         continue primeSearch;
  68.                     }
  69.                 }
  70.                 //if execution reaches here, no previous are a factor, so set the next prime number and increase index
  71.                 return primes[idx++] = i;
  72.             }
  73.     }
  74.  
  75. }
Add Comment
Please, Sign In to add comment