/// <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. 嵌入式Qt开发环境的搭建详解

    一.嵌入式Qt开发环境的搭建前奏 1.下载arm-linux-gcc-4.4.3-20100728.tar.gz 2.下载qt-everywhere-opensource-src-4.8.5.tar. ...

  2. rpmbuild SPEC语法

    rpmbuild SPEC语法 摘自:http://bbs.chinaunix.net/thread-4179207-1-1.html spec文件写作规范 2008-09-28 11:52:17 分 ...

  3. ESP8266-iot-3

    ESP8266 WIFI操作 参考资料 模式选择 程序: 把之前的Helloworld程序复制过来改成wifi工程 先输出获取的当前模式 编译后会出错,因为os_printf的头文件没有被包含进来 结 ...

  4. AIO和NIO的理解

    AIO: AIO 背后的基本思想是允许进程发起很多 I/O 操作,而不用阻塞或等待任何操作完成,可以继续做 另外的事情,等I/O操作完成,内核会通过函数回调或者信号机制通知用户进程.这样很大程度提高了 ...

  5. sql修改排序规则,区分大小

    alter   database   数据库   COLLATE   Chinese_PRC_CS_AS     修改排序规则,改成大小写敏感的排序规则     如果只修改一个表,用alter   t ...

  6. 树形DP-----HDU4003 Find Metal Mineral

    Find Metal Mineral Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Other ...

  7. 使用Recyclerview实现图片水平自动循环滚动

    简介: 本篇博客主要介绍的是如何使用RecyclerView实现图片水平方向自动循环(跑马灯效果) 效果图: 思路: 1.准备m张图片 1.使用Recyclerview实现,返回无数个(实际Inter ...

  8. c#设计模式之:组合模式(Composite)

    一:引言 在软件开发过程中,我们经常会遇到处理简单对象和复合对象的情况,例如对操作系统中目录的处理,因为目录客园包括单独的文件,也可以包括文件夹,文件夹又是由文件组成的,由于简单对象和复合对象在功能上 ...

  9. angular OnChange事件

    import { Component, OnInit, Input, OnChanges, SimpleChanges } from '@angular/core'; @Component({ sel ...

  10. angular 输出属性

    import { Component, OnInit, EventEmitter, Output } from '@angular/core'; @Component({ selector: 'app ...