[英]Getting weird output while finding prime number in java
I have two methods to find out prime number in java method - 2 working fine but getting wrong output from method one, can any help me where i did wrong in logic. 我有两种方法可以找出java方法中的质数-2工作正常,但是从方法一中得到错误的输出,可以帮助我在逻辑上做错的地方。 Thanks in advance 提前致谢
My entire code 我的整个代码
package prepare;
import java.util.Scanner;
public class Squar {
//Method - 1 to find prime number
boolean isPrime(int num){
int exp = (int)Math.sqrt(num);
for(int i=2;i<exp;i++){
if(exp%2==0){
return false;
}
}return true;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
Squar s = new Squar();
System.out.println("From M1 "+s.isPrime(num));
scan.close();
System.out.println("From M2 "+s.isPrimeNumber(num));
}
//Method - 2 to find prime number
public boolean isPrimeNumber(int number) {
if(number == 1){
return false;
}
if (number == 2 || number == 3) {
return true;
}
if (number % 2 == 0) {
return false;
}
int sqrt = (int) Math.sqrt(number) + 1;
for (int i = 3; i < sqrt; i += 2) {
if (number % i == 0) {
return false;
}
}
return true;
}
}
for input : 63 actual out put would be false in prime number but getting different output from method one output 输入:63个实际输出的质数为假,但与方法一输出的输出不同
63
From M1 true
From M2 false
在isPrime()
方法中,您不应该检查num % i == 0
而不是exp % 2 == 0
吗?
Have a look at this line of your code 看一下这段代码
if(exp%2==0){
it should be num % i
它应该是num % i
Change isPrime
function like this. 像这样更改isPrime
函数。
boolean isPrime(int num) {
int exp = (int) Math.sqrt(num);
for (int i = 2; i < exp; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
Because in if condition you are checking exp%2 == 0
. 因为在if条件下,您正在检查exp%2 == 0
。 But this statement does not change when iterating on i < exp
. 但是,当对i < exp
迭代时,该语句不会更改。 So this logic should be on with num % i == 0
因此,此逻辑应以num % i == 0
开启
Well I think the culprit is if(exp%2==0){
and it is causing a problem while iterating i<exp
.So you may want to tweak it to num%i==0
好吧,我认为罪魁祸首是if(exp%2==0){
并且它在迭代i<exp
时引起问题,因此您可能需要将其调整为num%i==0
I have tried to give a few other approaches to this issue. 我试图给出其他一些方法来解决这个问题。 I hope that would be helpful. 我希望这会有所帮助。
I think there is a reason that tempted you to use 我认为有一个诱使您使用的原因
(int)Math.sqrt(num); (int)Math.sqrt(num);
I have tried to elaborate it below. 我试图在下面详细说明。
Consider below 3 approaches. 考虑以下3种方法。 All of them are correct but the first 2 approaches have some drawbacks. 所有这些都是正确的,但是前两种方法有一些缺点。
Approach 1 方法1
boolean isPrime(int num) {
for(int i=2;i<num;i++) {
if(num%i==0)
return false;
}
return true;
}
We have a scope to make it faster. 我们有能力使其更快。
Consider that if 2 divides some integer n, then (n/2) divides n as well. 考虑一下, 如果2除以某个整数n,那么(n / 2)也将n除。 This tells us we don't have to try out all integers from 2 to n. 这表明我们不必尝试2到n之间的所有整数。
Now we can modify our algorithm: 现在我们可以修改算法:
Approach 2 方法2
//checks whether an int is prime or not.
boolean isPrime(int num) {
for(int i=2;2*i<num;i++) {
if(num%i==0)
return false;
}
return true;
}
Finally, we know 2 is the "oddest" prime - it happens to be the only even prime number. 最后,我们知道2是“奇数”素数-它恰好是唯一的偶数素数。 Because of this, we need only check 2 separately, then traverse odd numbers up to the square root of n. 因此,我们只需要单独检查2,然后遍历奇数直到n的平方根即可。 I think this might have tempted you to use (int)Math.sqrt(num); 我认为这可能会诱使您使用(int)Math.sqrt(num);
Approach 3 方法3
//checks whether an int is prime or not.
boolean isPrime(int num) {
//check if num is a multiple of 2
if (num%2==0) return false;
//if not, then just check the odds
for(int i=3;i*i<=num;i+=2) {
if(num%i==0)
return false;
}
return true;
}
Hence, we've gone from checking every integer (up to n to find out that a number is prime) to just checking half of the integers up to the square root . 因此, 我们从检查每个整数(最多检查n个以发现一个数为质数),到仅检查整数的一半直至平方根 。 Is it not an improvement, especially considering when numbers are large. 这不是一种改进,特别是考虑到数量很大时。
Well, your first algorithm is almost (replace %2 with %i) correct. 好吧,您的第一个算法几乎是正确的(用%i代替%2)。 I do not know the second algorithm, but i would definitely change it to this form: 我不知道第二种算法,但是我一定会将其更改为以下形式:
public boolean isPrime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i < Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.