如何从给定范围中查找素数 [英] How to find prime numbers from the given range

查看:111
本文介绍了如何从给定范围中查找素数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





有没有办法让我们可以给出范围并找到给定范围之间的素数。当我尝试这样做时,我将2作为我的第一个数字,如果我将任何数字作为第二个数字,那么代码会在给定数字之间找到正确的素数。但如果我将任何其他数字作为第一个数字然后代码混乱,而不是2。错误是:它显示给定范围之间的所有值。我写的代码类似于Rahul Aman的代码。



任何帮助都将不胜感激。



谢谢!



问候,



Sandeep



我尝试过:



Hi,

Is there a way so that we can give the range and find the Prime Numbers between the given range. When I try to do that, and I give 2 as my first number and if I give any number as second number then the code finds the correct prime numbers between the given numbers. But instead of 2 if I give any other number as first number and then the code messes up. Error is: It shows all the values between the given range. The code which I wrote is similar to that of Rahul Aman.

Any help would be appreciated.

Thank You!

Regards,

Sandeep

What I have tried:

StringBuilder sb = new StringBuilder();
int firstNumber, secondNumber;
bool isPrime = true;
firstNumber = int.Parse(tbxPN.Text);
secondNumber = int.Parse(tbxSPN.Text);
sb.Append(" The required prime numbers between "+firstNumber+" and "+secondNumber+" are:<br>");
for (int i=firstNumber;i<=secondNumber;i++)
{
    for (int j=firstNumber;j<=secondNumber;j++)
    {
        if(i!=j && i % j == 0)
        {
            isPrime = false;
            break;
        }

    }
    if (isPrime)
    {
        sb.Append(""+i+"<br>");
    }
    isPrime = true;
}
lbResult.Text = sb.ToString();

推荐答案

您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]

http: //docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your -first-java-application.html [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

当代码没有达到预期的效果时,你就接近了一个错误。



使用调试器,你会发现测试可能性是错的整数的除数。

整数的可能除数不依赖于你检查的序列。

如果你考虑它,你会发现你只需要要测试的整数,以了解您需要做什么以及何时停止。你也可以在一个函数中嵌入初步测试 isPrime()



[更新]

给DownVoters:

我想知道为什么你不喜欢这个答案?

做推荐学习调试器有什么不对?
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

With the debugger, you will see that you are wrong on testing possibles divisors of an integer.
The possible divisors of an integer do not depend on the sequence you are checking.
If you think about it, you will see that you only need the integer you want to test to know what you need to do and when to stop. You can as well embed the primarity test in a function isPrime()

[Update]
To DownVoters:
I would like to kno why you don't like this answer ?
What is wrong doing a recommendation of learning the debugger ?


你说这不是一个家庭作业问题,但它是非常家庭作业的类型分配。谷歌搜索c#中的素数会给你带来大量的结果。无论你是否还在上学都无所谓,你需要学会掌握的一项技能就是研究你的问题。



查找1到10之间的素数 - Google搜索 [ ^ ]



返回691k结果,前两个链接回答您的问题。但请记住,编程并不总能提供一种适合所有解决方案的解决方案......您可能需要做一些腿部工作来按摩互联网上的示例以满足您的需求。经常搜索确切的问题并没有显示出有意义的解决方案。



有了这个说法,这里有一个选项可以调整代码来查找给定的素数范围。



从此链接获取IsPrime方法:



c# - 检查数字是否为素数 - 堆栈溢出 [ ^ ]



然后将代码降低到1循环。这样的事情:



You say this isn't a homework problem, but it is very "homeworky" type assignment. A google search for figuring out prime numbers in c# would have brought you tons of results. Whether or not you are still in school doesn't matter, the one skill that you need to learn to master is researching your issues.

find prime numbers between 1 and 10 - Google Search[^]

Returns 691k results and the first two links answer your question. But keep in mind, programming doesn't always provide one size fits all solutions...you might actually have to do some leg work to massage examples on the internet to meet your needs. Doing a search for exactly your problem often times doesn't reveal meaningful solutions.

With that said, here is one option adapting your code to finding prime numbers in a given range.

Take the IsPrime method from this link:

c# - Check if number is prime number - Stack Overflow[^]

And then work your code down to 1 loop. Something like this:

for (int i = firstNumber; i <= secondNumber; i++)
{
    if (IsPrime(i))
    {
        sb.Append("" + i + "");
    }

}





了解如何将isprime方法按摩到您的码?这就是我的意思,做一个小腿工作来解决你的问题。



See how the isprime method was massaged into your code? That is what i mean by doing a little leg work to solve your issue.


这篇关于如何从给定范围中查找素数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