• Post Reply Bookmark Topic Watch Topic
  • New Topic

Printing Prime numbers  RSS feed

 
Sri Harsha Bolisetti
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I am just starting java and coding, I am trying to write a program which prints prime numbers but got stuck. I will be greatful if someone can help.
Thank You.



My apologies for being so vague
I'm trying to solve this question

Problem statement
Given a number, verify whether its prime or not

Input
First line of input will contain a number N = number of test cases. Next N lines will contain number n as test case where 0<=n<=1000000000

Output
For each input case, print "PRIME" if the number is prime, else print "NOT PRIME" (quotes for clarity)

and the output I am getting is "PRIME"
SO i was so confused.
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got stuck with what exactly?

Does it compile?
Does it work at all?
Does it do what you want?

Please be more specific with what help you are asking for.
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to rename your variable "temp" to something meaningful would help you to spot the problem.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Didn't try to compile and check how it works, but...
In line 11 you increment temp. Why? That value will never be used as it will be overriden in line 9 in next execution of a loop.
It looks like temp is supposed to be holding number of divisors. That could be ok. But it also holds results of division (modulo). You can't store two different values in one variable.

Several hints:
- You don't need to check if a number divides by even number (except for 2).
- You don't need to count how many divisors a number has. If it divides by anything other than 1 or itself it is not prime.
- Also if a number is less than 2 you should immediately return false without even trying to execute a loop.

My advice is StopCoding
Figure out how to solve this on paper. How would YOU check if a number is prime?
When you have the instructions then and only then try to write a code.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
+1 to what Pawel said: StopCoding
Ill also add one more point to his list. Only odd numbers can be prime!
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:Ill also add one more point to his list. Only odd numbers can be prime!

1. I already said that.
2. This is not true . The number 2 is not odd and it is prime .
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:
1. I already said that.
2. This is not true . The number 2 is not odd and it is prime .

+1 Meant I agreed to what you (already) said
#2. You got me there
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

More complaints, I am afraid. Poor style. Don't write if (something) return true; else return false;
It is return something;
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another hint...

you don't have to divide N by every number up to N. You don't even have to go halfway. for anything below 100, you only have to check up to 10. for numbers up to 121, you only have to check up to 11.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest you find about the Sieve of Eratosthenes which is a technique for working out prime numbers.
Don't print 1; it does not count as a prime number.
 
Sri Harsha Bolisetti
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You very much guys ! I got it
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done Please show us what you have achieved.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!