检查给定的数字是否为素数

编程入门 行业动态 更新时间:2024-10-24 20:21:27
本文介绍了检查给定的数字是否为素数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

亲爱的先生! 我编写的代码用于检查给定的数字是否为素数。 这是代码:

使用系统; 使用 System.Collections.Generic; 使用 System.Linq; 使用 System.Text; 命名空间 prime { class Program { 静态 void Main( string [] args) { int i,m; for (m = 0 ; m< = 4 ; m ++) { Console.WriteLine( 输入数字\ n); i = Convert.ToInt32(Console.ReadLine()); if ((i / 2 )!= 0 ) { Console.WriteLine(i + \t是素数); 继续; } else { Console.WriteLine(i + \t不是素数); Console.ReadKey(); } } Console.ReadKey(); } } }

但这并没有产生所需的输出 当我输入4时它显示4是素数。 第二版

使用系统; 使用 System.Collections.Generic; 使用 System.Linq; 使用 System.Text; 命名空间 prime { class Program { 静态 void Main( string [] args) { int i,m; for (m = 2 ; m< = i-1; m ++ ) { Console.WriteLine( 输入数字\ n ); i = Convert.ToInt32(Console.ReadLine()); if ((i%m)== 0 ) { Console.WriteLine(i + {0}不是素数\ n); Console.WriteLine(); } 其他 如果(m == i) { Console.WriteLine(i + \t是prime); Console.ReadKey( true ); } } Console.Clear(); Console.ReadKey(); } } }

但它说的是变量我不是已分配

解决方案

除了您的算法完全错误之外... 4/2!= 0(4/2 = 2 )所以4是素数(根据你的代码)!!!

4/2是2,它不是0所以你的代码说它是素数。你的代码似乎正在检查奇数而不是素数。如果数字是素数,有数学家一直致力于解决问题,并且(据我所知)尚未找到任何解决方案。 www.wikihow/Check-if-a-Number-Is-Prime [ ^ ]

首先,让我们回顾一下

如果((i / 2 )!= 0 ) { Console.WriteLine(i + \t是prime); 继续; }

如果我是4,你把它除以2,得到2不是0,因此你进入如果部分条件。 其次,如果你需要知道你的号码是否是素数,你的逻辑需要不同。 你不能除以2并找出答案。例如9不能被2整除但仍不是素数。 素数 - https://en.wikipedia / wiki / Prime_number [ ^ ]。

Dear sir ! I've written code for checking given number whether it is prime or not. Here 's the code:

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace prime { class Program { static void Main(string[] args) { int i, m; for (m = 0; m<=4;m++) { Console.WriteLine("Enter the number\n"); i = Convert.ToInt32(Console.ReadLine()); if ((i / 2)!= 0) { Console.WriteLine(i + "\t is prime"); continue; } else { Console.WriteLine(i+"\t is not prime"); Console.ReadKey(); } } Console.ReadKey(); } } }

But this is not generating desired output when I enter 4 it shows that 4 is prime . Second version

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace prime { class Program { static void Main(string[] args) { int i , m; for (m = 2; m<=i-1; m++) { Console.WriteLine("Enter the number\n"); i = Convert.ToInt32(Console.ReadLine()); if ((i %m ) == 0) { Console.WriteLine(i + "{0} is not prime \n"); Console.WriteLine(); } else if(m==i) { Console.WriteLine(i + "\t is prime"); Console.ReadKey(true); } } Console.Clear(); Console.ReadKey(); } } }

but it says tat variable i is not assigned

解决方案

Except the fact that your algorithm is totally wrong... 4 / 2 != 0 (4 / 2 = 2) so 4 is a prime (according to your code)!!!

4 / 2 is 2 which is not 0 so your code is saying it is prime. Your code seems to be checking for odd\even rather than prime. There are mathematicians who have dedicated their lives to working out if numbers are prime, and (as for as I know) no solution has yet been found. www.wikihow/Check-if-a-Number-Is-Prime[^]

First, lets review

if ((i / 2)!= 0) { Console.WriteLine(i + "\t is prime"); continue; }

If i is 4 and you divide it by 2, you get 2 which is not 0 and hence you get into the if part of the condition. Second, if you need to know whether your number is a prime number or not, your logic needs to be different. You cannot divide by just 2 and find out. 9 for e.g. is not divisible by 2 but still not a prime number. Prime numbers - en.wikipedia/wiki/Prime_number[^].

更多推荐

检查给定的数字是否为素数

本文发布于:2023-11-30 21:29:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651526.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:素数   数字

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!