Page 2 of 2 FirstFirst 12
Results 11 to 13 of 13

Thread: Java Prime Number Finder...

  1. #11
    Poster
    Join Date
    Jan 2004
    Posts
    3,073
    Originally posted by Lamsey@31 March 2004 - 14:28
    Great, now you have a program with no output. Very useful.

    I really wanted to know if 95,247 was prime or not
    Well, the point of removing the output was that it now shows you just how long it takes to calculate them, not to display them...

    Besides, if you wanted to show them, you could always output to text file...
    On a given day or given circumstance, you think you have a limit.
    And you then go for this limit and you touch this limit and you think "Ok, this is the limit".
    As soon as you touch this limit, something happens and you suddenly can go a little bit further.
    With your mind power, your determination, your instinct and the experience as well, you can fly very high.

    - Ayrton Senna, R.I.P.

  2. Software & Hardware   -   #12
    Poster
    Join Date
    Jan 2004
    Posts
    3,073
    This code outputs the results to a text file, only slowing the program down by about 0.15 seconds for all primes < 100000

    Code:
    import java.util.*;
    import java.io.*;
    
    public class lamsey
    {
     &nbsp;public static void main&#40;String&#91;&#93; args&#41; throws Exception
     &nbsp;{
     &nbsp; &nbsp;File outFile = new File&#40;&#34;primes.txt&#34;&#41;;
    	FileOutputStream outFileStream = new FileOutputStream&#40;outFile&#41;;
    	PrintWriter outStream = new PrintWriter&#40;outFileStream&#41;;
     &nbsp; &nbsp;
     &nbsp; &nbsp;boolean prime;
     &nbsp; &nbsp;int primes=0;
     &nbsp; &nbsp;double start_time=System.currentTimeMillis&#40;&#41;;
     &nbsp; &nbsp;System.out.println&#40;&#34;Working...please wait&#34;&#41;;
     &nbsp; &nbsp;for &#40;int number=1;number&#60;100000;number+=2&#41;
     &nbsp; &nbsp;{
     &nbsp; &nbsp; &nbsp;prime=true;
     &nbsp; &nbsp; &nbsp;for &#40;int x=3;x&#60;=number/2;x+=2&#41;
     &nbsp; &nbsp; &nbsp;{
     &nbsp; &nbsp; &nbsp; &nbsp;if&#40;number%x==0&#41;
     &nbsp; &nbsp; &nbsp; &nbsp;{
     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;prime=false;
     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;
     &nbsp; &nbsp; &nbsp; &nbsp;}
     &nbsp; &nbsp; &nbsp;}
     &nbsp; &nbsp; &nbsp;if&#40;prime&#41;
     &nbsp; &nbsp; &nbsp;{
     &nbsp; &nbsp; &nbsp; &nbsp;primes++;
     &nbsp; &nbsp; &nbsp; &nbsp;outStream.println&#40;number+&#34; is a prime number&#34;&#41;;
     &nbsp; &nbsp; &nbsp;}
     &nbsp; &nbsp;}
     &nbsp; &nbsp;double end_time = System.currentTimeMillis&#40;&#41;;
     &nbsp; &nbsp;System.out.println&#40;&#34;Time taken = &#34;+&#40;end_time - start_time&#41;&#41;;
     &nbsp; &nbsp;System.out.println&#40;&#34;Primes found = &#34;+primes&#41;;
     &nbsp;}
    }
    On a given day or given circumstance, you think you have a limit.
    And you then go for this limit and you touch this limit and you think &quot;Ok, this is the limit&quot;.
    As soon as you touch this limit, something happens and you suddenly can go a little bit further.
    With your mind power, your determination, your instinct and the experience as well, you can fly very high.

    - Ayrton Senna, R.I.P.

  3. Software & Hardware   -   #13
    Poster
    Join Date
    Jan 2004
    Posts
    3,073
    This code is a little bit quicker than mine and Lamesy&#39;s attempts so far:

    Code:
    import java.lang.*;
    
    class sieve
    {
    	public static void main &#40;String &#91;&#93; args&#41;
     &nbsp; &nbsp;{
     &nbsp;final int max = 100000;
     &nbsp; &nbsp; &nbsp; &nbsp;final int startat = 1;
     &nbsp; &nbsp; &nbsp; &nbsp;int n = max;
     &nbsp; &nbsp; &nbsp; &nbsp;double limit;
     &nbsp; &nbsp; &nbsp; &nbsp;double start_time=0, end_time=0; 
     &nbsp; &nbsp; &nbsp; &nbsp;int primesfound=0;
     &nbsp; &nbsp; &nbsp; &nbsp;start_time = System.currentTimeMillis&#40;&#41;;
     &nbsp; &nbsp; &nbsp;
     &nbsp; &nbsp; &nbsp; &nbsp;boolean numberpool &#91;&#93; = new boolean &#91;n&#93;;
     &nbsp; &nbsp; &nbsp; &nbsp;for&#40;int i=2; i&#60;n; i++&#41;
     &nbsp; &nbsp; &nbsp; &nbsp;{
     &nbsp; &nbsp; &nbsp; &nbsp;	numberpool&#91;i&#93;=true;
     &nbsp; &nbsp; &nbsp; &nbsp;}
    
     &nbsp; &nbsp; &nbsp; &nbsp;limit = Math.sqrt&#40;n&#41;;
     &nbsp; &nbsp; &nbsp; &nbsp;int j=2;
     &nbsp; &nbsp; &nbsp; &nbsp;for &#40;int i=j+j; i&#60;n; i=i+j&#41;
     &nbsp; &nbsp; &nbsp; &nbsp;{
     &nbsp; &nbsp; &nbsp; &nbsp;	numberpool&#91;i&#93;=false;
     &nbsp; &nbsp; &nbsp; &nbsp;}
    
     &nbsp; &nbsp; &nbsp; &nbsp;for &#40;j=3; j&#60;=limit; j=j+2&#41;
     &nbsp; &nbsp; &nbsp; &nbsp;{
     &nbsp; &nbsp; &nbsp; &nbsp;	if &#40;numberpool&#91;j&#93;==true&#41;
     &nbsp; &nbsp; &nbsp; &nbsp;	{
     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for &#40;int i=j+j; i&#60;n; i=i+j&#41;
     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{
     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;	numberpool&#91;i&#93;=false;
     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
     &nbsp; &nbsp; &nbsp; &nbsp;}
    
     &nbsp; &nbsp; &nbsp; &nbsp;System.out.println&#40;&#34;Prime Numbers from &#34; + startat + &#34; to &#34; + max + &#34;&#58;&#34;&#41;;
     &nbsp; &nbsp; &nbsp; &nbsp;for &#40;int i=startat; i&#60;max; i++&#41;
     &nbsp; &nbsp; &nbsp; &nbsp;{
     &nbsp; &nbsp; &nbsp; &nbsp;	if &#40;numberpool&#91;i&#93;==true&#41;
     &nbsp; &nbsp; &nbsp; &nbsp;	{
     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;primesfound++;
     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
     &nbsp; &nbsp; &nbsp; &nbsp;}
     &nbsp; &nbsp; &nbsp; &nbsp;end_time = System.currentTimeMillis&#40;&#41;;
     &nbsp; &nbsp; &nbsp; &nbsp;System.out.println&#40;&#34;Time taken = &#34;+&#40;end_time - start_time&#41;&#41;;
     &nbsp; &nbsp; &nbsp; &nbsp;System.out.println&#40;&#34;Number of primes found &#34;+primesfound&#41;;
     &nbsp; &nbsp;}
    }
    I should say it&#39;s not my code (well, I never wrote it anyway), but here are a few results:

    Prime Numbers from 1 to 100000:
    Time taken = 16.0ms
    Number of primes found 9592

    Prime Numbers from 1 to 1000000:
    Time taken = 94.0ms
    Number of primes found 78498

    Prime Numbers from 1 to 10000000:
    Time taken = 860.0ms
    Number of primes found 664579

    On a given day or given circumstance, you think you have a limit.
    And you then go for this limit and you touch this limit and you think &quot;Ok, this is the limit&quot;.
    As soon as you touch this limit, something happens and you suddenly can go a little bit further.
    With your mind power, your determination, your instinct and the experience as well, you can fly very high.

    - Ayrton Senna, R.I.P.

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •