[英]Finding prime numbers with a custom IsPrime method
I started learning Java about one month ago and today I saw this question I couldn't solve. 我在一个月前开始学习Java,今天我看到了这个问题我无法解决。
The question was: 问题是:
Write a method named
isPrime
, which takes an integer as an argument and returnstrue
if the argument is a prime number, orfalse
otherwise. 编写一个名为isPrime
的方法,该方法将整数作为参数,如果参数是素数,则返回true
否则返回false
。 Demonstrate the method in a complete program. 在完整的程序中演示该方法。
And the second part says: 第二部分说:
Use the
isPrime
method that you wrote in previous program in a program that stores a list of all the prime numbers from1
through100
in a file. 使用您在先前程序中编写的isPrime
方法,该方法存储文件中1
到100
所有素数的列表。
Here's my code, which doesn't work: 这是我的代码,它不起作用:
import java.io.*;
public class PrimeNumbers {
public static void main (String args[]) throws IOException {
PrintWriter outputFile = new PrintWriter("PrimeNumber.txt");
int j = 0;
for (int i = 0; i < 100; i++) {
isPrime(i);
outputFile.println("Prime nums are:" + i);
}
}
public static boolean isPrime (int j) {
int i;
for (j = 2; j < i; j++) {
if (i % j == 0) {
return false;
}
if (i == j) {
return true;
}
}
}
}
Your condition for returning true
in isPrime
- if (i == j)
- can never be met, since it's inside a loop whose condition is j < i
. 你在isPrime
返回true
条件 - if (i == j)
- 永远不能满足,因为它在一个循环中,其条件是j < i
。 Instead, just return true
after the loop. 相反,只需在循环后返回true
。 If the loop ends without returning false
, you know for sure that the input number is prime. 如果循环结束而没有返回false
,则确定输入数字是素数。
Your code that uses isPrime
is not checking the value returned by this method. 使用isPrime
代码不检查此方法返回的值。 You must check it in order to decide whether to write the number to the output file. 您必须检查它以决定是否将数字写入输出文件。
import java.io.IOException;
import java.io.PrintWriter;
public class PrimeNumbers
{
public static void main(String args[]) throws IOException
{
PrintWriter primeNumbersWriter = new PrintWriter("PrimeNumber.txt");
for (int i = 0; i < 100; i++)
{
// You didn't do anything with the return value of isPrime()
if (isPrime(i))
{
primeNumbersWriter.println("Prime numbers are: " + i);
}
}
// Please close writers after using them
primeNumbersWriter.close();
}
public static boolean isPrime(int prime)
{
// Do not use the number to check for prime as loop variable, also it's
// sufficient to iterate till the square root of the number to check
for (int number = 2; number < Math.sqrt(prime); number++)
{
if (prime % number == 0)
{
return false;
}
}
// You didn't always return a value, it won't let you compile otherwise
return true;
}
}
Prime Number
A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. 素数(或素数)是大于1的自然数,除了1和自身之外没有正除数。
What should be the logic? 应该是什么逻辑?
%
to find at least one number which can divide the passed number. 使用循环开始检查模数%
以找到至少一个可以除去传递的数字的数字。 passedNumber
. 检查,直到达到passedNumber
值。 0
for atleast one, it's not prime thank god! 如果模数为至少一个给出0
,那么它不是主要感谢上帝! 0
for any number ...oh man it's Prime.
如果modulo对于任何数字都不是0
...哦,这是Prime.
What are the problems in your code? 你的代码有什么问题?
j
which is the limit and you are incrementing it! 你正在循环,但使用变量j
这是限制,你正在递增它! i < j
how can the condition i == j
be true? 如果你想循环通过i < j
,条件i == j
怎么可能是真的? boolean
why are you using method as void
! 如果方法返回boolean
为什么你使用method作为void
! Use that returned value. 使用返回的值。 We did it...Just try now! 我们做到了......试试吧!
about isPrime: First of all, u should loop for( j = 2; j*j <= i; j++)
关于isPrime:首先,你应该循环for( j = 2; j*j <= i; j++)
the reason for this loop is that if a number isn't prime, its factor must be less or equal to the squared root of i, so there is no need to loop after that point now, if loop didn't return false - return true` 这个循环的原因是如果一个数不是素数,它的因子必须小于或等于i的平方根,所以现在没有必要在该点之后循环,如果循环没有返回false - 返回TRUE`
about second function: use if before checking isPrime - if(isPrime(i)) {add i to list}
关于第二个函数:在检查之前使用ifPrime - if(isPrime(i)) {add i to list}
see here is your solution. 看到这里是你的解决方案。
import java.util.Scanner;
public class Testing {
public static void main(String args[]) {
Scanner scnr = new Scanner(System.in);
int number = Integer.MAX_VALUE;
System.out.println("Enter number to check if prime or not ");
while (number != 0) {
number = scnr.nextInt();
System.out.printf("Does %d is prime? %s %s %s %n", number,
isPrime(number), isPrimeOrNot(number), isPrimeNumber(number));
}
}
/*
* Java method to check if an integer number is prime or not.
* @return true if number is prime, else false
*/
public static boolean isPrime(int number) {
int sqrt = (int) Math.sqrt(number) + 1;
for (int i = 2; i < sqrt; i++) {
if (number % i == 0) {
// number is perfectly divisible - no prime
return false;
}
}
return true;
}
/*
* Second version of isPrimeNumber method, with improvement like not
* checking for division by even number, if its not divisible by 2.
*/
public static boolean isPrimeNumber(int number) {
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;
}
/*
* Third way to check if a number is prime or not.
*/
public static String isPrimeOrNot(int num) {
if (num < 0) {
return "not valid";
}
if (num == 0 || num == 1) {
return "not prime";
}
if (num == 2 || num == 3) {
return "prime number";
}
if ((num * num - 1) % 24 == 0) {
return "prime";
} else {
return "not prime";
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.