wood burning stoves 2.0*
The moose likes Programming Diversions and the fly likes Java Code puzzle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Other » Programming Diversions
Bookmark "Java Code puzzle" Watch "Java Code puzzle" New topic
Author

Java Code puzzle

Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Algorithm for finding Prime Numbers?


The Best way to predict your future is to create it - Every great individual common man
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

No Solution so far!!!
Gabriel Claramunt
Ranch Hand

Joined: May 26, 2007
Posts: 375
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!


Gabriel
Software Surgeon
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
Think about this from a logical point of view...

What makes a number prime? Only divisible by itself and 1.
How can I tell if a number is only divisible by itself and 1?

This could be done in many ways.... have you started on it at all?


SCJA
~Currently preparing for SCJP6
Paul Yule
Ranch Hand

Joined: May 12, 2008
Posts: 229
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
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
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
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
Finding primes is a well known problem without an easy solution.
If the number is not too big, you can use Eratosthenes Sieve.
Maris Orbidans
Ranch Hand

Joined: Mar 08, 2004
Posts: 149
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
I fixed your code

Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
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
This was my take at generating the sieve... I guess I overcomplicated a little

Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
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
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
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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
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
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
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.
Brandon Bernie
Greenhorn

Joined: Nov 02, 2007
Posts: 9


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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Code puzzle