1,105,221 Community Members

Counting Prime Numbers in Java

Member Avatar
spittballz
Newbie Poster
1 post since Feb 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I am trying to create a program that counts the Prime numbers then I will continue and use this second method in my main method.
I am confused how to count the Prime numbers. I just pulled the text from JEdit.. not sure if that was the right way to enter my text.

public class Prime
{
	private static boolean primeCounter(int number)
	{
		int counter = 0
		
		for (int i = 2; i < number; i++)
	        {
		if (number % i == 0)
			return false;
		else
			return true;

		if (false);  //not sure how to count which prime number I am up to according to my user input of a positive integer
		counter++;
	        }
	}

// This is where I will enter my main method
                public static void main(String[] args)
	{

		String userInput = JOptionPane.showInputDialog(null, "Please enter a positive Integer:");
		int num = Integer.parseInt(userInput);
}
Member Avatar
verruckt24
Posting Shark
945 posts since Nov 2008
Reputation Points: 438 [?]
Q&As Helped to Solve: 97 [?]
Skill Endorsements: 0 [?]
 
0
 

You have made a lot of syntactical mistakes here.

if (number % i == 0)
	return false;
else
	return true;
if (false);

Do you really think the control will ever reach the line "if (false)" ? Also let's for a moment assume it does, will the control ever enter the statement in the if block ? What do you think are you doing evaluating if (false) ?
By putting a semi-colon immediately after the if condition you are terminating the if block there itself, so the "counter++" statement falls outside the block and is executed irrelevant of the condition in the if block being true or false.

Instead of return false/ return true you should set a flag say NOT_PRIME or COMPOSITE to true when the number is divisible and then exit the loop without checking further using the break keyword.

Member Avatar
apegram
LINQ!
550 posts since Jan 2010
Reputation Points: 302 [?]
Q&As Helped to Solve: 137 [?]
Skill Endorsements: 10 [?]
Team Colleague
 
0
 

As another suggestion, what sense does it make to try to divide 11212 by 11211? That's what would happen as a result of your loop

for (int i = 2; i < number; i++)

Think about what would actually be a good lesser-number to test through. When could you be assured you have accounted for all possible divisors? (And in answering this, consider that when you divide something by one number, you have also accounted for the result as a divisor.)

In general, though, I'm a bit confused as to what the purpose of your program is. Is your intent to count all prime numbers between 2 and the value supplied by the parameter? If so, why are you returning a boolean? Is it your intention to actually return the list of all prime numbers within the given range? Or is it simply to test whether the given number is a prime number? If so, why is there a counter?

Member Avatar
verruckt24
Posting Shark
945 posts since Nov 2008
Reputation Points: 438 [?]
Q&As Helped to Solve: 97 [?]
Skill Endorsements: 0 [?]
 
0
 

Yes apegram has correctly pointed out that you don't seem to have the logic cut out well, do the dry coding make the necessary changes and post again. Elementary mistakes have already been pointed out so correct them as well.

Member Avatar
jwenting
duckman
8,330 posts since Nov 2004
Reputation Points: 1,399 [?]
Q&As Helped to Solve: 445 [?]
Skill Endorsements: 35 [?]
Infraction Points: 5
Team Colleague
 
0
 

You have made a lot of syntactical mistakes here.

if (number % i == 0)
	return false;
else
	return true;
if (false);

Do you really think the control will ever reach the line "if (false)" ? Also let's for a moment assume it does, will the control ever enter the statement in the if block ? What do you think are you doing evaluating if (false) ?

Not to mention the complete waste of code there.
Simply writing

return (number % i == 0);

would work just as well :)

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article