简体   繁体   中英

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

I have to print a list of numbers 1-100 that are all prime. Those prime numbers have to be printed onto a separate .txt file.

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 . isPrime , as you've defined it, won't work correctly.

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 .

Also worth noting is that the range should not be division<=number , because number%number will always be 0.

The second conditional in isPrime doesn't do anything useful from the perspective of 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.

Good luck!

Your isPrime function does not work, try the below code:-

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. I have taken out the maths but you should be able to find this with a quick 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. , this will happen for every number larger than 3 on the first iteration of the loop. the correct loop will be something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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