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!

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

Joined: Nov 07, 2008
Posts: 488

posted

0

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.

Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488

posted

0

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

Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488

posted

0

Bah, I got it.... the numbers always meet at the square root of the number.

sq 100 = 10
sq 273529 = 523

I'll update my code later... I don't know the java sytax for square root. ;)

Gabriel Claramunt
Ranch Hand

Joined: May 26, 2007
Posts: 375

posted

0

Finding primes is a well known problem without an easy solution.
If the number is not too big, you can use Eratosthenes Sieve.

Brian, your code isn't correct. It wont even compile because primitives cannot be used as parametric types.

Maris Orbidans
Ranch Hand

Joined: Mar 08, 2004
Posts: 149

posted

0

I fixed your code

Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488

posted

0

Thanks, it's been a while since I've used any Java

So.... does it work?

Gabriel Claramunt
Ranch Hand

Joined: May 26, 2007
Posts: 375

posted

0

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

Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488

posted

0

No point using 3 loops when 1 will do I always say

Nice job though!

Gabriel Claramunt
Ranch Hand

Joined: May 26, 2007
Posts: 375

posted

0

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 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

Gabriel Claramunt
Ranch Hand

Joined: May 26, 2007
Posts: 375

posted

0

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...

Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296

posted

0

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 because should be 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

Joined: Jan 17, 2006
Posts: 1296

posted

0

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 because should be 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.

Its decently fast. I am working on a new algorithm that takes into account some number theoretic properties. I used this for solving problems involving small prime numbers over at projecteuler.net.

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.