• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Runtime Error

 
Ranch Hand
Posts: 43
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to solve Prime Generator problem in java.

upto input 10^9 code works fine(Test1) but input greater than 10^9 it gives runtime error(Test2).What is the reason of runtime error?
 
Bartender
Posts: 6140
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try enclosing the main body in a try/catch block and print the exception stack trace. Always post the complete error message here as text, not a screen snapshot or a link.
 
Carey Brown
Bartender
Posts: 6140
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be helpful to use meaningful variable names for t, n, and m. And perhaps a prompt to the user as to what the program is expecting.
 
Shubham Rai
Ranch Hand
Posts: 43
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
             999900000 1000000000
             999900000 1000000000


          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.

is not working.
 
Marshal
Posts: 65467
249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

[edit]Correct an ' HTML tag.
 
Saloon Keeper
Posts: 3462
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apart from that:

I ran the code in NetBeans a couple of times, looks okay, and for the input you gave the code runs without a hiccup, giving twice the same, truncated, output.
 
Carey Brown
Bartender
Posts: 6140
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 13730
228
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Marshal
Posts: 65467
249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are going to write a Sieve that large, a BitSet is probably the most space‑economical data structure, isn't it?
 
Carey Brown
Bartender
Posts: 6140
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Good night. Drive safely. Here's a tiny ad for the road:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!