• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sieve Coding, not pairing in the output

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having an issue with the final output. I seem to have most of the code right, or so I think, well I guess I wouldn't be posting this if I didn't. Anyways. when I compile the code, it goes through fine in the compiler. However after entering the boundaries for the lower an upper it doesn't produce the prime pairs between the two boundaries that the user sets. I have tried changing some of my integers that i initialized no luck. I looked at the last section of . However I can't seem to see where it has gone wrong there. I have included the entire code, plus the section again that I think is wrong. I have also posted the final output of what it looks like and what else should be included to make it easier to see what I am missing. Basically it keeps giving me zero primes even when the boundary is 100 and 200, or so and so forth.




This is the section that I think is wrong to be honest, since it is the final output of the algorithm.



This is what i get when I actually run the program. However in the section where I have added *****, this is where it should put the prime pairs in.
Please enter a lower boundary and an upper boundary and I will print all of the sexy prime pairs between those boundaries.
Please enter the lower boundary (between 1 and 50000): 200
Please enter the upper boundary (between 1 and 50000): 600
Here are all of the sexy prime pairs in the range 200 to 600, one pair per line:
********
There were 0 sexy pairs displayed between 200 and 600

Any input or thoughts would be awesome. I am still working on another code for my that I had issues with, however I had to go out of the country for a few days to take care of some work.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please break up long lines when you post code. I have edited your post for you. Thanks
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karl Beaudry wrote:


This is not the sieve of Eratosthenes algorithm. (See https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes).
You are using the modulus operator which is both expensive and not part of the algorithm, and you are using sqrt() inside the loop which is also expensive.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't use \n after printf. Use %n instead. In fact you should only use \n when you have been told to supply the LF character.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And don't do this:

primes[i] already evaluates to true or false so adding "== true" is redundant. Also, you can get a hard-to-find bug if you inadvertently write "= true" instead. If you choose a better name for your array, the code will read more naturally:

And this:

can be written as (assuming you change the array name to isPrime):

When you declare boolean[] isPrime = new boolean[50000], all elements of the array isPrime are set to false by default, so there's no need to set the first two elements to false again. Processing of the array can start at the third element, so you initialize i to 2. There's no rule that says the loop index variable must start at 0 all the time. In fact, you can have two loop index variables:
 
Karl Beaudry
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone for the posts. i dont remember anything about isPrimes in my lectures but I will double check that, thanks, it makes sense changing that and last time I created an assignment with something that wasn't mentioned I got smacked for it(figuratively smacked). I wrote an array when I wasn't suppose too. Anyways, Im still working to find out why it wont print out my lines of pair in the output, continues to give me zero outputs for the actual pairs between the given ranges specified by the user.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karl Beaudry wrote:Thanks everyone for the posts. i dont remember anything about isPrimes in my lectures but I will double check that, thanks, it makes sense changing that and last time I created an assignment with something that wasn't mentioned I got smacked for it(figuratively smacked). I wrote an array when I wasn't suppose too. Anyways, Im still working to find out why it wont print out my lines of pair in the output, continues to give me zero outputs for the actual pairs between the given ranges specified by the user.


I suspect your array of primes is not correct. I'd check that first. Start with a small range first and print out the list.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karl Beaudry wrote:Thanks everyone for the posts. i dont remember anything about isPrimes in my lectures but I will double check that, thanks, it makes sense changing that and last time I created an assignment with something that wasn't mentioned I got smacked for it(figuratively smacked). I wrote an array when I wasn't suppose too. Anyways, Im still working to find out why it wont print out my lines of pair in the output, continues to give me zero outputs for the actual pairs between the given ranges specified by the user.


I would imagine that the professor would give you enough freedom to create your own variable names at least. My suggestion is simply to change the name of your boolean array from primes to isPrime because the rest of the code will read more naturally like plain English. But if you have to specifically use the name primes, that's fine. I think it's a ridiculous restriction though, if indeed it is a restriction.
 
Karl Beaudry
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah thats what I am looking at. Also I noticed that in the showPrimes() i have the int count = 0. Which is what is showing how many prime pairs there are. However I don't have the count set anywhere else, so how is it going to grab the primes? With it set to 0, in the output it sets it to 0 as well, or any other number I change it too. Tells me I have one more thing I need to figure out here that isn't set right.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I ran this and it seemed to work for ranges 1-100 and 100-200.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karl Beaudry wrote:


This could be your problem. These three calls are enclosed in braces but are not part of any method.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

Karl Beaudry wrote:


This could be your problem. These three calls are enclosed in braces but are not part of any method.



You don't see it often and I don't know if Kevin Karl even did that on purpose but it's an initialization block, except this is an instance initialization block. You might be more familiar with a static initialization block, which has the static keyword in front of the opening brace. The instance initialization block will run every time a new instance is created. It probably is the problem since the code in this block will be executed before the code in the constructor.
 
Karl Beaudry
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So when I run it from 1-42, i get nothing, shows me 0 and not output pairs. When I enter in 1-100 or 100-200 nothing, and so on and so forth. I tried removing.



However when I remove it, it compiles, but when i execute the script it just returns me back to command line entry. Even if I move it around in the code, same issue gives me 0, and no output pairs. Still working this at every angle I can think of.

 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Those calls need to be in a method. Probably the best place is a method called by main().

See our page Main Is A Pain. BTW, the variable name i in main() is not a very good name. The name sieve would be better.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ahem. I did post the tweaked code that works.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:ahem. I did post the tweaked code that works.


Ah, sorry didn't notice it. The lumber (code that works) was buried in the shavings (code that has been commented out). Let that be a lesson for you, too, young man
 
Karl Beaudry
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see what you all were saying there. Wierd cause i swore I moved that part of the code around, removed braces, and everything adding it. But I must of had something off at the time to get there errors I did when compiling. Thanks for the help though. Also quick question looking at your code Carey, Why did you change the number to 50_001 and 50_000. Just wonder why you put the _.

 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karl Beaudry wrote: Why did you change the number to 50_001 and 50_000. Just wonder why you put the _.


See http://docs.oracle.com/javase/8/docs/technotes/guides/language/underscores-literals.html
 
Karl Beaudry
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That makes sense, plus the examples of valid invalid help to see exactly what is expected for implementing underscore in numerical numbers. Appreciate the link. Thanks.
 
Your buns are mine! But you can have this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic