Results 1 to 5 of 5

Thread: Finding prime numbers

Threaded View

  1. #1

    Thread Starter
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Resolved Finding prime numbers

    I have this code as an implementation of the 'Sieve of Eratosthenes'. But either it is super duper slow or it doesn't work. What is wrong with it?

    p.s. I am finding all the primes numbers in a sorted array with numbers 1-100 inclusive.
    Code:
    public void sievePrimes()
    {   
        numbers[0] = 0;
        int n = numbers[99];
       
        for(int i=1; i<n; i++)
        {
            numbers[i] = 1;    
        }
        int p = 2;
        while(p*p <= n)
        {
            int j = p*p;    
            while(j <=n)
            {
                numbers[j-1] = 0;
                j = j+p;
            }
            while(numbers[p] != 1)
            {
                p++;   
            }
        }
        for(int index=0; index < numbers.length; index++)
        {
            System.out.println(numbers[index]);    
        }
    }
    Last edited by x-ice; Feb 25th, 2007 at 07:27 PM.

Posting Permissions

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



Click Here to Expand Forum to Full Width