素数遍历方法比较(Find the prime number, with performance monitoring function)
来源:互联网 发布:sql round 编辑:程序博客网 时间:2024/05/17 02:48
//Find the prime number, with performance monitoring function
//Java how to program, 5/e, Exercise 6.26
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class IsPrime extends JApplet implements ActionListener {
int MAX;
JLabel MAXLabel;
JTextField MAXField;
JTextArea output1,output2;
JScrollPane scroller1,scroller2;
public void init()
{
Container container=getContentPane();
container.setLayout(new FlowLayout());
MAXLabel= new JLabel("Enter the search range (maximum):");
container.add(MAXLabel);
MAXField=new JTextField(10);
container.add(MAXField);
MAXField.addActionListener(this);
output1=new JTextArea(10,17);
scroller1=new JScrollPane(output1);
container.add(scroller1);
output2=new JTextArea(10,17);
scroller2=new JScrollPane(output2);
container.add(scroller2);
}
public void actionPerformed (ActionEvent event)
{
int counter=0;
MAX=Integer.parseInt(MAXField.getText());
long t1=System.currentTimeMillis();
for(int i=2;i<=MAX;i++)
{
if (IsPrimeMethod1(i))
{output1.append(i+" ");
counter++;
if (counter%5==0)
output1.append("/n");
}
}
long t2=System.currentTimeMillis();
output1.append("/n"+"Time taken in milliseconds is: "+Long.toString(t2-t1));
counter=0;
t1=System.currentTimeMillis();
for(int i=2;i<=MAX;i++)
{
if (IsPrimeMethod2(i))
{output2.append(i+" ");
counter++;
if (counter%5==0)
output2.append("/n");
}
}
t2=System.currentTimeMillis();
output2.append("/n"+"Time taken in milliseconds is: "+Long.toString(t2-t1));
}
public boolean IsPrimeMethod1(int argument)
{
int flag=0;
for (int i=2;i<=Math.sqrt(argument);i++)
{
if (argument%i==0)
{flag=1;break;}
}
return flag==0;
}
public boolean IsPrimeMethod2(int argument)
{
int flag=0;
for (int i=2;i<argument;i++)
{
if (argument%i==0)
{flag=1;break;}
}
return flag==0;
}
}
//Java how to program, 5/e, Exercise 6.26
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class IsPrime extends JApplet implements ActionListener {
int MAX;
JLabel MAXLabel;
JTextField MAXField;
JTextArea output1,output2;
JScrollPane scroller1,scroller2;
public void init()
{
Container container=getContentPane();
container.setLayout(new FlowLayout());
MAXLabel= new JLabel("Enter the search range (maximum):");
container.add(MAXLabel);
MAXField=new JTextField(10);
container.add(MAXField);
MAXField.addActionListener(this);
output1=new JTextArea(10,17);
scroller1=new JScrollPane(output1);
container.add(scroller1);
output2=new JTextArea(10,17);
scroller2=new JScrollPane(output2);
container.add(scroller2);
}
public void actionPerformed (ActionEvent event)
{
int counter=0;
MAX=Integer.parseInt(MAXField.getText());
long t1=System.currentTimeMillis();
for(int i=2;i<=MAX;i++)
{
if (IsPrimeMethod1(i))
{output1.append(i+" ");
counter++;
if (counter%5==0)
output1.append("/n");
}
}
long t2=System.currentTimeMillis();
output1.append("/n"+"Time taken in milliseconds is: "+Long.toString(t2-t1));
counter=0;
t1=System.currentTimeMillis();
for(int i=2;i<=MAX;i++)
{
if (IsPrimeMethod2(i))
{output2.append(i+" ");
counter++;
if (counter%5==0)
output2.append("/n");
}
}
t2=System.currentTimeMillis();
output2.append("/n"+"Time taken in milliseconds is: "+Long.toString(t2-t1));
}
public boolean IsPrimeMethod1(int argument)
{
int flag=0;
for (int i=2;i<=Math.sqrt(argument);i++)
{
if (argument%i==0)
{flag=1;break;}
}
return flag==0;
}
public boolean IsPrimeMethod2(int argument)
{
int flag=0;
for (int i=2;i<argument;i++)
{
if (argument%i==0)
{flag=1;break;}
}
return flag==0;
}
}
- 素数遍历方法比较(Find the prime number, with performance monitoring function)
- Find the Prime Number
- NOJ--- Find the Prime Number Again(1155)
- 判素数(Prime number)
- find prime number
- Google Treasure Hunt 2008---Find the Smallest Prime Number
- Find all the prime number between 1 and 100
- Number of prime number(打印素数表)
- 求质数(Prime Number 素数)的方法——厄拉多塞筛法
- 求质数(Prime Number 素数)的方法——厄拉多塞筛法
- 如何判断一个数是不是素数(prime number) 方法
- prime number n.质数,素数
- 素数表(Prime number table)
- Linux Performance Monitoring with Vmstat and Iostat Commands
- Linux Performance Monitoring with Vmstat and Iostat Commands
- CodeForces - 27E Number With The Given Amount Of Divisors (反素数入门)
- [CodeForces 27E] Number With The Given Amount Of Divisors (数论 + 反素数)
- zoj2562 More Divisors && CF27E. Number With The Given Amount Of Divisors(反素数)
- 8皇后(tiger语言描述)
- rs.open 与conn.execute()的区别
- 新版本又开始了......
- 疯狂的程序员38
- HTTP method GET is not supported by this URL
- 素数遍历方法比较(Find the prime number, with performance monitoring function)
- 第二回.初窥CF类型加载器
- 发布一个记账软件---流水记账
- 我理解的IoC和AOP
- 最近的迷茫。
- 24款实用的翻页页码css代码
- 加密解密存储过程
- 支持闰月的js农历日历代码
- vs.net2005新建网站和新建WEB应用程序的区别