Help - Search - Members - Calendar
Full Version: Java Prime Number Calculator
Pixel2Life Forum > Help Section > Desktop Programming
npsken
So I am making an example program to test myself that should find all prime numbers between two given numbers. I want to do this in an object oriented fashion.

I have a prime class with a check method, and a main class with a main method.

Here are my files:

prime.java
CODE
/*
* Prime Checker
* Kenneth Powers
*
* check() accepts a long integer and returns true or false as a boolean
*/
public class prime
{
boolean prime;

public boolean check(long n)
{
for(int i = 2; i < Math.sqrt((double)n); i++)
{
// set check to true for the first go
if(n==2)
{
prime = true;
}

// test for primeness
if(n%i==0)
{
prime = false;
break;
}
}
//Return The Result
if(prime == true)
{
return true;
} else {
return false;
}
}
}


main.java
CODE
/*
* This program will find all prime numbers between 2 given long integers
*/

//Import Scanner
import java.util.Scanner;
public class main
{

/**
* @param args
*/
public static void main(String[] args)
{
//Set up scanner
Scanner sc = new Scanner(System.in);

//Set up prime checker
prime p = new prime();

//Declare variables
long firstNumber, secondNumber;

//Get numbers from user
System.out.print("Enter the first number: ");
firstNumber = sc.nextLong();
System.out.print("Enter the second number: ");
secondNumber = sc.nextLong();

//Start crunching (alright! (borat))
System.out.println("It's HAMMER TIME!");
for(long i = firstNumber; i < secondNumber; i++)
{
if(p.check(i))
{
System.out.println("Prime Number: " + i);
}
}
System.out.println("Done!");

}

}


Both files are in an eclipse project called PRIME1. No matter what I type, the output of the program is as follows:
CODE

Enter the first number: 1
Enter the second number: 10
It's HAMMER TIME!
Done!


Does anybody know what is going on? How can I fix this?
rc69
CODE
/*
* Prime Checker
* Kenneth Powers
*
* check() accepts a long integer and returns true or false as a boolean
*/
public class prime
{
    /* No need for boolean prime; explained later */
    public boolean check(long n)
    {
        for(int i = 2; i < Math.sqrt((double)n); i++)
        {
            /* No need to check for n==2 as (2 < sqrt(2)) is always false */
            // test for primeness
            if(n%i==0)
            {
                /* Since 'n' isn't prime, just return, no need to take a break */
                return false;
            }
        }

        /* Provided 'prime' was still around, we could return it at this point.
           But since it isn't, and we know the number is prime, return true. */
        return true;
    }
}

Try the above. You can read my comments for what i changed/removed.

Also, moved to proper category.

p.s. Keep in mind that this is a brute-force algorithm. There are alternatives you can look at to speed this up if you were really interested (i.e. for those REALLY large numbers), but if i remember correctly, they aren't always 100% accurate.
unknown_gamer
What I would do would be. Make a isPrime method(to determine if a number is prime), so while I am cycling through the numbers between 1-10 for example, it will tell me which are prime, and using an IF statement I will start to concatenate them into a string, then showing them as an output.

As simple as that.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.