# Java Code puzzle

posted 7 years ago

Hmmm.... I saw *exactly* the same problem in an internal coding contest from some consulting company... coincidence? ;)

Have you given any though to the possible solution? I think if you find a mapping from coordinates to primes, you're done!

Have you given any though to the possible solution? I think if you find a mapping from coordinates to primes, you're done!

Gabriel

Software Surgeon

Brian Legg

Ranch Hand

Posts: 488

Paul Yule

Ranch Hand

Posts: 229

posted 7 years ago

I actually seem to remember this originally being a different problem. There were prime numbers in a circularish pattern and you had to be able to get the prime based on coordinants. Either way if you have anything on either of the problems you posted I'd love to see your thoughts up to this point.

Brian Legg

Ranch Hand

Posts: 488

posted 7 years ago

Ok Paul, just for you

Here is my approach:

I'm at work and I haven't done any Java in a while so I can't test this for accuracy or syntax errors. The basic concept is that you loop through each of the numbers in your array and see if any of them are divisible by a number between 2 and 10% of our number we are testing. Why 10%? Because after you go through 10% of the possible numbers you will start repeating the lower ones you already checked, like this:

100 is divisible by:

1 x 100

2 x 50

4 x 25

5 x 20

10 x 10

You can see that if we check any numbers higher than 10 the amount of times it can go into 100 will be less than 10, and we already checked all those so we can stop when we reach 10% of any number. If a divisor goes into our prime evenly then it's not prime. We set the flag to false and exit to our next test subject. Each one that passes the test is added to a new array and eventually returned containing just prime numbers.

Let me know if my logic is off or if the code doesn't work.

Here is my approach:

I'm at work and I haven't done any Java in a while so I can't test this for accuracy or syntax errors. The basic concept is that you loop through each of the numbers in your array and see if any of them are divisible by a number between 2 and 10% of our number we are testing. Why 10%? Because after you go through 10% of the possible numbers you will start repeating the lower ones you already checked, like this:

100 is divisible by:

1 x 100

2 x 50

4 x 25

5 x 20

10 x 10

You can see that if we check any numbers higher than 10 the amount of times it can go into 100 will be less than 10, and we already checked all those so we can stop when we reach 10% of any number. If a divisor goes into our prime evenly then it's not prime. We set the flag to false and exit to our next test subject. Each one that passes the test is added to a new array and eventually returned containing just prime numbers.

Let me know if my logic is off or if the code doesn't work.

SCJA

~Currently preparing for SCJP6

Brian Legg

Ranch Hand

Posts: 488

posted 7 years ago

Now that I have thought about it some more I think this could be even more efficient. I think it will work as is but could be done differently to return the answer much faster. 10% is guaranteed for higher numbers but doesn't work for lower ones.... also 10% is a lot more numbers than is neccassary to check on higher numbers.

Now I need to go figure out the formula for where the divisors meet on a given number.

For anyone testing my code as it is now, only pass in large numbers :P

Now I need to go figure out the formula for where the divisors meet on a given number.

For anyone testing my code as it is now, only pass in large numbers :P

SCJA

~Currently preparing for SCJP6

Brian Legg

Ranch Hand

Posts: 488

posted 7 years ago

Finding primes is a well known problem without an easy solution.

If the number is not too big, you can use Eratosthenes Sieve.

If the number is not too big, you can use Eratosthenes Sieve.

Gabriel

Software Surgeon

Maris Orbidans

Ranch Hand

Posts: 149

Brian Legg

Ranch Hand

Posts: 488

posted 7 years ago

This was my take at generating the sieve... I guess I overcomplicated a little

Gabriel

Software Surgeon

Brian Legg

Ranch Hand

Posts: 488

posted 7 years ago

Yep, 3 loops is too much.. I'm not convinced of starting with 2 either, but anyway... I wrote the code in a hurry too

Let me rewrite as an opportunity to get minimalistic on Java

Brian Legg wrote:No point using 3 loops when 1 will do I always say

Nice job though!

Yep, 3 loops is too much.. I'm not convinced of starting with 2 either, but anyway... I wrote the code in a hurry too

Let me rewrite as an opportunity to get minimalistic on Java

Gabriel

Software Surgeon

Garrett Rowe

Ranch Hand

Posts: 1296

posted 7 years ago

No this is over complicating it. Here is a sieve inspired by this paper. (Look ma' no arrays!)

Gabriel Claramunt wrote:I guess I overcomplicated a little

No this is over complicating it. Here is a sieve inspired by this paper. (Look ma' no arrays!)

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter

posted 7 years ago

On the other hand, I've used boolean primitive arrays because

Garrett Rowe wrote:Gabriel Claramunt wrote:I guess I overcomplicated a little

No this is over complicating it. Here is a sieve inspired by this paper. (Look ma' no arrays!)

On the other hand, I've used boolean primitive arrays because

*should*be pretty fast. If I recall correctly I did a fancy implementation with collections but it was slow...

Gabriel

Software Surgeon

Garrett Rowe

Ranch Hand

Posts: 1296

posted 7 years ago

I'm quite sure that that method is probably slower than one that uses arrays (I haven't timed it though), I only wrote and shared it as an academic curiosity.

I thought the paper was interesting because the naive (and elegant) SofE is one of those examples that always comes up in Haskell beginners tutorials with no disclaimer that it is likely wildly inefficient and there are better, although not as aesthetically pleasing, ways if implementing it.
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter

Gabriel Claramunt wrote:Garrett Rowe wrote:Gabriel Claramunt wrote:I guess I overcomplicated a little

No this is over complicating it. Here is a sieve inspired by this paper. (Look ma' no arrays!)

On the other hand, I've used boolean primitive arrays becauseshouldbe pretty fast. If I recall correctly I did a fancy implementation with collections but it was slow...

I'm quite sure that that method is probably slower than one that uses arrays (I haven't timed it though), I only wrote and shared it as an academic curiosity.

I thought the paper was interesting because the naive (and elegant) SofE is one of those examples that always comes up in Haskell beginners tutorials with no disclaimer that it is likely wildly inefficient and there are better, although not as aesthetically pleasing, ways if implementing it.

Garrett Rowe

Ranch Hand

Posts: 1296

posted 7 years ago

So for completeness sake, I did finally run some timing tests and the version with boolean[] was ~ 40x faster than the HashMap implementation.
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter

Garrett Rowe wrote:Gabriel Claramunt wrote:Garrett Rowe wrote:Gabriel Claramunt wrote:I guess I overcomplicated a little

No this is over complicating it. Here is a sieve inspired by this paper. (Look ma' no arrays!)

On the other hand, I've used boolean primitive arrays becauseshouldbe pretty fast. If I recall correctly I did a fancy implementation with collections but it was slow...

I'm quite sure that that method is probably slower than one that uses arrays (I haven't timed it though)

So for completeness sake, I did finally run some timing tests and the version with boolean[] was ~ 40x faster than the HashMap implementation.