C#判断素数的方法:试除法 vs 优化的试除法 vs 米勒-拉宾素数检测算法

目录

1.素数也就质数

2. 试除法

3.优化的试除法_1

4.优化的试除法_2

5.优化的试除法_3

6.米勒-拉宾素数检测算法


1.素数也叫质数

        一个质数是一个大于1的自然数,只有两个正因数:1和它自身。这意味着如果一个数只有两个正因数,那么它就是一个质数。例如,2、3、5、7、11和13都是质数。
        质数在数学和密码学中非常重要,因为它们具有一些独特的属性。例如,质数的乘积也是质数,如果一个数是两个质数的乘积,则这个数是质因数分解的唯一方式。这使得质数在密码学中非常有用,特别是在公钥加密中,例如RSA算法。
        在编程中,可以使用不同的算法来检查一个数是否为质数,例如试除法、米勒-拉宾素性测试或AKS素性测试。

2. 试除法

        试除法算法用于检查一个数字是否为质数。它通过尝试将2到a/2之间的所有数字除以a,并检查余数是否为0来工作。如果找到任何可以将a整除的数字,该函数返回false,表明a不是质数。如果在2到a/2之间的任何数字都不能将a整除,则该函数返回true,表明a是质数。
        该算法不是特别高效,因为它检查了a的所有数字除数,但适用于小型数字。对于大型数字,更高效的算法,如米勒-拉宾素性测试或AKS素性测试,可能更合适。

