Eratosthenes筛选法
说到素数,最基本的算是一百以内的那些数了。这些数在数学竟赛中常常会被用到。比如说有这样一道题:“一百以内有多少在加2后仍然是素数的素数?”11和17就是这样的素数。如果对素数很熟悉的话,就能迅速得出答案。
那么,给定一个一百以内的数,如何迅速判断它是不是素数呢?
一个最简单的方发就是“埃拉托斯特尼筛法” (Sieve of Eratosthenes)。如上图所示,给出要筛数值的范围n,找出n√以内的素数p1,p2,…,pk。先用2去筛,即把2留下,把2的倍数剔除掉;再用下一个素数,也就是3筛,把3留下,把3的倍数剔除掉;接下去用下一个素数5筛,把5留下,把5的倍数剔除掉;不断重复下去......。

using System;
using System.Collections.Generic;
using System.Text; namespace 产生素数
{
class PrimeGenerator
{
private static bool[] crossedOut;
private static int[] result;
public static int[] GeneratePrimeNumbers(int maxValue)
{
if (maxValue < )
{
return new int[];
}
else
{
UncrossIntegersUpTo(maxValue);
CrossOutMultiples();
PutUncrossedIntegersIntoResult();
return result;
}
} private static void PutUncrossedIntegersIntoResult()
{
result = new int[NumberOfUncrossedIntegers()];
for (int j = , i = ; i < crossedOut.Length; i++)
{
if (NotCrossed(i))
{
result[j++] = i;
}
}
} private static bool NotCrossed(int i)
{
return crossedOut[i] == false;
} private static int NumberOfUncrossedIntegers()
{
int count = ;
for(int i=;i<crossedOut.Length;i++)
{
if (NotCrossed(i))
{
count++;
}
}
return count;
} private static void CrossOutMultiples()
{
int limit = DetermineIterationLimit();
for (int i = ; i <= limit; i++)
{
if (NotCrossed(i))
{
CrossOutMultiplesOf(i);
}
}
} private static void CrossOutMultiplesOf(int i)
{
for(int multiple=*i;multiple<crossedOut.Length;multiple+=i)
{
Console.WriteLine("multiple{0} {1}", multiple,i);
crossedOut[multiple] = true;
}
} private static int DetermineIterationLimit()
{
double interationLimit = Math.Sqrt(crossedOut.Length);
return (int)interationLimit;
} private static void UncrossIntegersUpTo(int maxValue)
{
crossedOut = new bool[maxValue + ];
for (int i = ; i < crossedOut.Length; i++)
{
crossedOut[i] = false;
}
}
}
}
Eratosthenes筛选法的更多相关文章
- Eratosthenes筛选法计算质数
<C和指针>第6章第4道编程题: 质数就是只能被1和本身整除的数.Eratosthenes筛选法是一种计算质数的有效方法.这个算法的第一步就是写下所有从2至某个上限之间的所有整数.在算法的 ...
- 每日一小练——Eratosthenes 筛选法
上得厅堂.下得厨房,写得代码.翻得围墙,欢迎来到睿不可挡的每日一小练! 题目:Eratosthenes筛选法 内容: 求质数是一个非常普遍的问题,通常不外乎用数去除.除到不尽时,给定的数就是质数.可是 ...
- Eratosthenes筛选法构造1-n 素数表
筛选法:对于不超过n的每个非负整数p,删除2p,3p,4p...当处理完所有数之后,还没没删除的就是素数. 代码中进行了相应的优化. 本代码功能,输入一个数,输出从1-该数之间的素数.功能待完善,可将 ...
- Eratosthenes筛选法求解质数
问题说明: 除了自身之外,无法被其它整数整除的数称之为质数,要求质数很简单,但如何快速的求出质数则一直是程式设计人员与数学家努力的课题, 在这边介绍一个着名的 Eratosthenes求质数方法. 解 ...
- 使用埃拉托色尼筛选法(the Sieve of Eratosthenes)在一定范围内求素数及反素数(Emirp)
Programming 1.3 In this problem, you'll be asked to find all the prime numbers from 1 to 1000. Prime ...
- 筛选实现C++实现筛选法
每日一贴,今天的内容关键字为筛选实现 筛选法 分析: 筛选法又称筛法,是求不超越自然数N(N>1)的全部质数的一种方法.据说是古希腊的埃拉托斯特尼(Eratosthenes,约公元前274-19 ...
- 素数筛选法(prime seive)
素数筛选法比较有名的,较常用的是Sieve of Eratosthenes,为古希腊数学家埃拉托色尼(Eratosthenes 274B.C.-194B.C.)提出的一种筛选法.详细步骤及图示讲解,还 ...
- 算法笔记_012:埃拉托色尼筛选法(Java)
1 问题描述 Compute the Greatest Common Divisor of Two Integers using Sieve of Eratosthenes. 翻译:使用埃拉托色尼筛选 ...
- C++实现筛选法
筛选法 介绍: 筛选法又称筛法,是求不超过自然数N(N>1)的所有质数的一种方法.据说是古希腊的埃拉托斯特尼(Eratosthenes,约公元前274-194年)发明的,又称埃拉托斯特尼筛子. ...
随机推荐
- hadoop hdfs的java操作
访问hdfs上的文件并写出到输出台 /** * 访问hdfs上的文件并写出到输出台 * @param args */ public static void main(String[] args) { ...
- HDU-4705 Y 树形DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4705 题意:给一颗树,从树上任意选择3个点{A,B,C},要求他们不在一条链上,求总共的数目. 容易想 ...
- 各种排序算法代码(C语言版)
选择排序 #include <stdio.h> /* * 选择排序 * 稳定性:不稳定 * 时间复杂度:O(N^2) **/ void select_sort(int a[], int l ...
- RecyclerView设置verticalSapcing等
RecyclerView没有像GridView那样有提供verticalSpacing属性,上StackOverflow找到了一种替代方法,代码如下 public class SpacesItemDe ...
- EXCEL 建立工作薄与工作表
//1.引用单元 uses ComObj; //2.建立工作薄与工作表 procedure TForm1.Button1Click(Sender: TObject); Var ExcelApp,She ...
- A Tour of Go Methods with pointer receivers
Methods can be associated with a named type or a pointer to a named type. We just saw two Abs method ...
- hdoj 2051 Bitset
Bitset Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- Java的面向对象思想
多态性: 一种方法有多种实现,采用哪一种实现由Java虚拟机在运行时动态决定,这种能力成为动态绑定(dynamic binding),也称为多态性(polymorphism)(源于一个希腊单词,意为“ ...
- IEnumerable和IEnumerator 详解
初学C#的时候,老是被IEnumerable.IEnumerator.ICollection等这样的接口弄的糊里糊涂,我觉得有必要切底的弄清楚IEnumerable和IEnumerator的本质. 下 ...
- MySQL Cluster测试过程中的错误汇总--ERROR 1296 (HY000)等等
参考资料: http://dev.mysql.com/doc/refman/5.1/en/mysql-cluster-privilege-distribution.html http://www.cl ...