きったんの頭

素数を求める Java Applet

n 番目の素数を計算 (n <= 100000)

// <applet code="PrimeApp.class" width="177" height="77"></applet>

/**
 * 素数を求める Java Applet
 * @see https://mind.kittttttan.info/math/primeapp.html
 */

import java.applet.Applet;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;

import java.awt.Container;
import java.awt.BorderLayout;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class PrimeApp extends Applet implements ActionListener {
  private final static int MAX = 100000;
  private Container contentPane;
  private JPanel panel1;
  private JPanel panel2;
  private JButton btn;
  private JLabel labelp;
  private JLabel labelt;
  private JTextField tf;

  /**
   * Initialize
   */
  public void init() {
    btn = new JButton("OK");
    btn.addActionListener(this);

    tf = new JTextField("7", 7);
    //tf.setToolTipText("Input Integer");

    labelp = new JLabel("Prime Number");
    labelt = new JLabel("Time[ms]");

    panel1 = new JPanel();
    panel1.add(tf);
    panel1.add(btn);

    panel2 = new JPanel();
    panel2.add(labelp);
    panel2.add(labelt);

    add(panel1, BorderLayout.CENTER);
    add(panel2, BorderLayout.SOUTH);
  }

  /**
   * Called when button pushed
   */
  public void actionPerformed(ActionEvent e) {
    try {
      long start = System.currentTimeMillis();
      String s = tf.getText();
      int n = Integer.parseInt(s);
      if (n > MAX) {
        labelp.setText("too BIG");
        labelt.setText("Error");
        return;
      }
      //int p = getPrime(n);
      int p = sieve(n);
      long stop = System.currentTimeMillis();
      String ps = String.valueOf(p);
      String t = String.valueOf(stop - start) + "ms";
      labelp.setText(ps);
      labelt.setText(t);
    } catch (NumberFormatException err) {
      labelp.setText("Input 1, 2, ...");
      labelt.setText("Error");
    } catch (ArrayIndexOutOfBoundsException err) {
      labelp.setText("ArrayIndexOut");
      labelt.setText("Error");
    }
  }

  /**
   * Get <var>n</var>th prime number
   * @param n
   * @return prime number
   */
  public static int getPrime(int n){
    if (n < 1) {
      return -1;
    }
    if (n < 2) {
      return 2;
    }
    int[] primes = new int[n];
    int index = 2, i, j, pj;
    primes[0] = 2;
    primes[1] = 3;
    boolean addflag;
    for (i = 5; index < n; i+=2) {
      addflag = true;
      for (j = 1; (pj = primes[j]) * pj <= i; j++) {
        if (i % pj == 0) {
          addflag = false;
          break;
        }
      }
      if (addflag) {
        primes[index++] = i;
      }
    }
    return primes[index - 1];  
  }

  /**
   * Get <var>n</var>th prime number
   * @param n
   * @return prime number
   */
  public static int sieve(int n) {
    if (n < 1) {return 0;}
    double times;
    if (n < 50) {times = 5;}
    else {times = Math.log(n) + 2;}
    int limit = (int)Math.floor(n*times);
    
    int sq = (int)Math.sqrt(limit);
    boolean[] s = new boolean[limit+1];
    int i, j;
    s[0] = false;
    s[1] = false;
    for (i = 2; i < limit+1; i++) {
      s[i] = true;
    }
    for (i = 2; i < sq+1; i++) {
      if (s[i]) {
        for (j = i*i; j < limit+1; j+=i) {
          s[j] = false;
        }
      }
    }
    int[] primes = new int[n];
    j = 0;
    for (i = 0; j < n; i++) {
      if (s[i]) {primes[j++] = i;}
    }
    return primes[j - 1];  
  }
}