java program to calculate prime numbers

i need to make a program to calculate all the prime numbers from 2 unt

Page 1 of 1

5 Replies - 13310 Views - Last Post: 12 January 2009 - 06:25 PM Rate Topic: -----

#1 runner03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-January 09

java program to calculate prime numbers

Post icon  Posted 12 January 2009 - 01:31 PM

Scanner keyboard = new Scanner(System.in);
		int userNum;
		int remander = 0;
		System.out.print ("Enter a positive integer and find out all the prime numbers leading up to the number you chose: ");
		userNum = keyboard.nextInt();
		
		for(int lcv=2; lcv <= userNum; lcv++){
			  
		for (int lcv2=2;lcv2<=lcv-1;lcv2++){
			//IF STATEMENT
			if ((lcv2>=2)&&(lcv2<lcv)){
				remander=(lcv % lcv2);
			}
		
	
		if (remander !=0){
			System.out.print(lcv2);
		}
		}
			}
		}                                  // i need to calculate all the prime numbers between 2 and whatever the user inputs\


Mod Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

This post has been edited by PsychoCoder: 12 January 2009 - 01:36 PM


Is This A Good Question/Topic? 0
  • +

Replies To: java program to calculate prime numbers

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: java program to calculate prime numbers

Posted 12 January 2009 - 01:37 PM

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:

  • Post the code you're having problems with (DONE)
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field (or as a comment in your code)

Was This Post Helpful? 0
  • +
  • -

#3 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: java program to calculate prime numbers

Posted 12 January 2009 - 01:53 PM

check out the sieve of eratosthenes algorithm, its the fastest algorithm to generate prime numbers and its also really simple as well.. i think you might find it in the snippets section, if not then you can google it
Was This Post Helpful? 0
  • +
  • -

#4 runner03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-January 09

Re: java program to calculate prime numbers

Posted 12 January 2009 - 05:01 PM

View PostPsychoCoder, on 12 Jan, 2009 - 12:37 PM, said:

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
  • Post the code you're having problems with (DONE)
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field (or as a comment in your code)







View PostPsychoCoder, on 12 Jan, 2009 - 12:37 PM, said:

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
  • Post the code you're having problems with (DONE)
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field (or as a comment in your code)




I do not get any syntax errors just logic errors.... I have too for loops the first one just a counter from 2 to userInput.. the second for loop is a second counter from 2 to the first counter.. i believe where my problems are, is in the if statement inside of the for loop...i cant figure out what that should be.. and my program is in no way printing the prime numbers

This post has been edited by runner03: 12 January 2009 - 04:59 PM

Was This Post Helpful? 0
  • +
  • -

#5 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: java program to calculate prime numbers

Posted 12 January 2009 - 05:09 PM

Why do you have nested for loop to print out the prime numbers?

Just have one loop which loops from 2 until the user entered number and call a method to check if that number is prime, if it is print if not don't print.

Or else see: Sieve of Eratosthenes as mentioned above to calculate the prime numbers in your list
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: java program to calculate prime numbers

Posted 12 January 2009 - 06:25 PM

for performance reason you can have your loop of lcv2 go to sqrt(lcv) not lcv-1
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1