Jump to content


Java Prime Number Calculator


  • You cannot reply to this topic
2 replies to this topic

#1 npsken

    Young Padawan

  • Members
  • Pip
  • 26 posts
  • Gender:Male
  • Interests:Computer Programming<br />Graphic Design

Posted 24 March 2009 - 11:17 AM

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
/*
 * 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
/*
 * 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:
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?

#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 24 March 2009 - 12:03 PM

/*
 * 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.

Edited by rc69, 24 March 2009 - 12:04 PM.


#3 unknown_gamer

    Young Padawan

  • Members
  • Pip
  • 52 posts
  • Gender:Male
  • Location:Internet =p

Posted 08 May 2009 - 09:08 AM

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users