简体   繁体   English

将1-100的质数打印到单独的文件中?

[英]Printing prime numbers from 1-100 into a separate file?

I have to print a list of numbers 1-100 that are all prime. 我必须打印一个都是素数的数字1-100的列表。 Those prime numbers have to be printed onto a separate .txt file. 这些素数必须打印到单独的.txt文件中。

import java.io.*;
public static boolean isPrime(int number)
{
int division = 0;

if(number<1)
{
  return false;
}

for(division=1; division<=number; division++)
{
  if(number%division==0)
  {
    division+=1;
  }
  if((number%division>2) || (number==1) || (number==-1))
  {
    return false;
  }


}
return true;

} }

public static void main(String[] args) throws IOException
{
  PrintWriter fileToWrite = new PrintWriter("primeNumberList.txt");

  for(int currentNumber=1; currentNumber<=100; currentNumber++)
  {
    if(isPrime(currentNumber))
    {
      fileToWrite.println(currentNumber);
    }
  }
  fileToWrite.close();
  System.out.println("The prime numbers have been listed in the file primeList.txt");
}

} }

I have this code, whenever I run it will only print the number to. 我有这段代码,每当我运行它时,它只会将数字打印到。 Anything I can do to change it? 我有什么办法可以更改它?

This looks like a homework question, so I'll avoid complete code solutions. 这看起来像是一个作业问题,因此我将避免使用完整的代码解决方案。

Also, your last sentence seems cut-off - I'm guessing you're saying that the numbers being printed are not prime. 另外,您的最后一句话似乎是断句-我您是在说要打印的数字不是质数。

One thing you might want to look at is the Sieve of Eratosthenes . 您可能要看的一件事是Eratosthenes筛 isPrime , as you've defined it, won't work correctly. 如您所定义, isPrime将无法正常工作。

The first conditional, if (number % divisor) == 0 , will always be true for 1 (so the iterator shouldn't start there), and if it's true for any value > 1, then number is not prime, and the function should return false . 第一个条件if (number % divisor) == 0 始终对1成立(因此迭代器不应该从那里开始),如果对任何大于1的值都为true,则number不是素数,并且该函数应该返回false

Also worth noting is that the range should not be division<=number , because number%number will always be 0. 另外值得注意的是,范围不应为division<=number ,因为number%number始终为0。

The second conditional in isPrime doesn't do anything useful from the perspective of primefinding. 在第二个条件isPrime没有做任何事情,从primefinding的角度来看是有用的。

The Sieve of Eratosthenes is a more elegant prime-finder than just running through all integers lower than the number, but its implementation is left as an exercise to the student. Eratosthenes的Sieve是一个更为优雅的寻素工具,它不仅可以遍历所有小于该整数的整数,而且它的实现方法还是留给学生练习。

Good luck! 祝好运!

Your isPrime function does not work, try the below code:- 您的isPrime函数不起作用,请尝试以下代码:-

public static boolean isPrime(int number)
{       
    for(int division = 2; division < number; division++) {
        if(*MATHS FOR PRIME*)
            return false;
    }

    return true;    
}

Start at 2, the first prime number and cycle though all numbers up to it and see if it divides evenly. 从第一个质数2开始,循环遍历所有素数,看是否均分。 I have taken out the maths but you should be able to find this with a quick google. 我已经完成了数学运算,但是您应该可以通过快速的Google找到它。

this is your problem: 这是你的问题:

if((number%division>2) || (number==1) || (number==-1))

the first condition is if the modulo of the number and division is larger than 2 then return false. 第一个条件是,如果数字和除法的模数大于2,则返回false。 , this will happen for every number larger than 3 on the first iteration of the loop. ,则在循环的第一次迭代中,每个大于3的数字都会发生这种情况。 the correct loop will be something like this: 正确的循环如下所示:

for(division=2; division<number; division++)
{
  if(number%division == 0)
  {
    return false;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM