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

huangapple go评论78阅读模式
英文:

Check whether the given number is prime or not?

问题

这是我的代码。

Scanner scan = new Scanner(System.in);
int number = scan.nextInt();
int[] arr = new int[number];
for (int i = 0; i < number; i++) {
    arr[i] = scan.nextInt();
}

for (int i = 0; i < number; i++) {
    int num = arr[i];
    for (int j = 2; j <= Math.sqrt(num); j++) {
        if (num % j == 0 && num != 2) {
            System.out.println(num + "不是质数");
            break;
        }
    }
    System.out.println(num + "是质数");
}
英文:

I am trying to implement a code to check whether a given array of numbers is prime or not,

but when the number is not a prime number, the output displays "Prime" and "Not Prime" both answers. What is the mistake I did here and it is a pleasure to have an answer from you? Thank you in advance!

Here is my code.

    Scanner scan = new Scanner(System.in);
    int number = scan.nextInt();
    int[] arr = new int[number];
    for (int i = 0; i &lt; number; i++) {
        arr[i] = scan.nextInt();

    }

    for (int i = 0; i &lt; number; i++) {
        int num = arr[i];
        for (int j = 2; j &lt;= Math.sqrt(num); j++) {
            if (num % j == 0 &amp;&amp; num !=2) {
                System.out.println(num + &quot;Not prime&quot;);
                break;
            }
        }
        System.out.println(num +&quot;Prime&quot;);
    }

答案1

得分: 2

如果你对使你的代码更高效感兴趣,可以采用以下方法。

int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
                15, 16, 17, 18, 19, 20 };
for (int num : numbers) {
    System.out.println(num + ((isPrime(num) ? " 是" : " 不是") + " 质数"));
}		

private static boolean isPrime(int num) {
    // 两是质数
    if (num == 2) {
        return true;
    }
    // 数字小于等于1或任何偶数
    // 数字(除2外)都不是质数
    if (num <= 1 || num % 2 == 0) {
        return false;
    }
    // 现在你可以检查奇数除数。
    // 并从3开始每次增加2。
    for (int i = 3; i <= Math.sqrt(num); i+=2) {
        if (num % i == 0) {
            return false;
        }
    }
    return true;
}
英文:

If you're interested in making your code a little more efficient you can go this route.

int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
		15, 16, 17, 18, 19, 20 };
for (int num : numbers) {
	System.out.println(num + ((isPrime(num) ? &quot; is&quot; : &quot; is not&quot;) + &quot; a prime&quot;));
}		
	
private static boolean isPrime(int num) {
	// two is a prime
    if (num == 2) {
       return true;
    }
    // numbers 1 or less or any even 
    // number (sans 2) are not primes
    if (num &lt;= 1 || num % 2 == 0) {
        return false;
    }
    // Now you can check for odd divisors.
    // and increment by 2 starting with 3.
    for (int i = 3; i &lt;= Math.sqrt(num); i+=2) {
        if (num % i == 0) {
            return false;
        }
    }
    return true;
}

答案2

得分: 1

你应该记住这个数字是不是素数。你的代码没有这样做,因此两个打印语句都会被执行。

Scanner scan = new Scanner(System.in);
int number = scan.nextInt();
int[] arr = new int[number];
for (int i = 0; i < number; i++) {
    arr[i] = scan.nextInt();
}

for (int i = 0; i < number; i++) {
    int num = arr[i];
    boolean isPrime = true;

    for (int j = 2; j <= Math.sqrt(num); j++) {
        if (num % j == 0 && num != 2) {
            isPrime = false;
            break;
        }
    }
    
    if (isPrime) {
        System.out.println(num + "是素数");
    } else {
        System.out.println(num + "不是素数");
    }
}
英文:

You should remember whether the number was prime or not. Your code doesn't do that so both prints are reached.

Scanner scan = new Scanner(System.in);
int number = scan.nextInt();
int[] arr = new int[number];
for (int i = 0; i &lt; number; i++) {
    arr[i] = scan.nextInt();

}

for (int i = 0; i &lt; number; i++) {
    int num = arr[i];
    boolean isPrime = true;

    for (int j = 2; j &lt;= Math.sqrt(num); j++) {
        if (num % j == 0 &amp;&amp; num !=2) {
            isPrime = false;
            break;
        }
    }
    
    if (isPrime) {
        System.out.println(num +&quot;Prime&quot;);
    } else {
        System.out.println(num + &quot;Not prime&quot;);
    }

}

答案3

得分: 1

使用布尔值来跟踪数字是否为素数。开始时假设它为true(素数),如果发现不是素数,则将其设置为false。

boolean isPrime = true;

然后,根据该布尔值确定消息。

String message = isPrime ? "素数" : "非素数";
英文:

Use a boolean to track whether the number is prime. Assume it's true (prime) to begin with and set it false if it is discovered not to be prime.

boolean isPrime = true;

Afterwards, determine the message based on that boolean.

String message = isPrime ? &quot;Prime&quot; : &quot;Not prime&quot;;

答案4

得分: 0

你可以通过引入一个布尔变量来修复它,例如:

for (int i = 0; i < number; i++) {
    int num = arr[i];
    boolean isPrime = true;
    for (int j = 2; j <= Math.sqrt(num); j++) {
        if (num % j == 0 && num != 2) {
            isPrime = false;
            break;
        }
    }
    if (isPrime) {
       System.out.println(num + "是质数");
    } else {
       System.out.println(num + "不是质数");
    } 
}
英文:

You can fix it for example by introducing a boolean variable:

    for (int i = 0; i &lt; number; i++) {
        int num = arr[i];
        boolean isPrime = true;
        for (int j = 2; j &lt;= Math.sqrt(num); j++) {
            if (num % j == 0 &amp;&amp; num !=2) {
                isPrime = false;
                break;
            }
        }
        if (isPrime) {
           System.out.println(num +&quot;Prime&quot;);
        } else {
           System.out.println(num + &quot;Not prime&quot;);
        } 
    }

huangapple
  • 本文由 发表于 2020年4月8日 01:39:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/61086061.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定