100000以内素数java实验结果截图_生成100000以内的素数XML文件的Demo(Jdom写成)

搜索热词

public class TestPrimeXml {

private static String fileName = "Primes.xml";

//获取源文件路径

private static String path = System.getProperty("user.dir") + File.separator + "src" + File.separator + fileName;

public static void main(String[] args) {

//创建PrimeXML文档

creatPrimeXML(100000);

//输入一个数字,检验是否为质数,如果是则返回相应的下标位置,如果不是则打印其为合数

checkNum(73897);

Add();

}

//添加一些片段

private static void Add() {

try {

//创建解析器

SAXBuilder sb = new SAXBuilder();

//获取要解析的目标文件

File file = new File(path);

//解析文件获取document

Document document = sb.build(file);

Element rooteElement = document.getRootElement();

//获取子节点集合

List eList = rooteElement.getChildren();

for (Element element : eList) {

element.setAttribute("name","wanglf");

}

rooteElement.setAttribute("name","wanglf");

saveDocument(document);

} catch (Exception e) {

e.printStackTrace();

}

}

@SuppressWarnings("unchecked")

private static void checkNum(int i) {

if (!isPrimeBaidu(i)) {

System.out.println(i + "是合数");

} else {

try {

//创建解析器

SAXBuilder sb = new SAXBuilder();

//获取要解析的目标文件

File file = new File(path);

//解析文件获取document

Document document = sb.build(file);

//获取文档根节点

Element rootElement = document.getRootElement();

//获取指定节点的所有子节点

List eList = rootElement.getChildren();

for (Element element : eList) {

if (element.getText().equals(i + "")) {

System.out.println(i + "所在的id为:" + element.getAttributeValue("id"));

System.exit(0);

}

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

public static void creatPrimeXML(Integer s) {

try {

long f1 = System.nanoTime();

Document document = getDocument(s);

saveDocument(document);

long f2 = System.nanoTime();

//计算运行时间

System.out.println("总共用时" + (f2 - f1) / 10e8 + "秒");

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

}

private static void saveDocument(Document document) throws Exception{

//格式化

Format format = Format.getPrettyFormat();

//输出文本

XMLOutputter out = new XMLOutputter(format);

out.output(document,new FileOutputStream(path));

}

/**

*

* @param n

* @return Document

*/

private static Document getDocument(Integer n) {

Element rootElement = new Element("primes");

int s = 1;

for (int i = 2; i <= n; i++) {

if (isPrimeBaidu(i)) {

Element element = new Element("prime");

element.setAttribute("id",s + "");

element.setText(i + "");

rootElement.addContent(element);

s++;

}

}

Document document = new Document(rootElement);

return document;

}

/**

* 一个很优秀的素数计算方法

* @param n

* @return boolean

*/

public static boolean isPrimeBaidu(long n) {

if (n <= 3) {

return n > 1;

} else if (n % 2 == 0 || n % 3 == 0) {

return false;

} else {

for (int i = 5; i * i <= n; i += 6) {

if (n % i == 0 || n % (i + 2) == 0) {

return false;

}

}

return true;

}

}

}

相关文章

总结

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

如您喜欢交流学习经验,点击链接加入交流1群:1065694478(已满)交流2群:163560250

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值