• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Java Code puzzle

 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Algorithm for finding Prime Numbers?
 
Shaan Shar
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No Solution so far!!!
 
Ranch Hand
Posts: 376
Scala Monad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 376
Scala Monad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Finding primes is a well known problem without an easy solution.
If the number is not too big, you can use Eratosthenes Sieve.
 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brian, your code isn't correct. It wont even compile because primitives cannot be used as parametric types.

 
Maris Orbidans
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I fixed your code

 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, it's been a while since I've used any Java

So.... does it work?
 
Gabriel Claramunt
Ranch Hand
Posts: 376
Scala Monad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This was my take at generating the sieve... I guess I overcomplicated a little

 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No point using 3 loops when 1 will do I always say

Nice job though!
 
Gabriel Claramunt
Ranch Hand
Posts: 376
Scala Monad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Gabriel Claramunt
Ranch Hand
Posts: 376
Scala Monad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
And tomorrow is the circus! We can go to the circus! I love the circus! We can take this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic