Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Sorting Prime Numbers
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
ds_matrix




PostPosted: Sat Dec 15, 2007 7:06 pm   Post subject: Sorting Prime Numbers

Hi,

I just had an exam where I had to use a boolean query to calculate and list the first 10 prime numbers (1 and above)

I honestly had no idea how to do this and was wondering if anyone could tell me how?

Thanks
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Sat Dec 15, 2007 7:18 pm   Post subject: Re: Sorting Prime Numbers

ds_matrix @ Sat Dec 15, 2007 7:06 pm wrote:
list the first 10 prime numbers (1 and above)

it's a trick question, 1 is not a prime Wink
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
ds_matrix




PostPosted: Sat Dec 15, 2007 8:37 pm   Post subject: Re: Sorting Prime Numbers

Tony @ Sat Dec 15, 2007 7:18 pm wrote:
ds_matrix @ Sat Dec 15, 2007 7:06 pm wrote:
list the first 10 prime numbers (1 and above)

it's a trick question, 1 is not a prime Wink


sorry meant to say starting from 2
Tony




PostPosted: Sat Dec 15, 2007 8:48 pm   Post subject: RE:Sorting Prime Numbers

can you post the actual question? your wording is ambigious.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
PaulButler




PostPosted: Sat Dec 15, 2007 9:12 pm   Post subject: RE:Sorting Prime Numbers

There are a few simple ways to find prime numbers. The most obvious way is to pick a number and check if any number between 1 and that number are factors of that number. If they are, the number isn't prime. You can make this more efficient by only testing for factors up to the square root of the number and only using odd numbers (making an exception to check for 2).

The second method is called the Sieve of Eratosthenes. It's a bit better when you are looking for multiple prime numbers, especially if they are low primes. If you want to find prime numbers up to n, you make a bit array of n elements all set to false. Then start with 2 and set every second bit except 2 to true. Then set every third bit except 3 to true. Then skip 4, because the fourth element of the array is true. Keep setting every xth element after x, if the xth element is false. If my explanation is bad, try wikipedias: http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes .
Nick




PostPosted: Sat Dec 15, 2007 9:26 pm   Post subject: RE:Sorting Prime Numbers

so you want just the first ten upto number n?
if so then wouldn't this be the same for every number past 29? the first 10 are:
2
3
5
7
11
13
17
19
23
29
CodeMonkey2000




PostPosted: Sat Dec 15, 2007 9:40 pm   Post subject: RE:Sorting Prime Numbers

momop, I'm pretty sure that's not what he/she wanted.
Tony




PostPosted: Sat Dec 15, 2007 9:51 pm   Post subject: RE:Sorting Prime Numbers

a boolean query is just an if statement... you could totally check if any given number is in this hard-coded list.

or, since we know that every number above 1 is either a prime, or has a prime factor, we could be fancy and check if one of the hard-coded values is a factor or not. Which is actually kind of how Sieve of Eratosthenes works.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: