/// <summary>
/// 素数帮忙类
/// 本类是从.net源码 类 internal static class HashHelpers 类里抽取相应的代码
/// https://referencesource.microsoft.com/#mscorlib/system/collections/hashtable.cs,e8668bf19da49963
/// Hashtable.cs里
/// </summary>
public class PrimeHelper
{
/// <summary>
/// 给定一些素数(除了2)
/// </summary>
public static readonly int[] Primes = {
, , , , , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , ,
, , , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , }; /// <summary>
/// 判断是否是素数
/// </summary>
/// <param name="candidate"></param>
/// <returns></returns>
public static bool IsPrime(int candidate)
{
//偶数当然不是素数
if ((candidate & ) != )
{
int limit = (int)Math.Sqrt(candidate);
for (int divisor = ; divisor <= limit; divisor += )
{
if ((candidate % divisor) == )
return false;
}
return true;
} //2是素数
return (candidate == );
} /// <summary>
/// 大于给定数的最小素数
/// </summary>
/// <param name="min"></param>
/// <returns></returns>
public static int GetPrime(int min)
{
if (min < )
{
throw new ArgumentException("给定数必须是自然数");
} //先从给定数组里找
foreach (int prime in Primes)
{
if (prime >= min)
{
return prime;
}
} //给定数组里没有相应的素数,则循环找
//如果min是偶数,则起始值为加1,否则起始值为本身
for (int i = (min | ); i < Int32.MaxValue; i += )
{
if (IsPrime(i) )
return i;
} return min;
} /// <summary>
/// 获取最小素数(2除外)
/// </summary>
/// <returns></returns>
public static int GetMinPrime()
{
return Primes[];
} }

C#工具类之素数扩展类的更多相关文章

  1. [Django REST framework - 视图组件之视图基类、视图扩展类、视图子类、视图集]

    [Django REST framework - 视图组件之视图基类.视图扩展类.视图子类.视图集] 视图继承关系 详图见文章末尾 视图组件可点我查看 两个视图基类:APIView.GenericAP ...

  2. C#工具类之字符串扩展类

    /// <summary> /// 字典串帮忙类 /// </summary> public static class StringHelper { /// <summa ...

  3. C#工具类之字典扩展类

    using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...

  4. c#工具类之Int扩展类

    public static class IntHelper { /// <summary> /// 转换为2进制字符串 /// </summary> /// <param ...

  5. C#工具类之XmlNode扩展类

    using System; using System.Linq; using System.Xml; /// <summary> /// XmlNodeHelper /// </su ...

  6. C#工具类之日期扩展类

    /// <summary> /// DateTimeHelper /// </summary> public static class DateTimeHelper { /// ...

  7. ios开发总结:Utils常用方法等收集,添加扩展类,工具类方法,拥有很多方便快捷功能(不断更新中。。。)

    BOBUtils 工具大全 本人github开源和收集功能地址:https://github.com/niexiaobo [对ios新手或者工作一年以内开发人员很有用处] 常用方法等收集.添加扩展类. ...

  8. DRF框架(五)——context传参,二次封装Response类,两个视图基类(APIView/GenericAPIView),视图扩展类(mixins),子类视图(工具视图),视图集(viewsets),工具视图集

    复习 1.整体修改与局部修改 # 序列化get (给前端传递参数) #查询 ser_obj = ModelSerializer(model_obj) #只传递一个参数,默认是instance的参数,查 ...

  9. 第一步 使用sencha touch cmd 4.0 创建项目、打包(加入全局变量、公用类、自定义扩展、资源文件)

    参考资料: http://www.cnblogs.com/qqloving/archive/2013/04/25/3043606.html http://www.admin10000.com/docu ...

随机推荐

  1. 18-拍卖叫价(hdu2149 巴什博弈)

    http://acm.hdu.edu.cn/showproblem.php?pid=2149 Public Sale Time Limit: 1000/1000 MS (Java/Others)    ...

  2. Entity Framework 6.0 Tutorials(10):Index Attribute

    Index Attribute: Entity Framework 6 provides Index attribute to create Index on a particular column ...

  3. 手机APP兼容性测试

    兼容性测试方案 兼容性问题 屏幕分辨率兼容性问题 软件(iOS和Android系统版本及不同厂家的定制ROM)兼容性问题 硬件(不同的CPU.内存大小等等)兼容性问题 网络(2G/3G/4G/WIFI ...

  4. [GO]二维数组的介绍

    package main import "fmt" func main() { ][]int // 有几个方括号就是几维数据 // 有几个方括号就需要几重循环 k := ; i&l ...

  5. 安装vmtools Error: Unable to execute "/usr/bin/vmware-uninstall-tools.pl.

    Error: Unable to execute "/usr/bin/vmware-uninstall-tools.pl. 安装vmware tools错误解决办法 很多朋友都在用vmwar ...

  6. 20169219 SEED SQL注入实验

    实验环境SEED Ubuntu镜像 环境配置 实验需要三样东西,Firefox.apache.phpBB2(镜像中已有): 1.运行Apache Server:只需运行命令sudo service a ...

  7. poj1840 Eqs(hash+折半枚举)

    Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 The co ...

  8. 关于AJAX与JSON的杂记

    一.当网页需要有多个XMLHttpRequest对象时,可以使用Callback 函数,callback 函数是一种以参数形式传递给另一个函数的函数. <html> <head> ...

  9. SpringMVC+Hibernate 项目开发之一(Maven环境搭建)

    Maven环境搭建网上一大堆文章,直接引用leiOOlei同学的了:http://www.cnblogs.com/leiOOlei/p/3359561.html Maven版本:apache-mave ...

  10. 关于nosql的讲解

    Data Base  关于nosql的讲解 nosql非关系型数据库. 优点: 1.可扩展 2.大数据量,高性能 3.灵活的数据模型 4.高可用 缺点: 1.不正式 2.不标准 非关系型数据库有哪些: ...