浏览 5682 次
锁定老帖子 主题:判断质数以及求某数以内的质数
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2007-11-26
xml 代码
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2008-01-21
请使用线性筛法
|
|
返回顶楼 | |
发表时间:2008-01-22
package com.maodajun.javaeye5; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class SingleWord { List list = new ArrayList(); /** * 将list中的数据求合 * @return */ public int addAll(){ int all = 0 ; String temp = null; for(Iterator it = list.iterator();it.hasNext();){ temp = (String) it.next(); all += Integer.parseInt(temp); } return all; } /** * @deprecated * 已过时方法 * 用于判断是否是质数 * @param str */ public void isSingle(String str){ int tmp = Integer.parseInt(str); if(tmp<2){ return; } for(int i = 2 ; Math.sqrt(tmp) >=i ; i ++ ){ if(tmp%i==0){ return; } } list.add(str); } /** * 用来找到所有小于str的质数方法 * */ public void findAllSingle(String str){ int tmp = Integer.parseInt(str); for(int i = 0 ; tmp >i ;i ++){ isOldSingle(""+i); } } /** * 对输入的数值与已有的数值对比 * 看可否被整除. * * @param str */ public void isOldSingle(String str){ int tmp = Integer.parseInt(str); if(tmp<2){ return; }else{ for(int i = 0 ; list.size()>i ;i++ ){ int single =Integer.parseInt(list.get(i).toString()); if(tmp%single ==0){ return; } } } list.add(str); } public List getList() { return list; } public void setList(List list) { this.list = list; } } package com.maodajun.javaeye5; import java.util.ArrayList; import java.util.List; import junit.framework.TestCase; public class SingleWordTest extends TestCase { public static void main(String[] args) { junit.textui.TestRunner.run(SingleWordTest.class); } public SingleWordTest(String arg0) { super(arg0); } protected void setUp() throws Exception { super.setUp(); } protected void tearDown() throws Exception { super.tearDown(); } /* * Test method for 'com.maodajun.javaeye5.SingleWord.addAll()' */ public void testAddAll() { List list = new ArrayList(); list.add("1"); list.add("2"); SingleWord singleword = new SingleWord(); singleword.setList(list); int i = singleword.addAll(); assertEquals(3,i); } public void testIsSingle(){ SingleWord singleword = new SingleWord(); singleword.isSingle("0"); assertEquals(0,singleword.getList().size()); singleword.isSingle("1"); assertEquals(0,singleword.getList().size()); singleword.isSingle("4"); assertEquals(0,singleword.getList().size()); singleword.isSingle("2"); assertEquals(1,singleword.getList().size()); singleword.isSingle("3"); assertEquals(2,singleword.getList().size()); singleword.isSingle("13"); assertEquals(3,singleword.getList().size()); singleword.isSingle("25"); assertEquals(3,singleword.getList().size()); } public void testIsOldSingle(){ SingleWord singleword = new SingleWord(); singleword.isOldSingle("0"); assertEquals(0,singleword.getList().size()); singleword.isOldSingle("1"); assertEquals(0,singleword.getList().size()); singleword.isOldSingle("2"); assertEquals(1,singleword.getList().size()); singleword.isOldSingle("3"); assertEquals(2,singleword.getList().size()); singleword.isOldSingle("4"); assertEquals(2,singleword.getList().size()); singleword.isOldSingle("5"); assertEquals(3,singleword.getList().size()); singleword.isOldSingle("6"); assertEquals(3,singleword.getList().size()); } public void testFindAllSingle(){ SingleWord singleword = new SingleWord(); singleword.findAllSingle("10"); assertEquals(17,singleword.addAll()); singleword .setList(new ArrayList()); singleword.findAllSingle("20"); assertEquals(77,singleword.addAll()); } } |
|
返回顶楼 | |
发表时间:2008-01-26
来个Erlang版本的: -module(test). -export([main/1]). main(N) -> L = calc(N, []), S = lists:foldl(fun(X, Total) -> Total+X end, 0, L), io:format("质数:~p~n", [L]), io:format("总和:~p~n", [S]). %统计某个数以内的所有质数 calc(1, L) -> L; calc(N, L) -> case is_single(N) of false -> calc(N-1, L); true -> calc(N-1, [N|L]) end. %判断是否是质数 is_single(N) -> is_single(N, N-1). is_single(_N, 1) -> true; is_single(N, D) when N rem D ==0 -> false; is_single(N, D) -> is_single(N, D-1).
|
|
返回顶楼 | |