# Joe musk

Greenhorn
since Mar 28, 2021
Cows and Likes
Cows
0
In last 30 days
0
Total given
0
Likes
0
Received in last 30 days
0
Total given
5
Given in last 30 days
5
Scavenger Hunt
Ranch Hand Scavenger Hunt
Greenhorn Scavenger Hunt

## Recent posts by Joe musk

Campbell Ritchie wrote:There is a section in Urma Fusco and Mycroft Modern Java in Action (Manning, 2017, page 166) about “Divide only by prime numbers”. Haven't read it recently.

Thank you, I will look into it.
5 days ago

No. Didn't the link I gave you explain it all?

I beg your pardon, it appears I missed the hyperlink. I have gone through it now, and everything makes sense to me now.

so test two, then three and go up in steps of two.

It is incredible how such minute tweaks can cut the running time in half. I initially did think that we never needed to iterate through the even numbers, but I reckoned checking if the no. was odd even or not ,

would take about the same amount of time as

It never occurred to me that we could actually just increment the loop in steps of 2 and skip those even numbers altogether.

This has been very insightful. Learnt a lot.
Thank you for your patience.

^_^
1 week ago
Thank you for taking the time to answer in such great detail. It has helped me to  understand the concept clearly.
1 week ago
Thank you so much for the warm welcome and answering my doubt. I have understood exactly what was going wrong and so much more.

Get all the “real” code out into objects and you won't be tempted to use the keyword static

Could you please elaborate this? I understand I can put all the real code into methods outside the main method, and just call them within the main method. Is that what you referring to? I realise that objects are created  using constructors for different classes and they can store methods and variables. But in this particular case, how do I create objects which would replace the "misplaced code" in the main method?

You don't need to iterate every number > 2 to verify that a number is prime.

Is there a more optimised solution than to iterate until the root of the Number?

Thank you for your time and patience.
1 week ago
The problem is simple, I need to remove all primes from an arraylist(called al) and print it.

I created a second arraylist(called noPrime) to store all the non-primes, and then stored the noPrime's reference in al. The output was not correct so I printed it twice,

Once inside the solution() method and once in the main method AFTER solution() was already called.

Yet,

When I print the array al in the solution method, it outputs the correct answer.

When I print it in the main method, after calling the solution method, it outputs the incorrect answer.

Example:
Original = [3, 12, 13, 15]
first printing = [12, 15]  (from inside the solution method)
second printing = [3, 12, 13, 15]   (after the solution method is called in main method)

I suspect it is because noPrime has been declared within solution() and it gets destroyed once the solution() has returned. So al, which was holding the reference of noPrime, also gets destroyed? And it comes back to holding its original reference?

Why is this happening?

what does al = noPrime really do?

1 week ago