• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Finding prime numbers

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am stuck and looking for some guidance with this problem. I have researched and found solutions to this problem online but none of them work for me. I need to implement a single method that is going to have an int x passed to it. The method will need to check all values between 2 and x and then print all the prime numbers that fall between this range. For example

calcPrimeNumbers(28) would output
The primes less than or equal to 28 are:
2 3 5 7 11 13 17 19 23



I have tried to implement methods I found into my problem but they don't work. Most of the methods I found use two methods to solve or just use a boolean to return if the value passed is prime or not.

Here is what I have so far, thank you very much for any help offered, it is greatly appreciated.

 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The inner condition is wrong...



If you find a divisor, then obviously, it isn't a prime... and yes, you need to check the next count. However, just incrementing the count isn't going to do it, you need to also jump to the next iteration of the while loop, in order to process the count.

If you don't find a divisor, with one iteration of the inner loop, that doesn't mean that it is a prime -- as you do in your condition. You need to fail the test for all iterations of the inner loop before it is considered a prime.

Henry
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Finding all the primes up to some input value would be a natural fit with using the Sieve of Eratosthenes. If you Google that you should find lots of references online. There was also a thread about it a couple of weeks ago right here in this forum.
 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Same question here, i'm also stuck in this to find the prime nos.
What i'm assuming is that if the no. provided is greater than 1 than '2' itseld will be stored in a list to be displayed later to the user as a series of prime and if the no. given is either 0 or 1 then the list is null and hence no prime nos exists.

Following are the code:-


I got stuck in the logic. Right now i'm passing 10 in the construtor so the looping will be done as
2,3,4,5,6,7,8,9,10

The prime nos in the series are

2,3,5,7



But please give me some hint how can i check in the if condition while iterating in for loop that the current index value is a prime or not and then put it in the list.

Thanks alot.
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will be interested in reading through this past forum thread as well.

https://coderanch.com/t/516856/java/java/Sieve-Eratosthenes#2343984
 
Vinod Vinu
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You will be interested in reading through this past forum thread as well.

https://coderanch.com/t/516856/java/java/Sieve-Eratosthenes#2343984

said by Bobby

Just provide in your simple language. Don't post such complex links.
 
Marshal
Posts: 80673
478
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bobby Smallman wrote:. . . past forum thread as well. . . .

Since there was a lot more than only Sieve of Eratosthenes in that discussion (I remember it well ), it is worth doing a search of javaRanch.
 
Bobby Smallman
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since it sounds like you want to avoid more complex code like the Sieve. A simple yet efficient way to do it is something along the lines of this



And if your end goal was providing a list of prime numbers and not just determining if a number was prime, you can clearly use that isPrime method that way as well. I got in the habit of breaking out small chunks of code like this for Project Euler.



The Sieve can speed things up but as you have pointed out it can be overwhelming when you are new to thinking about this kind of logic. It is important to note in the isPrime method there that it only goes to the sqrt of the number looking for divisors. That is a significant speed increase at least over iterating all the way through the number.
 
Campbell Ritchie
Marshal
Posts: 80673
478
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bobby Smallman wrote:. . . A simple yet efficient way . . .

The Sieve is hardly more complicated, and much more efficient; that code will run in quadratic time and looks inefficient to me.

I suspect the Sieve is quadratic complexity, too, but still much faster.
 
Bobby Smallman
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Bobby Smallman wrote:. . . A simple yet efficient way . . .

The Sieve is hardly more complicated, and much more efficient; that code will run in quadratic time and looks inefficient to me.

I suspect the Sieve is quadratic complexity, too, but still much faster.



The Sieve is absolutely a significantly more efficient way to go. After already suggesting they research the Sieve further and read through a past post which showed the general outline of the Sieve and some pitfalls/optimizations which are helpful there was a comment that it was complex and to keep it simple. The method in my last post is an implementation of a non-Sieve method of finding prime numbers, I posted it as a stepping stone in their logic, because it is closer to their current code, to help them build a foundation in that sort of logic in order to ultimately be able to revisit their research in the Sieve.

With that being said, hopefully it is clear to the original poster that the Sieve of Eratosthenes make prime finding happy.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You guys should realize that by now, the original poster has copied the inefficient code you gave, copy-pasted it, and handed it in for his homework assignment. He won't be back, and he didn't learn anything. Please don't post working code examples, especially in this kind of thread; remember the adage regarding lifelong piscatorial pursuits.
 
Campbell Ritchie
Marshal
Posts: 80673
478
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ernest Friedman-Hill wrote:. . . handed it in for his homework assignment. . . .

You don't do that round our way at Teesside. Our anti-plagiarism software would find CodeRanch in a few seconds and the entire assignment would receive a mark of 0
 
Bobby Smallman
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My bad At least I can be comforted by the knowledge that if that wonderfully intelligent/moral/scrupulous/Java loving/Insert other sarcastic adjectives here, did just copy/paste. He will be stuck with terribly weak code and no understanding of what should have been their goal of the thread. Won't happen again guys!
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bobby Smallman wrote:My bad ... Won't happen again guys!



On the other hand, we love people who want to help others, so good show in any event!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic