`
to_zoe_yang
  • 浏览: 144493 次
  • 性别: Icon_minigender_2
  • 来自: 01
社区版块
存档分类
最新评论

[Sieve of Eratosthenes]埃拉托色尼素数筛选法

阅读更多


In mathematics, the Sieve of Eratosthenes (Greek: κόσκινον Ἐρατοσθένους) is a simple, ancient algorithm for finding all prime numbers up to a specified integer.[1] It works efficiently for the smaller primes (below 10 million).[2] It was created by Eratosthenes, an ancient Greek mathematician. However, none of his mathematical works survived—the sieve was described and attributed to Eratosthenes in the Introduction to Arithmetic by Nicomachus.

A prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself.

[素数的定义是,一个仅能被1和本身整除的自然数]

To find all the prime numbers less than or equal to a given integer n by Eratosthenes' method:

[可以通过以下的埃拉托色尼筛选法寻求所有小于整数n的素数]

Create a list of consecutive integers from two to n: (2, 3, 4, ..., n). [列出从2到n的一串连续自然数]
Initially, let p equal 2, the first prime number. [开始,先把2当作第一个素数,并赋值给变量p]
Strike from the list all multiples of p less than or equal to n. (2p, 3p, 4p, etc.) [将该列自然数中划掉p的倍数]
Find the first number remaining on the list after p (this number is the next prime); replace p with this number. [将剩下的自然数按原来顺序重新组成新的一列,并将第一个数赋给变量p]
Repeat steps 3 and 4 until p2 is greater than n. [重复第三第四步,直到p的平方大于n]
All the remaining numbers in the list are prime. [剩下的自然数就是所有小于n的素数]


	public static List<Long> find_prime_below_number(long number){
		int exe_number = 0;
		boolean close = false;
		List<Long> numberSet = new ArrayList<Long>();
		List<Long> resultSet = new ArrayList<Long>();
		
		for(long i=3; i<number; i+=2){
			numberSet.add(i);
			exe_number++;
		}
		double half = Math.sqrt(number);
		do{
			Long curN = numberSet.get(0);
			resultSet.add(curN);
			for(int j=0; j<numberSet.size(); j++){
				exe_number++;
				long l = numberSet.get(j);
				if(l%curN==0){
					numberSet.remove(j);
				}
			}
			if(curN>half){
				close = true;
			}
		}while(!close&&numberSet.size()!=0);
		
		if(numberSet.size()!=0){
			for(Long n : numberSet){
				exe_number++;
				resultSet.add(n);
			}
		}
		System.out.println("exe_number:"+exe_number);
		return resultSet;
	}


当找1000000以下的素数时,执行就挂了
肯定有问题
因为原算法说了是10 million
以下的数字~
一千万以下啊~
想办法解决
分享到:
评论

相关推荐

    埃拉托色尼示例代码

    **埃拉托色尼素数筛选法** 埃拉托色尼素数筛选法,也称为埃拉托斯特尼筛法,是一种古老而有效的算法,用于找出一个给定范围内的所有素数。该方法由古希腊数学家埃拉托色尼提出,通过一系列的排除过程,可以有效地...

    sieveofEratosthenesExample.zip

    **埃拉托色尼筛选法(Sieve of Eratosthenes)是古代数学家埃拉托色尼提出的一种寻找素数的有效算法。这个方法基于一个简单原理:从2开始,将所有能被2整除的数标记为合数,然后从下一个未标记的数3开始,将所有它的...

    素数筛选法的改进及C语言实现.pdf

    埃拉托色尼筛法(Sieve of Eratosthenes)是一种古老且高效的算法,用于找出小于或等于给定数N的所有素数。该方法首先将1到N的整数列表中的所有数字视为可能的素数,然后从2开始,对于每一个已知的素数,将其所有的...

    实验二RSA算法.pdf

    ### 埃拉托色尼筛选法(Sieve of Eratosthenes) 埃拉托色尼筛选法是一种用来找出一定范围内所有质数的古老算法。它通过筛选掉所有合数的方式,留下质数。基本步骤如下: 1. 创建一个列表,其中包含了从2开始到所需...

    MyPrime:埃拉托色尼筛

    在这个示例中,`sieveOfEratosthenes`函数实现了埃拉托色尼筛法,`MainActivity`类则负责UI交互。用户输入的数字范围会传递给该函数,然后返回的结果会在界面上展示。 为了提高性能,我们通常只筛选到平方根最大数...

    java笔试题算法-sieve:用各种语言实现Eratosthenes筛以展示GraalVM和Truffle的强大功能

    java笔试题算法多种语言的埃拉托色尼筛子 以各种语言实现 Eratosthenes 筛以展示 GraalVM 和 Truffle 的强大功能。 请先下载后再进行实验。 已经过测试可以与版本19.3.1 。 Ruby速度 使用以下命令可以发现 GraalVM ...

    Python练习——判断正整数是否为质数的三种方法

    除了以上的方法,还有其他更高级的算法来高效地找出一定范围内的所有质数,如埃拉托色尼筛选法(Sieve of Eratosthenes)和线性筛法(Linear Sieve)。埃拉托色尼筛选法是一种经典的算法,通过排除每个质数的倍数来...

    2018西农复试机考模拟题目及答案(1)1

    2. **筛选法求素数**:埃拉托色尼的筛选法(Sieve of Eratosthenes)是经典的寻找素数的方法。在编程中,可以创建一个足够大的布尔数组表示候选数,初始化所有数为真,然后从最小的素数开始,标记其倍数为假,直至...

    12.zip_图形图像处理_C/C++_

    标题中提到的“12.zip_图形图像处理_C/C++_”虽然在字面上让人联想到包含图形图像处理技术的C/C++源代码文件压缩包,但根据描述中的内容“埃拉托色尼筛法(Sieve of Eratosthenes)是一种用来求所有小于N的素数的...

Global site tag (gtag.js) - Google Analytics