• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Prime Number Generator help

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello. My program is supposed to accept #test cases. For each test case, it must accept two integers as a range, then return all prime numbers within the range.
Currently, it prints odd numbers within the range.
So I suspect my problem is within the Boolean i'm using to test whether a number is prime or not.
I pulled the code for the Boolean from this site - http://www.mkyong.com/java/how-to-determine-a-prime-number-in-java/
Maybe I didn't quite understand it, or maybe that's not my real issue.
Any help would be greatly appreciated. Please, and thank you!
 
Marshal
Posts: 26697
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You changed the variable names from the code in that link, but you made one little error.

Here it is:

 
Marshal
Posts: 73284
332
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Beware: what happens if you try to work out whether 6917529027641081857L is prime or not? That is 2^61 + 2^62 + 1.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

albert abbene wrote:Any help would be greatly appreciated. Please, and thank you!


Another little wrinkle for you: Your program accepts ints for its range values, and yet uses a calculation method that takes a long. There's nothing intrinsically wrong with it, but it will slow things down unnecessarily, because arithmetic on longs takes longer (no pun intended). It also wastes space, because your ArrayList has to hold Longs when it could be holding Integers.

Winston
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
one thing i noticed when i was writing a similar method is it seems unnecessary to check for even numbers
 
lowercase baba
Posts: 12994
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You really only need to test (6*n + 1) and (6*n - 1) for all positive integer values of n, once you get 2 and 3 on your list. That eliminates another 33%.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here's the code which accepts two integers as range to print prime numbers,



Output:

Please enter the lower range :
1
Please enter the upper range :
50
Twin prime numbers within the given range.
(3,5) (5,7) (11,13) (17,19) (29,31) (41,43)
 
Campbell Ritchie
Marshal
Posts: 73284
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can see a serious problem in that code.
Also, why are you checking individual numbers for prime‑ness at all? There is a far better way to create a large range of prime numbers.
I think the link you provided isn't very good. There isn't enough explanation of what the different examples do, there are too many similar examples without enough information to distinguish them from one another, and there is another problem in the code shown there which you have fortunately not committed here.
 
You are HERE! The other map is obviously wrong. Better confirm with this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic