PrimeDirective Java Project Help

I was wondering if someone could check my code and try to debug it.
Everything seems correct when I run it, but I end up getting an unwanted value in the printed array when the code is run. The array should only print out the prime numbers for the given array in the code, but 33 gets printed along with the other numbers. I’ve tried removing 33 and placing another number like 66 to see if the code includes it in the printed array. 66 does not get printed, which is good, but I’m still curious as to why 33 is still getting printed.

Thank you so much, and appreciate the help.

You have a loop that only makes one iteration. Either that’s a bug, or you should not use a loop statement for it.

One way you could have found this is by writing out information about what your program is doing while it’s doing it. You could then view this information while comparing to what you would have done manually in that situation.

Linters will also tell you that your loop isn’t a loop.

I see. Do you think you could give me a hint as to what part of the loop is incorrect. I’m just following the instructions as per the project, so I’m trying to keep the same format.

Thanks.

I argue that you should know what each thing in your code does.
You might not understand it fully as a whole, but you should still understand the parts so well that you can execute the program yourself manually… in your head. Do that. What makes it not make a second iteration?

Or if you end up making a second iteration then you’ve shown that you’ve misunderstood what something does. You may now want to start adding print statements in your code to get a better idea of what’s happening. If you only want to know about your loop then that’s the only place you’d need to put print statements in.

If you have a debugger available that supports stepping then you can do that as well to step through it one instruction at a time. But, printing things out will do the same thing so this is barely useful tbh.

Before you do anything at all I suggest that you confirm that this is really happening. You can for example print something out from within the loop. If it runs more than once it’ll show more than once. (you may want to print something out before and/or after the loop as well so that you don’t mix up multiple calls on the whole method with individual iterations)

1 Like

I figured out the mistake after doing what you said. Tried to understand each line printed; figured out the reason it was being printed was because I had an unnecessary else statement with a return value. The “return true” value I had at the end of the for loop was enough.

Thank you so much!

If you have many tests that all must pass, then passing any test is not sufficient to show that all pass.

Failing any is however sufficient to show that not all pass.

tests = ....
return all(tests)

both all and any are functions that conceptually can stop when a case is found that is sufficient to determine the result, so all will stop as soon as something is false

(no those are probably not functions available by default in java, but you could make such functions, or use the idea of them when reasoning about your problem)

1 Like
isPrime n =
  let candidates = [2 ..] & takeWhile (< n)
      isDivisor d = (n `mod` d) == 0
  in  all (not . isDivisor) candidates

An implementation in another language that uses all.
It starts out with enumerating the numbers we care about, namely those 2 or above, keep up until but not including n itself.
Then define a test: does d evenly divide n?

Then the overall outcome can be defined as either
all (not . isDivisor) candidates
or, any:
(not . any) isDivisor candidates

(I left out special cases for numbers less than 2)

And then in keeping up with composing things to get bigger things:

onlyPrimes = filter isPrime

See a similarity? There’s one function representing iteration (filter/any/all/takeWhile) and another one to say what to do with one value (isPrime/isDivisor/< n) that is provided to the concept of iteration.

This topic was automatically closed 18 hours after the last reply. New replies are no longer allowed.