Results 1 to 4 of 4

Thread: Prime numbers

  1. #1

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

    Resolved Prime numbers

    For a coursework assignment i must implement the 'Sieve of Eratothenes' on a sorted array of number and then place all the prime numbers in an ArrayList. (I am using Java).

    Here is the psuedo code algorithm.
    Code:
    Eratosthenes(n) {
      a[1] := 0                          
      for i := 2 to n do a[i] := 1
      p := 2
      while p2  <  n do {
        j := p2
        while (j  <  n) do {             
          a[j] := 0
          j := j+p
        }
        repeat p := p+1 until a[p] = 1   
      }
      return(a)
    }
    And here is my Java code, but it doesn't work. I don't know what is wrong with it, it just hangs (possible infinate loop somewhere).
    Code:
    public void sievePrimes()
    {   
        numbers[0] = 0;
        int n = 100;
       
        for(int i=1; i<n; i++)
        {
            numbers[i] = 1;    
        }
        int p = 2;
        while(p*p <= n)
        {
            int j = p*p;    
            while(j <= n) //Error is possibly here, infinate loop!
            {
                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]);    
        }
    }
    Any help would be great. Thanks.
    Last edited by x-ice; Feb 25th, 2007 at 07:26 PM.

  2. #2
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Prime numbers

    Add p++ before while(numbers[p] != 1), otherwise you keep looping the same P (with your current code P is always 2).

    Here is a VB Classic version for those who are interested:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Function Eratosthenes(ByVal N As Long) As Long()
    4.     Dim A() As Long, I As Long, P As Long, J As Long
    5.     ' reserve enough space for the array
    6.     ReDim A(N)
    7.     ' initialize i
    8.     I = 2
    9.     ' loop from 2 to n and set all items to 1
    10.     Do While I < N: A(I) = 1: I = I + 1: Loop
    11.     ' initialize p and j
    12.     P = 2
    13.     J = 4
    14.     ' loop while the starting number fits in the array
    15.     Do While J < N
    16.         ' loop while in the array
    17.         Do While J < N
    18.             ' nullify
    19.             A(J) = 0
    20.             ' keep jumping to every P item
    21.             J = J + P
    22.         Loop
    23.         P = P + 1
    24.         ' loop until we find a known prime
    25.         Do Until A(P) = 1: P = P + 1: Loop
    26.         ' prepare J for the next loop
    27.         J = P * P
    28.     Loop
    29.     Eratosthenes = A
    30. End Function
    31. Private Sub Form_Load()
    32.     Dim Test() As Long, lngA As Long
    33.     Test = Eratosthenes(1000)
    34.     For lngA = 1 To UBound(Test)
    35.         If Test(lngA) = 1 Then Debug.Print lngA
    36.     Next lngA
    37. End Sub


    If someone appreciates small memory usage more than speed, modify Long arrays to Byte Also... one would get more speed if the thing was done the other way: mark those items that aren't prime numbers so there would be no need to loop all items to 1 in the beginning of the function.
    Last edited by Merri; Nov 22nd, 2005 at 10:45 AM.

  3. #3
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Prime numbers

    Quote Originally Posted by x-ice
    Code:
    public void sievePrimes()
    {   
        numbers[0] = 0;
        int n = 100;
       
        for(int i=1; i<=n; i++)
        {
            numbers[i] = 1;it must be i instead of 1    
        }
        int p = 2;
        while(p*p <= n)
        {
            int j = p*p;    
            while(j <= n) //Error is possibly here, infinate loop!
            {
                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]);    
        }
    }
    Any help would be great. Thanks.
    sorry but this code is quite confusing for me. i made a similar program long time back, but its in C++. see if it helps you:
    Code:
    /*Generating Prime Numbers using a procedure called
      sieve of Eratosthenes*/
    
    
    #include<iostream.h>
    #include<conio.h>
    
    void main()
     {
      clrscr();
      int num[100],i,j;
    
      //FILL AN ARRAY OF 100 FROM 1 - 100
    
      for(i=0;i<100;i++)
            num[i] = i+1;
    
      //START FROM THE SECOND ELEMENT i.e. 2
      i = 1;
      while(i<=100)
      //for(i=2;i<=100;i++)
       {
    
      //PROCEED TO THE NEXT NON-ZERO ELEMENT
    
        if(num[i]!=0)
         {
          for(j=i+1;j<=100;j++)
           {
    
      //SET ALL OF THE MULTIPLES OF THE NON-ZERO ELEMENT TO 0
    
            if(num[j]%num[i]==0)
             {
              num[j] = 0;
             }
           }
         }
        i++;
       }
    
      //PRINTING THE NON-ZERO ELEMENTS OF THE ARRAY
    
      cout<<"\n\t\t\t  ==========================\n";
      cout<<"\t\t\t  Prime Numbers from 1 - 100";
      cout<<"\n\t\t\t  ==========================\n\n    ";
      for(i=0;i<100;i++)
       {
        if(num[i]!=0)
         {
          cout<<num[i]<<" ";
         }
       }
      getch();
     }
    Show Appreciation. Rate Posts.

  4. #4

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

    Re: Prime numbers

    Thanks alot, got it working now.

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