In the above code :
t=no of test cases n=min range (greater than or equal to which prime no is to be printed)
m=max range(less than or equal to which prime no is to be printed)
for input: 2
when i run this code on
1.Eclipse ide-: My program terminates without any output
2.Codechef ide-: My program runs successfully but getting different output for same input.
3.Ideone ide-: My program gives runtime error.
Beware of online “IDE”s. I don't use them; I tried some a long time ago and found they were not executing the program in accordance with the requirements of the Java® Language Specification. I don';t know whether they are any better now. If you are going to develop any code, only use Java® installed on your own computer, with Eclipse or a decent text editor.
Please start by taking note of what you have been told about variable names. Also format your code. Things like (t-->0) are really difficult to understand. Use a bit of space so you get (t-- > 0). Or better still, move the -- operator into the body of the loop. What's more, you need to indent that loop correctly. You cannot see where the clear() call appears, nor how often you are calling clear().
Then please explain what algorithm you are using. That is not at all clear. I can't see how you are working out prime‑ness. Nor why you are using <= so often. Nor why you are using a Map. Nor why you are using Longs when the question says the numbers will all be in the range of an int.
I tried it in Eclipse and it didn't give me any output. Then I inserted a new-line for every 10 numbers and everything magically appeared.
Interesting algorithm, where did you get it? I compared it to a highly optimized Sieve program that I have and yours was fast but only for the given range. I tried to set the range to match my 2 --> N algorithm and I couldn't get a speed because the Map caused a stack overflow. I may try to implement a class to encapsulate that without causing an overflow. I also noticed the algorithm could be significantly improved by not setting the map for the same even numbers over and over.
If I understand the program correctly, it looks like it's trying to map numbers that are multiples of other numbers. Lines 24-27 basically prints out any number that isn't mapped. Seems to me just a more inefficient variation of the Sieve of Eratosthenes
Junilu Lacar wrote:If I understand the program correctly, it looks like it's trying to map numbers that are multiples of other numbers. Lines 24-27 basically prints out any number that isn't mapped. Seems to me just a more inefficient variation of the Sieve of Eratosthenes
It is the Sieve but starting at 'n'. Could be optimized by making divisible by 2 a special case.