//试除法判断素数
namespace _140_2
{public partial class Form1 : Form{private GroupBox? groupBox1;private TextBox? textBox1;private Label? label1;private Button? button1;private Label? label2;public Form1(){InitializeComponent();StartPosition = FormStartPosition.CenterScreen;Load += Form1_Load;}private void Form1_Load(object? sender, EventArgs e){// // textBox1// textBox1 = new TextBox{Location = new Point(84, 33),Name = "textBox1",Size = new Size(100, 23),TabIndex = 1};// // label1// label1 = new Label{AutoSize = true,Location = new Point(18, 36),Name = "label1",Size = new Size(68, 17),TabIndex = 0,Text = "输入数字:"};// // groupBox1// groupBox1 = new GroupBox{Location = new Point(12, 12),Name = "groupBox1",Size = new Size(200, 85),TabIndex = 0,TabStop = false,Text = "判断素数"};groupBox1.Controls.Add(textBox1);groupBox1.Controls.Add(label1);groupBox1.SuspendLayout();// // button1// button1 = new Button{Location = new Point(121, 103),Name = "button1",Size = new Size(75, 23),TabIndex = 2,Text = "判断",UseVisualStyleBackColor = true};button1.Click += Button1_Click;// // label2// label2 = new Label{AutoSize = true,Location = new Point(30, 109),Name = "label2",Size = new Size(43, 17),TabIndex = 3,Text = "label2"};// // Form1// AutoScaleDimensions = new SizeF(7F, 17F);AutoScaleMode = AutoScaleMode.Font;ClientSize = new Size(224, 136);Controls.Add(label2);Controls.Add(button1);Controls.Add(groupBox1);Name = "Form1";Text = "判断是否素数";groupBox1.ResumeLayout(false);groupBox1.PerformLayout();}private void Button1_Click(object? sender, EventArgs e){int j = Convert.ToInt32(textBox1!.Text);if (Prime(j) == true){label2!.Text = "是素数";}else{label2!.Text = "不是素数";}}/// <summary>/// 试除法判断素数/// </summary>static bool Prime(int a){int i;if (a == 2)return true;//else if (a == 4) { return false; }//如果使用上面这条语句,那么for循环条件用<;//如果不用上面这条语句,那么for循环条件用<=;//区别在于循环数量多一次else{for (i = 2; i <= a / 2; i++){if (a % i == 0)return false;}return true;}}}
}

 

3.优化的试除法_1

        步骤2里的试除法将2到a/2之间的所有数字除以a。优化的试除法_1首先检查a是否为2,因为2是唯一的偶数质数。然后,如果a为1或偶数,该算法返回false,因为1和所有偶数(除了2)都不是质数。最后,如果a为奇数,则该算法执行试除法,仅检查3到a的平方根之间的奇数是否可以将a整除。这比原始算法更有效,因为它避免了检查2到a/2之间的所有数字。

//优化的试除法判断素数
namespace _140_1
{public partial class Form1 : Form{private GroupBox? groupBox1;private TextBox? textBox1;private Label? label1;private Button? button1;private Label? label2;public Form1(){InitializeComponent();StartPosition = FormStartPosition.CenterScreen;Load += Form1_Load;}private void Form1_Load(object? sender, EventArgs e){// // textBox1// textBox1 = new TextBox{Location = new Point(84, 33),Name = "textBox1",Size = new Size(100, 23),TabIndex = 1};// // label1// label1 = new Label{AutoSize = true,Location = new Point(18, 36),Name = "label1",Size = new Size(68, 17),TabIndex = 0,Text = "输入数字:"};// // groupBox1// groupBox1 = new GroupBox{Location = new Point(12, 12),Name = "groupBox1",Size = new Size(200, 85),TabIndex = 0,TabStop = false,Text = "判断素数"};groupBox1.Controls.Add(textBox1);groupBox1.Controls.Add(label1);groupBox1.SuspendLayout();// // button1// button1 = new Button{Location = new Point(121, 103),Name = "button1",Size = new Size(75, 23),TabIndex = 2,Text = "判断",UseVisualStyleBackColor = true};button1.Click += Button1_Click;// // label2// label2 = new Label{AutoSize = true,Location = new Point(30, 109),Name = "label2",Size = new Size(43, 17),TabIndex = 3,Text = "label2"};// // Form1// AutoScaleDimensions = new SizeF(7F, 17F);AutoScaleMode = AutoScaleMode.Font;ClientSize = new Size(224, 136);Controls.Add(label2);Controls.Add(button1);Controls.Add(groupBox1);Name = "Form1";Text = "判断是否素数";groupBox1.ResumeLayout(false);groupBox1.PerformLayout();}private void Button1_Click(object? sender, EventArgs e){int j = Convert.ToInt32(textBox1!.Text);if (Prime(j) == true){label2!.Text = "是素数";}else{label2!.Text = "不是素数";}}/// <summary>/// 优化的试除法判断素数/// </summary>static bool Prime(int a){int i;if (a == 2)return true;else if (a == 1 || a % 2 == 0)return false;else{for (i = 3; i * i <= a; i += 2){if (a % i == 0)return false;}return true;}}}
}

 

4.优化的试除法_2

        步骤2里的试除法将2到a/2之间的所有数字除以a。优化的试除法_1在2到a/2的范围内筛除偶数。

        优化的试除法_2是通过遍历从2到这个数的平方根之间的所有整数,检查这个数是否可以被这些整数整除。如果可以被任何一个整数整除,那么这个数就不是素数。否则,这个数就是素数。

//优化的试除法_2判断素数
namespace _140_3
{public partial class Form1 : Form{private GroupBox? groupBox1;private TextBox? textBox1;private Label? label1;private Button? button1;private Label? label2;public Form1(){InitializeComponent();StartPosition = FormStartPosition.CenterScreen;Load += Form1_Load;}private void Form1_Load(object? sender, EventArgs e){// // textBox1// textBox1 = new TextBox{Location = new Point(84, 33),Name = "textBox1",Size = new Size(100, 23),TabIndex = 1};// // label1// label1 = new Label{AutoSize = true,Location = new Point(18, 36),Name = "label1",Size = new Size(68, 17),TabIndex = 0,Text = "输入数字:"};// // groupBox1// groupBox1 = new GroupBox{Location = new Point(12, 12),Name = "groupBox1",Size = new Size(200, 85),TabIndex = 0,TabStop = false,Text = "判断素数"};groupBox1.Controls.Add(textBox1);groupBox1.Controls.Add(label1);groupBox1.SuspendLayout();// // button1// button1 = new Button{Location = new Point(121, 103),Name = "button1",Size = new Size(75, 23),TabIndex = 2,Text = "判断",UseVisualStyleBackColor = true};button1.Click += Button1_Click;// // label2// label2 = new Label{AutoSize = true,Location = new Point(30, 109),Name = "label2",Size = new Size(43, 17),TabIndex = 3,Text = "label2"};// // Form1// AutoScaleDimensions = new SizeF(7F, 17F);AutoScaleMode = AutoScaleMode.Font;ClientSize = new Size(224, 136);Controls.Add(label2);Controls.Add(button1);Controls.Add(groupBox1);Name = "Form1";Text = "判断是否素数";groupBox1.ResumeLayout(false);groupBox1.PerformLayout();}private void Button1_Click(object? sender, EventArgs e){int j = Convert.ToInt32(textBox1!.Text);if (Prime(j) == true){label2!.Text = "是素数";}else{label2!.Text = "不是素数";}}/// <summary>/// 优化的试除法判断素数/// </summary>public static bool Prime(int number){if (number <= 1){return false;}for (int i = 2; i <= Math.Sqrt(number); i++){if (number % i == 0){return false;}}return true;}}
}

 

5.优化的试除法_3

         步骤2里的试除法将2到a/2之间的所有数字除以a。优化的试除法_1在2到a/2的范围内筛除偶数。优化的试除法_2是通过遍历从2到这个数的平方根之间的所有整数。

        优化的试除法_3只检查从2到这个数的平方根之间的奇数是否可以整除这个数。因为任何大于1的自然数都可以表示为2的幂次方乘以一个奇数,所以只需要检查奇数是否可以整除这个数,这样可以减少一半的检查次数。

//优化的试除法_3判断素数
namespace _140_4
{public partial class Form1 : Form{private GroupBox? groupBox1;private TextBox? textBox1;private Label? label1;private Button? button1;private Label? label2;public Form1(){InitializeComponent();StartPosition = FormStartPosition.CenterScreen;Load += Form1_Load;}private void Form1_Load(object? sender, EventArgs e){// // textBox1// textBox1 = new TextBox{Location = new Point(84, 33),Name = "textBox1",Size = new Size(100, 23),TabIndex = 1};// // label1// label1 = new Label{AutoSize = true,Location = new Point(18, 36),Name = "label1",Size = new Size(68, 17),TabIndex = 0,Text = "输入数字:"};// // groupBox1// groupBox1 = new GroupBox{Location = new Point(12, 12),Name = "groupBox1",Size = new Size(200, 85),TabIndex = 0,TabStop = false,Text = "判断素数"};groupBox1.Controls.Add(textBox1);groupBox1.Controls.Add(label1);groupBox1.SuspendLayout();// // button1// button1 = new Button{Location = new Point(121, 103),Name = "button1",Size = new Size(75, 23),TabIndex = 2,Text = "判断",UseVisualStyleBackColor = true};button1.Click += Button1_Click;// // label2// label2 = new Label{AutoSize = true,Location = new Point(30, 109),Name = "label2",Size = new Size(43, 17),TabIndex = 3,Text = "label2"};// // Form1// AutoScaleDimensions = new SizeF(7F, 17F);AutoScaleMode = AutoScaleMode.Font;ClientSize = new Size(224, 136);Controls.Add(label2);Controls.Add(button1);Controls.Add(groupBox1);Name = "Form1";Text = "判断是否素数";groupBox1.ResumeLayout(false);groupBox1.PerformLayout();}private void Button1_Click(object? sender, EventArgs e){int j = Convert.ToInt32(textBox1!.Text);if (Prime(j) == true){label2!.Text = "是素数";}else{label2!.Text = "不是素数";}}/// <summary>/// 优化的试除法判断素数/// </summary>public static bool Prime(int number){if (number <= 1){return false;}for (int i = 2; i <= Math.Floor(Math.Sqrt(number)); i += 2){if (number % i == 0){return false;}}for (int i = 3; i <= Math.Floor(Math.Sqrt(number)); i += 2){for (int j = i * i; j <= number; j += i){if (number % j == 0){return false;}}}return true;}}
}

 

6.米勒-拉宾素数检测算法

        使用更高级的算法,如米勒-拉宾素数检测算法(Miller-Rabin Primality Test),这是一种概率算法,通过反复应用费马小定理来判断一个数是否为素数。

// 米勒-拉宾素数检测算法(Miller-Rabin Primality Test)判断素数
using System.Numerics;namespace _140_5
{public partial class Form1 : Form{private GroupBox? groupBox1;private TextBox? textBox1;private Label? label1;private Button? button1;private Label? label2;public Form1(){InitializeComponent();StartPosition = FormStartPosition.CenterScreen;Load += Form1_Load;}private void Form1_Load(object? sender, EventArgs e){// // textBox1// textBox1 = new TextBox{Location = new Point(84, 33),Name = "textBox1",Size = new Size(100, 23),TabIndex = 1};// // label1// label1 = new Label{AutoSize = true,Location = new Point(18, 36),Name = "label1",Size = new Size(68, 17),TabIndex = 0,Text = "输入数字:"};// // groupBox1// groupBox1 = new GroupBox{Location = new Point(12, 12),Name = "groupBox1",Size = new Size(200, 85),TabIndex = 0,TabStop = false,Text = "判断素数"};groupBox1.Controls.Add(textBox1);groupBox1.Controls.Add(label1);groupBox1.SuspendLayout();// // button1// button1 = new Button{Location = new Point(121, 103),Name = "button1",Size = new Size(75, 23),TabIndex = 2,Text = "判断",UseVisualStyleBackColor = true};button1.Click += Button1_Click;// // label2// label2 = new Label{AutoSize = true,Location = new Point(30, 109),Name = "label2",Size = new Size(43, 17),TabIndex = 3,Text = "label2"};// // Form1// AutoScaleDimensions = new SizeF(7F, 17F);AutoScaleMode = AutoScaleMode.Font;ClientSize = new Size(224, 136);Controls.Add(label2);Controls.Add(button1);Controls.Add(groupBox1);Name = "Form1";Text = "判断是否素数";groupBox1.ResumeLayout(false);groupBox1.PerformLayout();}private void Button1_Click(object? sender, EventArgs e){int j = Convert.ToInt32(textBox1!.Text);if (Prime(j) == true){label2!.Text = "是素数";}else{label2!.Text = "不是素数";}}/// <summary>/// 米勒-拉宾素数检测算法(Miller-Rabin Primality Test)判断素数/// </summary>public static bool Prime(int number){if (number <= 1){return false;}if (number == 2 || number == 3){return true;}if (number % 2 == 0){return false;}int s = 0;int d = number - 1;while (d % 2 == 0){s++;d /= 2;}Random rand = new();int a = rand.Next(2, number - 1);BigInteger x = BigInteger.ModPow(a, d, number);if (x == 1 || x == number - 1){return true;}for (int i = 0; i < s; i++){x = BigInteger.ModPow(x, 2, number);if (x == number - 1){return true;}}return false;}}
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/541302.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Google Play上架:谷歌支付政策变更

目录 政策发布时间概括内容付款政策变动内容归纳google付款用户政策政策发布时间 2024 年 3 月 6 日 概括内容 为遵守《数字市场法案》(DMA) 的规定,从 2024 年 3 月 6 日起,我们将更新付款政策,允许开发者将欧洲经济区 (EEA) 的用户引导至其应用之外的平台(包括推广应用…

Swift:.ignoresSafeArea():自由布局的全方位掌握

ignoresSafeArea(_ regions : edges:)修饰符的说明 SwiftUI布局系统会调整视图的尺寸和位置&#xff0c;以避免特定的安全区域。这就确保了系统内容&#xff08;比如软件键盘&#xff09;或设备边缘不会遮挡您的视图。要将您的内容扩展到这些区域&#xff0c;您可以通过应用该修…

目标检测——YOLOv4算法解读

论文&#xff1a;YOLOv4&#xff1a;Optimal Speed and Accuracy of Object Detection 作者&#xff1a;Alexey Bochkovskiy, Chien-Yao Wang, Hong-Yuan Mark Liao 链接&#xff1a;https://arxiv.org/pdf/2004.10934.pdf 代码&#xff1a;https://github.com/AlexeyAB/darkne…

扛着半个互联网前进的core-js,其作者快被钱“拖垮”了。

Core-js 的作者是 Denis Pushkarev&#xff0c;他是一位俄罗斯的 JavaScript 开发者和贡献者。他在 2013 年创建了 core-js 项目&#xff0c;并一直积极地维护和更新这个库。 一、神奇的core-js Core-js 的月均 NPM 下载量为 2.5 亿次&#xff0c;总下载量高达 90 亿次&#xf…

城乡居民基本医疗信息管理系统|基于Springboot的城乡居民基本医疗信息管理系统设计与实现(源码+数据库+文档)

城乡居民基本医疗信息管理系统目录 目录 基于Springboot的城乡居民基本医疗信息管理系统设计与实现 一、前言 二、系统设计 三、系统功能设计 1、病例管理 2、医院资讯信息管理 3、医院资讯类型管理 四、数据库设计 五、核心代码 六、论文参考 七、最新计算机毕设选…

HTML_CSS练习:HTML注释

一、代码示例 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>HTML注释</title> </head> <body><marquee loop"1">马龙强<!--下面的输入框是可以滚动的&#x…

PMP和软考,考哪一个?

PMP跟软考有部分知识点是重合的&#xff0c;软考高项比较适用于计算机 IT 行业&#xff0c;而 PMP 不受行业限制&#xff0c;各行各业都适用&#xff0c;至于哪个更合适&#xff0c;看你想去国企还是民企&#xff0c;国企软考吃香&#xff0c;民企PMP 吃香 下面说下两者具体有什…

Nginx介绍、架构和安装

Nginx介绍、架构和安装 文章目录 Nginx介绍、架构和安装1.Nginx介绍2.Nginx架构3.Nginx安装3.1 主机初始化3.1.1 设置网卡名和ip地址3.1.2 配置镜像源3.1.3 关闭防火墙3.1.4 禁用SELinux3.1.5 设置时区 3.2 Nginx版本和安装方式3.3 包安装3.3.1 CentOS 安装3.3.1.1 查看当前系统…

P1149 [NOIP2008 提高组] 火柴棒等式

题目描述 给你 &#xfffd;n 根火柴棍&#xff0c;你可以拼出多少个形如 &#xfffd;&#xfffd;&#xfffd;ABC 的等式&#xff1f;等式中的 &#xfffd;A、&#xfffd;B、&#xfffd;C 是用火柴棍拼出的整数&#xff08;若该数非零&#xff0c;则最高位不能是 00&…

穿越半个世纪,探索中国数据库的前世今生

引言 在数字化潮流席卷全球的今天&#xff0c;数据库作为 IT 技术领域的“活化石”&#xff0c;已成为数字经济时代不可或缺的基础设施。那么&#xff0c;中国的数据库技术发展经历了怎样的历程&#xff1f;我们是如何在信息技术的洪流中逐步建立起自己的数据管理帝国的呢&…

css入门基础(二)链接伪类细节详讲

注释很详细&#xff0c;直接上代码 新增内容&#xff1a; 1.链接伪类的使用顺序规范 2.链接伪类的使用效果 3.浏览器安全策略对visited伪类造成的影响 4.visited伪类的工作原理 源码&#xff1a; index.html <!DOCTYPE html> <html lang"en"> <head&…