[英]Check prime numbers
I need to make a GUI application that checks prime numbers when the user input two numbers.当用户输入两个数字时,我需要制作一个检查素数的 GUI 应用程序。 The criteria is that I make two methods, one that checks if a number is prime and that is my
isPrime(int n)
and another that checks which of the two inputs is lower/higher so that the program knows where to start.标准是我制作了两种方法,一种检查数字是否为素数,即我的
isPrime(int n)
,另一种检查两个输入中的哪个更低/更高,以便程序知道从哪里开始。
I can't manage to do this.我无法做到这一点。 When I run the script I don't have any output in Listbox.
当我运行脚本时,列表框中没有任何 output 。
My code so far:到目前为止我的代码:
private void button1_Click(object sender, EventArgs e)
{
int num1, num2;
num1 = Int32.Parse(textBox1.Text);
num2 = Int32.Parse(textBox2.Text);
for (int i = num1; i <= num2; i++) //Method that checks which of the two inputs is lower/hihger
{
if (isPrime(i))
{
for (int j = 2; j <= 1 / 2; j++)
{
if (i % j == 0)
{
break;
}
}
if (i == 0 && i != 1)
{
listBox1.Items.Add(i);
}
}
}
}
private static bool isPrime(int n) //Method to check prime numbers
{
for (int i = 2; i < n; i++)
{
if (n % i == 0)
{
return false;
}
}
return true;
}
Let's start from isPrime
.让我们从
isPrime
开始。 Since it takes int n
as an argument, n
can be up to int.MaxValue ~ 2_000_000_000
;由于它以
int n
作为参数,因此n
可以达到int.MaxValue ~ 2_000_000_000
; 2 billions iterations is a bit too many when we can easily implement a routine with sqrt(2_000_000_000) / 2 ~ 30_000
loops only:当我们可以轻松地使用
sqrt(2_000_000_000) / 2 ~ 30_000
循环实现例程时, 20 亿次迭代有点太多了:
private static bool isPrime(int n) {
if (n <= 1)
return false;
else if (n % 2 == 0)
return n == 2;
int max = (int)(Math.Sqrt(n) + 0.5);
for (int divisor = 3; divisor <= max; divisor += 2)
if (n % divisor == 0)
return false;
return true;
}
Now, it's time to get the primes between num1
and num2
:现在,是时候获取
num1
和num2
之间的质数了:
for (int i = num1; i <= num2; ++i)
if (isPrime(i))
listBox1.Items.Add(i);
please, note, that since we've optimized isPrime
we can keep for
loop being very simple.请注意,由于我们优化了
isPrime
,我们可以保持for
循环非常简单。
Your isPrime
function is correct, just minor change you need is that update condition to i <= Math.Sqrt(n)
as for (int i = 2; i <= Math.Sqrt(n); i++)
.您的
isPrime
function 是正确的,您需要的只是较小的更改是更新条件为i <= Math.Sqrt(n)
for (int i = 2; i <= Math.Sqrt(n); i++)
。 That would be more efficient
.那会更有
efficient
。 Sieve of Eristhostenes . Eristhostenes 筛。
You can also refer below links to find better and efficient way to check isPrime
.您还可以参考下面的链接以找到更好、更有效的方法来检查
isPrime
。 As suggested by @Fildor in comments.正如@Fildor在评论中所建议的那样。
To find min
or max
value from two inputs use Math.Min
& Math.Max
like shown below.要从两个输入中查找
min
或max
,请使用Math.Min
和Math.Max
,如下所示。 And update your for
loop as below.并如下更新您的
for
循环。
private void button1_Click(object sender, EventArgs e)
{
int num1, num2;
num1 = Math.Min(Int32.Parse(textBox1.Text), Int32.Parse(textBox2.Text));
num2 = Math.Max(Int32.Parse(textBox1.Text), Int32.Parse(textBox2.Text));
for (int i = num1; i <= num2; i++) //Method that checks which of the two inputs is lower/hihger
{
if (i != 0 && i != 1 && isPrime(i))
{
listBox1.Items.Add(i);
}
}
}
private static bool isPrime(int n) //Method to check prime numbers
{
for (int i = 2; i <= Math.Sqrt(n); i++)
{
if (n % i == 0)
{
return false;
}
}
return true;
}
Just to add another view to the problem:只是为问题添加另一种观点:
I wouldn't check ( as in "compute" ) primes here at all .我根本不会在这里检查(如“计算”)素数。
Int32 contains a pretty finite number of primes, which allows us to either precompute or even just download a list from the internet if you can find one. Int32 包含相当有限数量的素数,这使我们可以预先计算,甚至可以从互联网上下载一个列表(如果你能找到的话)。
As soon as we have it, all you need to do is a lookup of those that happen to be in the desired range.一旦我们拥有它,您需要做的就是查找恰好在所需范围内的那些。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.