フィボナッチ数の計算など
package ktn.gui; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.math.BigInteger; import javax.swing.JTextArea; import javax.swing.JScrollPane; import java.awt.BorderLayout; import javax.swing.JPanel; import javax.swing.BoxLayout; import javax.swing.JLabel; import java.awt.Component; import javax.swing.SwingConstants; import javax.swing.JComboBox; import javax.swing.DefaultComboBoxModel; import ktn.Big; import ktn.Prime; public class Win { private static enum ExeType { FIBONACCI, FACTORIAL, PRIME }; private JFrame frmKtn; private JTextField textFieldInput; private JTextArea textArea; private JScrollPane scrollPane; private JPanel panel; private JLabel lblMs; private JComboBoxcomboBox; private JPanel panel_1; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Win window = new Win(); window.frmKtn.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public Win() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { frmKtn = new JFrame(); frmKtn.setTitle("ktn"); frmKtn.setBounds(100, 100, 264, 260); frmKtn.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frmKtn.getContentPane().setLayout(new BorderLayout(0, 0)); textArea = new JTextArea(); textArea.setLineWrap(true); textArea.setEditable(false); textArea.setRows(8); textArea.setColumns(10); scrollPane = new JScrollPane(textArea); frmKtn.getContentPane().add(scrollPane); panel = new JPanel(); frmKtn.getContentPane().add(panel, BorderLayout.NORTH); panel.setLayout(new BorderLayout(0, 0)); comboBox = new JComboBox (); comboBox.setModel(new DefaultComboBoxModel (ExeType.values())); comboBox.setSelectedIndex(0); panel.add(comboBox, BorderLayout.NORTH); panel_1 = new JPanel(); panel.add(panel_1); panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.X_AXIS)); textFieldInput = new JTextField(); textFieldInput.setText("77"); textFieldInput.setHorizontalAlignment(SwingConstants.RIGHT); panel_1.add(textFieldInput); textFieldInput.setColumns(20); JButton btnExe = new JButton("Exe"); panel_1.add(btnExe); btnExe.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { execute(event); } }); lblMs = new JLabel("ready"); frmKtn.getContentPane().add(lblMs, BorderLayout.SOUTH); lblMs.setHorizontalAlignment(SwingConstants.RIGHT); lblMs.setAlignmentX(Component.RIGHT_ALIGNMENT); } /** * [Exe] * @param event */ private void execute(ActionEvent event) { try { String res; final String input = textFieldInput.getText(); final int n = Integer.parseInt(input); final ExeType type = (ExeType) comboBox.getSelectedItem(); final long start = System.currentTimeMillis(); switch (type) { case FIBONACCI: { final BigInteger bi = Big.fib(n); res = order(input) +" fibonacci number is\n"+ bi.toString(); } break; case FACTORIAL: { final BigInteger bi = Big.fact(n); res = input +"! =\n"+ bi.toString(); } break; case PRIME: { final int p = Prime.sieve(n); res = order(input) +" prime number is\n"+ (p > 0 ? p : "undefined"); } break; default: res = "Invalid Type: "+ type; break; } final long stop = System.currentTimeMillis(); textArea.setText(res); lblMs.setText((stop - start) + "ms"); } catch (Exception ex) { textArea.setText(ex.toString()); } } private static String order(String order) { if (order == null) { return null; } final int length = order.length(); if (length < 1) { return ""; } String suffix; final char first = order.charAt(0); if (length == 2 && first == '1') { suffix = "th"; } else { final char last = order.charAt(length - 1); if (last == '1') { suffix = "st"; } else if (last == '2') { suffix = "nd"; } else if (last == '3') { suffix = "rd"; } else { suffix = "th"; } } return order + suffix; } }
package ktn; import java.math.BigInteger; public class Big { /** ** |a b| |d e| |ad+be bd+ce| * | | x | | = | | * |b c| |e f| |bd+ce be+cf| ** * @param a * @param b * @return */ private static BigInteger[] utm2mul(BigInteger[] a, BigInteger[] b) { final BigInteger ad = a[0].multiply(b[0]); final BigInteger be = a[1].multiply(b[1]); final BigInteger bd = a[1].multiply(b[0]); final BigInteger ce = a[2].multiply(b[1]); final BigInteger bdce = bd.add(ce); final BigInteger cf = a[2].multiply(b[2]); final BigInteger[] m = { ad.add(be), bdce, be.add(cf) }; return m; } /** * fibonacci * ** |1 1|^n |fib(n+1) fib(n) | * | | = | | * |1 0| |fib(n) fib(n-1)| ** * @param n * @return nth fibonacci */ public static BigInteger fib(int n) { if (n-- > 0) { if (n-- == 1) { return BigInteger.ONE; } BigInteger[] m0 = { BigInteger.ONE, BigInteger.ONE, BigInteger.ZERO }; BigInteger[] m = { BigInteger.ONE, BigInteger.ONE, BigInteger.ZERO }; for (; n > 0; n >>>= 1, m0 = utm2mul(m0, m0)) { if ((n & 1) == 1) { m = utm2mul(m, m0); } } return m[0]; } return BigInteger.ZERO; } public static BigInteger fact(int n) { BigInteger b = BigInteger.ONE; for (int i = 2; i < n + 1; ++i) { b = b.multiply(BigInteger.valueOf(i)); } return b; } }
package ktn; public class Prime { /** * Get nth prime number * * @param n * @return prime number */ public static int getPrime(int n) { if (n < 1) { return 0; } if (n < 2) { return 2; } int[] primes = new int[n]; primes[0] = 2; primes[1] = 3; int index = 2, i, j, pj; 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 nth 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; } final int limit = (int) Math.floor(n * times); final int sq = (int) Math.sqrt(limit); boolean[] s = new boolean[limit + 1]; s[0] = false; s[1] = false; int i, j; 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]; } }