说到素数,最基本的算是一百以内的那些数了。这些数在数学竟赛中常常会被用到。比如说有这样一道题:“一百以内有多少在加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筛选法的更多相关文章

  1. Eratosthenes筛选法计算质数

    <C和指针>第6章第4道编程题: 质数就是只能被1和本身整除的数.Eratosthenes筛选法是一种计算质数的有效方法.这个算法的第一步就是写下所有从2至某个上限之间的所有整数.在算法的 ...

  2. 每日一小练——Eratosthenes 筛选法

    上得厅堂.下得厨房,写得代码.翻得围墙,欢迎来到睿不可挡的每日一小练! 题目:Eratosthenes筛选法 内容: 求质数是一个非常普遍的问题,通常不外乎用数去除.除到不尽时,给定的数就是质数.可是 ...

  3. Eratosthenes筛选法构造1-n 素数表

    筛选法:对于不超过n的每个非负整数p,删除2p,3p,4p...当处理完所有数之后,还没没删除的就是素数. 代码中进行了相应的优化. 本代码功能,输入一个数,输出从1-该数之间的素数.功能待完善,可将 ...

  4. Eratosthenes筛选法求解质数

    问题说明: 除了自身之外,无法被其它整数整除的数称之为质数,要求质数很简单,但如何快速的求出质数则一直是程式设计人员与数学家努力的课题, 在这边介绍一个着名的 Eratosthenes求质数方法. 解 ...

  5. 使用埃拉托色尼筛选法(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 ...

  6. 筛选实现C++实现筛选法

    每日一贴,今天的内容关键字为筛选实现 筛选法 分析: 筛选法又称筛法,是求不超越自然数N(N>1)的全部质数的一种方法.据说是古希腊的埃拉托斯特尼(Eratosthenes,约公元前274-19 ...

  7. 素数筛选法(prime seive)

    素数筛选法比较有名的,较常用的是Sieve of Eratosthenes,为古希腊数学家埃拉托色尼(Eratosthenes 274B.C.-194B.C.)提出的一种筛选法.详细步骤及图示讲解,还 ...

  8. 算法笔记_012:埃拉托色尼筛选法(Java)

    1 问题描述 Compute the Greatest Common Divisor of Two Integers using Sieve of Eratosthenes. 翻译:使用埃拉托色尼筛选 ...

  9. C++实现筛选法

    筛选法 介绍: 筛选法又称筛法,是求不超过自然数N(N>1)的所有质数的一种方法.据说是古希腊的埃拉托斯特尼(Eratosthenes,约公元前274-194年)发明的,又称埃拉托斯特尼筛子. ...

随机推荐

  1. 最近升级了一下小老婆(8核 2x8G DDR3 128G SSD)

    晒图(笔者的硬件知识属于小白级别, 且看且参考吧): 另外优化一下休眠&虚拟内存功能节省SSD空间: 1. 台式机, 休眠功能基本没必要, 果断关掉 C:\Windows\system32&g ...

  2. android http 通信(httpclient 实现)

    1.httpclient get 方式 HttpGet httpGet = new HttpGet(url); HttpClient client = new DefaultHttpClient(); ...

  3. SQLite使用教程8 Insert 语句

    http://www.runoob.com/sqlite/sqlite-insert.html SQLite Insert 语句 SQLite 的 INSERT INTO 语句用于向数据库的某个表中添 ...

  4. CentOS下系统时间同步和时区的修改和设置(用的这个)

    一.修正时区 rm -rf /etc/localtime #删除当前默认时区www.kwx.gd ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localt ...

  5. poj 2251 搜索

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13923   Accepted: 5424 D ...

  6. 学习 MFC之 工具栏(二)

    对于InitToolBar()函数进行进一步解析: 1.首先声明一个全局对象:  CToolBar m_toolbar; 2.然后用create()创建toolbar: //创建ToolBar工具条 ...

  7. MyEclipse与Mysql数据库的连接

    1.载入MySql驱动程序 Class.forName("com.mysql.jdbc.Driver");    // 载入MySql驱动程序 2.建立Connection连接对象 ...

  8. 【转】CppUnit使用简介

    以下内容来自:http://www.cnblogs.com/wishma/archive/2008/08/01/1258370.html 1. 简介 CppUnit 是个基于 LGPL 的开源项目,最 ...

  9. Codeforces Round #200 (Div. 1) B. Alternating Current 栈

    B. Alternating Current Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/343 ...

  10. 解决Please ensure that adb is correctly located at 'D:\java\sdk\platform-tools\adb.exe' and can be executed.

    遇到问题描述: 运行android程序控制台输出 [2012-07-18 16:18:26 - ] The connection to adb is down, and a severe error ...