1

So here's my problem that I have for homework.

Write a program that takes a command-line argument N (integer, N>= 1) and prints the number of primes less than or equal to N. A number is prime if it is divisible only by itself and 1.

Here's the code that I've come up with so far, and I'm sure that there's plenty of errors to it.

import java.util.Random;

public class PrimeCounter {
    public static void main(String[] args) {
        Random rand = new Random();
        int N = rand.nextInt();

        for(int number = 2; 1 <= N; number++){
            if(isPrime(number)){
                System.out.println(number);
            }
        }
    }

    public static boolean isPrime(int number){
        for(int i=2; i<=number; i++){
            if(number%i == 0){
                return false;
            }
        }
        return true;
    }
}

Any suggestions? Every time I run it through the unit tests through eclipse I fail all of the tests my professor has given me. From the way that I understand the assignment, he wants the program to take the number N and print out how many prime numbers are less than or equal to N. The problem is that we haven't gone over inputs yet so I'm not sure how he wants us to do this program without having some kind of input line for N?

4
  • N is a command-line argument, so it will be in args in main.
    – ooga
    Oct 27, 2014 at 23:24
  • You could investigate the args array: docs.oracle.com/javase/tutorial/essential/environment/… Oct 27, 2014 at 23:24
  • 1
    This is too broad a question to answer, especially as we don't know how it is "failing". I'd recommend putting in some print statements that show what the program thinks the list of primes is. As for not knowing how to accept input, review your class notes and speak with your professor if you are having issues.
    – user764357
    Oct 27, 2014 at 23:24
  • 1
    Should be for(int i=2; i<number; i++){ as number % number is always == 0
    – user2134086
    Oct 27, 2014 at 23:31

2 Answers 2

0

1: When you run a Java program through the command line, you have the option of giving "arguments" that are stored in the args array (that's what "String[] args" is for).

For example, the command "java PrimeCounter 1000" would run your program and store "1000" (as a string) in the args array. You can access this through args[0], which returns the string (you'll have to convert it to an integer) in the array's first slot, which in this case would be "1000".

2: Here are some comments on your code:

if(isPrime(number)){
    System.out.println(number);
}

This prints out each prime, not the number of primes. You should add 1 to a "counter" variable each time you find a prime, then print that counter when you've finished searching.

for(int number = 2; 1 <= N; number++){

"1 <= N" translates to "while 1 is less than N", but what you want is to continue while number is less than N.

3
  • This was the perfect response. I changed the 1 to number and I'm starting to look at how he did the counter variables in class. Thank you!
    – Bryan Cruz
    Oct 28, 2014 at 0:19
  • Also, I love your username
    – Bryan Cruz
    Oct 28, 2014 at 0:20
  • Thanks to you too. Good luck!
    – Pig
    Oct 28, 2014 at 0:26
0

This is an implementation of the Sieve of Eratosthenes.

public class PrimeCounter {
  public static boolean isPrime(int number){
    if(number == 2 || number == 3 | number == 5 || number == 7) return true;
    return ((number % 2) != 0 && (number % 3) != 0 && (number % 5) != 0 && (number % 7) != 0);
  }

  public static void main(String args[]){
    if(args.length == 0){
      System.out.println("Usage:\n\tjava primecounter <number>");
      System.exit(0);
    }
    int ceiling = Integer.parseInt(args[0]);
    int numberprimes = 0;
    for(int i = 0;i < ceiling;i++){
      if(PrimeCounter.isPrime(i)){
        numberprimes++;
        System.out.println(i);
      }
    }
    System.out.println("Total Number of Primes: " + numberprimes);
  }
}

Can be run as such:

javac <file>.java
java  <file> <numberToGoUpTo>

NOTE THAT THE ABOVE ONLY WORK ON THE COMMAND LINE!
PS: does not include the .java.

1
  • Note: compiled and ran perfectly!
    – Lux
    Apr 21, 2015 at 23:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.